Keyboard Shortcuts – How to Detect Global Key Presses

keyboardkeyboard shortcutsx-server

I have an Asus laptop that has a special key that can be configured to launch any software (at least on Windows).

The general question is: how can I detect any key press (globally)?

Then, how can I detect when the user press this key?

Best Answer

I typically will use xev to determine the key's scancode and then map it to whatever action I want using either xdotool or XBindKeys.

xev

$ xev | grep -A2 --line-buffered '^KeyRelease' \
    | sed -n '/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p'

After running the above xev command you'll get a little white window that'll pop up. You'll want to put the mouse over this window and then press the problem key. The name of the key should be showing up in the terminal as you press the various keys.

Screenshot

                   ss of xev dialog

mapping the key to something useful

You can create shortcut key combinations that will launch commands using xbindkeys, for example. I've successfully been using XBindKeys on GNOME 3.8.4 for this very purpose.

My use has been modest but I like to create keyboard shortcuts for Nautilus to launch with certain directories opened.

Example

You'll need to first make sure the packages xbindkeys is installed.

Then you'll need to run the following command, one time only, to create a template xbindkeys configuration file.

$ xbindkeys --defaults > /home/saml/.xbindkeysrc

With the file created you can open it in a text editor and add a rule like this:

"nautilus --browser /home/saml/projects/path/to/some/dir"
  Mod4+shift + q

With the above change made we need to kill xbindkeys if it's already running and then restart it.

$ killall xbindkeys
$ xbindkeys

Now with this running any time I type Mod+Shift+Q Nautilus will open with the corresponding folder opened.

Using GNOME's Keyboard Applet

If you go through the settings (System SettingsKeyboard, select Shortcuts tab and add a new custom shortcut for your browser.

   ss #1

Using the steps 1-5 as in the diagram you could map a command to your special key as well.

Related Question