4

I'm interested in remapping some keys on my keyboard. In particular, I want to hold down the Function key, press the F1 key, and have this trigger a script. I know that xev is useful for figuring out what keycode is associated with a keypress. For example, when I press the x key, I get:

KeyPress event, serial 41, synthetic NO, window 0x2600001,
    root 0xae, subw 0x0, time 1597243, (-190,43), root:(910,336),
    state 0x0, keycode 53 (keysym 0x78, x), same_screen YES,
    XLookupString gives 1 bytes: (78) "x"
    XmbLookupString gives 1 bytes: (78) "x"
    XFilterEvent returns: False

KeyRelease event, serial 41, synthetic NO, window 0x2600001,
    root 0xae, subw 0x0, time 1597363, (-190,43), root:(910,336),
    state 0x0, keycode 53 (keysym 0x78, x), same_screen YES,
    XLookupString gives 1 bytes: (78) "x"
    XFilterEvent returns: False

This makes sense to me.

However, when I press Function-F1, I don't understand what happens. Why does pressing Function-F1 seem to behave as if I've also held the Windows button, and typed the letter l?

xev output when I press Function-F1:

KeyPress event, serial 41, synthetic NO, window 0x2600001,
    root 0xae, subw 0x0, time 1735349, (-157,221), root:(943,514),
    state 0x0, keycode 133 (keysym 0xffeb, Super_L), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyPress event, serial 41, synthetic NO, window 0x2600001,
    root 0xae, subw 0x0, time 1735350, (-157,221), root:(943,514),
    state 0x40, keycode 46 (keysym 0x6c, l), same_screen YES,
    XLookupString gives 1 bytes: (6c) "l"
    XmbLookupString gives 1 bytes: (6c) "l"
    XFilterEvent returns: False

MappingNotify event, serial 41, synthetic NO, window 0x0,
    request MappingKeyboard, first_keycode 8, count 248

KeyPress event, serial 41, synthetic NO, window 0x2600001,
    root 0xae, subw 0x0, time 1735356, (-157,221), root:(943,514),
    state 0x40, keycode 160 (keysym 0x1008ff2d, XF86ScreenSaver), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyRelease event, serial 41, synthetic NO, window 0x2600001,
    root 0xae, subw 0x0, time 1735357, (-157,221), root:(943,514),
    state 0x40, keycode 160 (keysym 0x1008ff2d, XF86ScreenSaver), same_screen YES,
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False

MappingNotify event, serial 42, synthetic NO, window 0x0,
    request MappingKeyboard, first_keycode 8, count 248

KeyRelease event, serial 42, synthetic NO, window 0x2600001,
    root 0xae, subw 0x0, time 1735463, (-157,221), root:(943,514),
    state 0x40, keycode 46 (keysym 0x6c, l), same_screen YES,
    XLookupString gives 1 bytes: (6c) "l"
    XFilterEvent returns: False

KeyRelease event, serial 43, synthetic NO, window 0x2600001,
    root 0xae, subw 0x0, time 1735470, (-157,221), root:(943,514),
    state 0x40, keycode 133 (keysym 0xffeb, Super_L), same_screen YES,
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False

When I press Function-F1 with a text window focused, it types the letter l.

Background information: Running Lubuntu 11.10 on a Toshiba r835. Happy to provide any further details that might be helpful.

EDIT:

For comparison, when I type Function-F12, I get:

KeyPress event, serial 41, synthetic NO, window 0x2200001,
    root 0xae, subw 0x0, time 8369184, (-620,473), root:(480,766),
    state 0x0, keycode 78 (keysym 0xff14, Scroll_Lock), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyRelease event, serial 41, synthetic NO, window 0x2200001,
    root 0xae, subw 0x0, time 8369344, (-620,473), root:(480,766),
    state 0x0, keycode 78 (keysym 0xff14, Scroll_Lock), same_screen YES,
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False

I wanted to check if there's something special about the F1-F12 keys, but it looks like at least some of them work how I expect.

2

2 Answers 2

3

When you say Function-F1, I assume you mean pressing that funny little Fn key in the corner of a laptop followed by the F1 key at the top left of the keyboard. And that you're trying to map one of the extra keys like Sleep that some laptop keyboards have.

It looks like that key is literally pressing multiple keys for you. First I see Win + L as one combination. On Windows, this shortcut locks the screen, but does not start the screensaver.

Next, I see a mapping for XF86ScreenSaver which might mean that X is seeing some kind of sleep keycode. I'm guessing that that key on your laptop keyboard is physically sending out those three keycodes of Win + L + Sleep as a convenience for Windows users to quickly lock the screen and activate the screensaver.

I'm not sure of a good way to filter those out. The Fn key on keyboards normally is not seen by the Linux Kernel. Instead, it changes which codes the keyboard tells the kernel. When I was trying to use a USB RF PowerPoint remote with OpenOffice on Linux, I discovered that the button to start/stop the presentation was just a lame control to send out alternating F5 and Esc key codes. Other buttons were just as lame, like sending out b to blank the screen.

7
  • Interesting. You think the hardware is responsible for the MappingNotify event? I assumed that somewhere in linux, the key had been mapped to multiple keypresses, and I just had to figure out where so I could deactivate the multiple mapping. For comparison, when I hit Fn-F12, I get an xev response that contains keycode 78 (keysym 0xff14, Scroll_Lock), but no MappingNotify-type stuff.
    – Andrew
    Commented Mar 4, 2012 at 0:54
  • 1
    Keycodes in X11 represent physical keys delivered by the kernel to the X server. The X server then uses various mechanisms to map them to Keysyms and then to strings, if needed. A normal keyboard sends one keycode per physical key using standard USB HID or PS/2 keycodes. However, some compressed, simplified, or simply non-standard keyboards can compose various keycodes differently depending on the state like when the numeric keypad is overlayed onto the letter keys. That's done in hardware. Also, sometimes keys like monitor select or sleep don't even generate normal codes, instead ACPI events
    – penguin359
    Commented Mar 4, 2012 at 3:10
  • @Andrew actually, I can't explain the MappingNotify itself without reviewing the X11 protocol again, but I do see three different keycodes listed above.
    – penguin359
    Commented Mar 4, 2012 at 3:12
  • @Andrew The Fn key combinations are mapped by the BIOS. The OS doesn't see them. Depending on the computer model and on the key, you may or may not be able to reconfigure it through ACPI. Commented Mar 5, 2012 at 17:09
  • Really! Interesting! I will check my bios. Any insight into what the MappingNotify events might be? I didn't have much luck googling them.
    – Andrew
    Commented Mar 6, 2012 at 4:51
0

I used xbindkeys for this purpose. Simply run xbindkeys -k and press the desired buttons.

You should get an output like:

"(Scheme function)"
    m:0x0 + c:244
    XF86Battery

Paste this into the .xbindkeysrc file in your home directory. Now you want to replace the (Scheme Function) part with the command you wish to execute, for example perl /home/my_script.pl to run your script.

This should make your task of remapping keys a lot easier.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .