How can I set a certain keycode to a key and bind it to the right action?

I have a laptop with Swedish keyboard layout, everything works well with the keyboard. Due to injuries I am using an external ergonomic USB keyboard, this is where I am experiencing difficulties.

The external keyboard doesn't have a key for Control_R or ALT-GR (I guess that's ALT_R) which causes trouble because typing @, $, and {} or [] all uses ALT-GR in Swedish keyboard layout.

UPDATE:: This is not true. The keyboard comes with both Control keys but was messed up due to custom remapping, read how to manage that here. After a complete reset I it turns out that the external keyboard has Alt-GR (shown as ISO_Level3_Shift by xev) as well (though backticks was hard to find). ::END UPDATE

The keyboard has a built-in remapping feature as well as macro features but there's no way to remap keys from the on-board keyboard to the external keyboard (as far as I know)

I would like to set one of my keys on the external keyboard to have a new keycode (such as Control_R or ALT_R and then remap that to acting as ALT-GR - is this possible? The reason I don't want the keycode Control_L is, if I remap it then the other Control key will be remapped as well.

With xev I have checked what keycode is trigged by clicking the key of ALT-GR on my on-board keyboard and that's keycode 108 (keysym 0xfe03, ISO_Level3_Shift). After some reasearch I've noticed this is not expected for the ALT-GR key, but this key works as I expect.

With xev I also check the output for the key on the external keyboard which I desire to use as ALT-GR: keycode 105 (keysym 0xffe3, Control_L).

I have another key on the external keyboard outputting the exact same thing, that key is the one I use for Control_L, making the one I wish was ALT_GR redundant.

I have messed around with xmodmap to see if I can get what I need but alas, with no success. Here are a few scenarios.

xmodmap -e "keycode 105 = Alt_R" #=> This disables both Control keys and doesn't work like ALT-GR, virtually the key doesn't do anything.

xmodmap -e "keycode 105 = ISO_Level3_Shift" #=> Same as above

Is there a way to address these problems?


Setup:

  • Ubuntu 13.10
  • External USB keyboard (Kinesis Advantage Pro)

Switch to a virtual console (CTRL+ALT+F1) and run

showkey -s

This will print out the scancodes of the keys, so just press those left and right CTRL keys and see whether they have the same scancodes. After you verified that, wait 10 seconds and don't press any key, so the showkey program can return. Then switch back to X (ALT+F7).

If the scancodes were different, then there is hope that those keys can be remapped differently (because the kernel sees them different), if the scancodes are the same, then there is no way for a different remapping.


If you have two keys with the same scancode, remapping those keys to another action will remap both keys with that scancode. There's no way (as far as I know) to differentiate two different keys if they have the same scancode.

As @falconer suggests, you can see if your two keys in fact are different or the same by checking with showkey -s in the virtual console. Remember how to switch back to X (ALT+F7) so you don't get stuck in the virtual console.

If you press your two keys that you wish to map differently, and those two keys gives you the same output here, then you need to look for another solution.

The external keyboard you are talking about does in fact have keys with the scancodes mapping to actions that you suggest are missing (Control_L and Control_R) - I would recommend you to reset the keyboard to it's default settings and check the output of your keys with xev again.

Make sure that you start with the correct Operative system settings, on your keyboard they are:

=+ + w for windows.

=+ + p for PC (non-windows).

=+ + m for Mac.

Setting that up before you start remapping will get you off to a good start with all the scancodes that you need.

As for the Alt-GR issue, ISO_Level3_Shift should be the correct action for this key (as far as I know). Keys with that action triggers the behavior of Alt-GR correctly.

Additionally, when tampering with scancodes and actions it may be nice to have some kind of software like KeyMon which will tell you what key or key combination you are pressing.