Creating Custom Keyboard Shortcuts

Filed in: Documentation.CustomKeyboardShortcuts · Modified on : Wed, 16 Dec 09

For users who wish to have smart keyboard or mouse shortcuts to open applications, run commands or insert a certain combination of keystrokes, move the mouse, etc. (macros), this guide may come in handy.

1.  Installing

If you don't need to run macros, installing xbindkeys will be sufficient.

 $ sudo apt-get install xbindkeys

If you want to run macros, you should install xmacro as well. (xmacro is able to press keys, mouse buttons and move the mouse.)

 $ sudo apt-get install xmacro

2.  Configuring xbindkeys

Create a file called .xbindkeysrc in your home folder.

 $ touch ~/.xbindkeysrc

Open the newly created file in your favourite text editor and add the following:

 "[command]"
 [key(s)]

Now, for the sake of testing if it works, replace those two lines with the following:

 "icecat &"
 Shift + F5

Save the file and run the following in a terminal:

 $ xbindkeys

Now, when you press Shift and F5, icecat will be launched.

Next, let's try and add another command. Edit the .xbindkeysrc file and append the following lines:

 "gedit &"
 Shift + F6

Save the file, and run xbindkeys again.

 $ xbindkeys

Now, gedit will be opened when Shift and F6 are pressed.

NOTE: Some keys and keycombinations may be predefined by the window manager, and will overwrite your keybinds. If your keybind doesn't work, try another keycombination to make sure that it is not caused by the window manager.

NOTE: You need to start xbindkeys everytime the system starts, in order for your binds to work.

2.1  Using the mouse

You can also use mouse buttons for keyboard shortcuts. Let's try and replace F6 with b:2 (this is the middle-button on most mice. If your mouse doesn't have a middle button, try b:1 for LMB or b:3 for RMB). Run xbindkeys again and when you hold Shift and press your middle mouse button, gedit should be opened.

If you have a mouse with a lot of buttons, you can define their behaviour this way.

3.  Combining xbindkeys and xmacro

If you want a certain key or combination to yield another combination, you need to use xmacro. In this example, we will make Shift + Mouse scroll (up/down) press the left and right arrow keys. This can be useful when selecting text using the mouse.

Open your .xbindkeysrc file and add the following lines:

 "echo -e 'KeyStrPress Left\n KeyStrRelease Left' | xmacroplay ':0.0'"
 Shift + b:4

 "echo -e 'KeyStrPress Right\n KeyStrRelease Right' | xmacroplay ':0.0'"
 Shift + b:5

3.1  Getting xmacro events

Writing macros can be difficult, if you don't know the names of the keys you want to press. In short, there are 13 different events, that xmacro can run:

  • Delay [sec] -- Waits for [sec] seconds
  • ButtonPress [n] -- Presses mouse button [n]
  • ButtonRelease [n] -- Releases mouse button [n]
  • MotionNotify [x] [y] -- Moves the mouse to [x] [y]
  • KeyCodePress [kc] -- Presses the key with the keycode [kc]
  • KeyCodeRelease [kc] -- Releases the key with the keycode [kc]
  • KeySymPress [ks] -- Presses the key with the keysym [ks]
  • KeySymRelease [ks] -- Releases the key with the keysym [ks]
  • KeySym [ks] -- Presses and releases the key with the keysym [ks]
  • KeyStrPress [ksname] -- Presses the key with the keysym name [ksname]
  • KeyStrRelease [ksname] -- Releases the key with the keysym name [ksname]
  • KeyStr [ksname] -- Presses and releases the key with the keysym name [ksname]
  • String [max. 1024 long string] -- Presses and releases the keys associated with the characters in the string, in the sequence in which they appear. Example: "ABCD" will press and release "A", "B", "C" and "D".

If you don't know the name of the event you wish to use, you can use xmacrorec2, which will output the events that you press to a terminal. Alternatively, you can make xmacrorec2 record the events to a file, which can then later be run.

 $ xmacrorec2 > my.macro
 >> Record macro...
 $ cat my.macro | xmacroplay ":0.0"

4.  Useful examples

There are several ways using xbindkeys and xmacro, to create neat and useful shortcuts. If you use a shortcut that you think might be useful to other users, please add it below.

Open top by holding shift down and clicking first with LMB, then with RMB.

 "gnome-terminal --hide-menubar --title=top --geometry=80x45-2-27 -x top &"
 Shift + b:1 then Shift + b:3

Page last modified on December 16, 2009, at 09:36 PM

gNewSense is a project developed by volunteers all over the world and it's supported by the Free Software Foundation.

The content in this Web site can be used as follows:

All documentation is available under the terms of the GFDL with no invariant sections. ( note on the license )

Artwork is Free Cultural Work and is available under the terms of the cc-by-sa license.