4

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?

  • for this you'll need a input sniffer for kbd devices find /dev|grep input|grep kbd. – user55518 Mar 18 '14 at 14:10
  • Technically, every key can be mapped to do anything :) – orion Mar 18 '14 at 19:51
  • @orion I agree. But the question is how?. :-) – Ionică Bizău Mar 18 '14 at 19:53
  • See @slm's answer. xbindkeys is a desktop-independent solution. gnome & KDE (and others probably) all have their own shortcut managers, so it really shouldn't be a problem.. – orion Mar 18 '14 at 19:56
10

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.

  • Very useful. Is it possible to create a little C++ application do detect these key presses, like xev does? But it seems that xev doesn't detect my key presses globally. It only prints in console when the opened window is focused. – Ionică Bizău Mar 18 '14 at 14:39
  • Probably. xev is just to detect what the key's scan code is. xbindkeys will catch this key when pressed globally and do something when this happens. – slm Mar 18 '14 at 15:51
  • Is saml a typo (your username is sml)? :-) Also, I guess you mean xbindkeys --defaults > ~/.xbindkeysrc. Right? For the special button xev outputs 248 NoSymbol. I tried to set a nautilus start when NoSymbol is pressed but nothing is happening. I restarted xbindkeys process. – Ionică Bizău Mar 18 '14 at 19:41
  • In some cases (but not here it turns out), the key's scan code needs to be declared to the kernel. I thought we had a question about that but I can't find it now. – Gilles Mar 18 '14 at 23:17
  • @IonicăBizău - no my username on my laptop is saml. My name is sam and I always suffix user accounts with an l to denote that it's local to the system. My username on SE sites is slm. These are my initials, which was traditionally what one would use to create user accounts on Unix. Or they'd use the first 2 letter from your 1st and middle names followed by 6 letters from your last name. – slm Mar 18 '14 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.