Page 1 of 1

Remap an arbitrary key on keyboard to another output

Posted: Sun Jul 29, 2012 1:31 am
by ecscott
I have a calculator key on my new laptop, right next to the control key. My muscle memory from my old keyboard keeps hitting calculator key instead of the control. Until I find a compelling use for the key I'd ideally like to have it as an additional control. Hopefully having multiple keys doing the same thing on the keyboard is possible?

I'm asking how to change any key on the keyboard to another because there's a few other changes I'd like to make in the future (there's a few other shortcut keys besides calculator). I need to know how to set this up this change permanently and reversibly (in case something goes terribly wrong)

I am a complete newbie and would prefer it explained like I'm five. However, if one of the steps ends up being "go read this tutorial on editing these kinds of configuration files," that would be fine too--I installed Linux in part to learn how to configure arbitrary things about my computer.

edit: Changed "Change" in subject to remap, per further googling. Still hoping for step-by-step instructions, however. "xmodmap" looks like something I'll have to use?

Re: Remap an arbitrary key on keyboard to another output

Posted: Sun Jul 29, 2012 9:59 am
by Jaws
Not sure if you're running the gnome DEs but in KDE there are settings under System Settings -> Hardware -> Input Devices -> Keyboard where you can change keyboard models, layouts (including shortcuts for switching layout, and configure keyboard options. Maybe this will help.

Re: Remap an arbitrary key on keyboard to another output

Posted: Sun Jul 29, 2012 12:12 pm
by ecscott
Jaws wrote:Not sure if you're running the gnome DEs but in KDE there are settings under System Settings -> Hardware -> Input Devices -> Keyboard where you can change keyboard models, layouts (including shortcuts for switching layout, and configure keyboard options. Maybe this will help.
I'm pretty sure I'm running Mate. I don't quite remember what I ended up installing, what's the quick and dirty way to check? (I have a program called eye of mate, which is why I'm guessing I have Mate installed).

Re: Remap an arbitrary key on keyboard to another output

Posted: Sun Jul 29, 2012 12:36 pm
by ecscott
I think I'm making progress, I found http://askubuntu.com/questions/24916/ho ... rtain-keys to be pretty much what I'm looking for.

Using xev, here is the output for "ctrl" and then, immediately after, "calc":

Code: Select all

KeyPress event, serial 33, synthetic NO, window 0x3600001,
    root 0xaf, subw 0x0, time 79820428, (374,-425), root:(377,82),
    state 0x0, keycode 37 (keysym 0xffe3, Control_L), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyRelease event, serial 33, synthetic NO, window 0x3600001,
    root 0xaf, subw 0x0, time 79820533, (374,-425), root:(377,82),
    state 0x4, keycode 37 (keysym 0xffe3, Control_L), same_screen YES,
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False

FocusOut event, serial 33, synthetic NO, window 0x3600001,
    mode NotifyGrab, detail NotifyAncestor

FocusIn event, serial 33, synthetic NO, window 0x3600001,
    mode NotifyUngrab, detail NotifyAncestor

KeymapNotify event, serial 33, synthetic NO, window 0x0,
    keys:  4294967265 0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   
           0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   

FocusOut event, serial 33, synthetic NO, window 0x3600001,
    mode NotifyNormal, detail NotifyNonlinear
...the output for the calc key is really confusing to me. I'm not sure how that would fit into the xmodmap editing instructions that are listed in the link apply to the output I actually got.

edit: updated pasted xev output to a possibly more relevant snippet. Seems to be double-reporting keypresses in the output?

Re: Remap an arbitrary key on keyboard to another output

Posted: Sun Jul 29, 2012 12:59 pm
by Lord_Eager
This is not what he's searching for.
It's very easy to achieve what you ask for.

First thing you need to know is the key code for the special key you want to configure, to achieve that write in a terminal:
xev

a small window will open and since then every key you press, or movement of mouse is recorded and print on terminal, so don't move mouse a press the key you are interested to know the code, and suddenly some values will appear in the terminal window. The one you are interested in is key code. Keep in mind that value or write it down somewhere.

Now write in terminal:
xmodmap -pke > $HOME/.keymap.conf
and press Enter.
Then:
sudo nano $HOME/.keymap.conf
search for the key code you previously wrote down, and write after the "=" :

Code: Select all

Control_L NoSymbol Control_L NoSymbol Control_L


If your keycode instead is not there in the list it means that the key is not mapped in the layout you choose, so try to add the line you need, something like:
keycode XX = Control_L NoSymbol Control_L NoSymbol Control_L
where "XX" is the key code you previously wrote down.

Save it by pressing Ctrl+o and exit by pressing Ctrl+x.
Now write in the terminal:
xmodmap $HOME/.keymap.conf
And check if your special key acts like Ctrl.

If it works fine you should in the end write a small script to execute at startup, so that you will have your key mapped every time you start your machine up.

Write in a terminal:
sudo nano /etc/init.d/keymap.sh
and copy inside the empty file modifying "user" with your account name:

Code: Select all

#!/bin/sh
PATH=/home/"user"/.keymap.conf
#Execute key re-mapping
xmodmap $PATH
save the file and exit.

Last thing to do is to write in terminal:
sudo update-rc.d keymap.sh defaults
Reboot machine and pray :D
it should work, however if it doesn't we could try to understand what's the matter.
I hope it's enough "5yrs old style" :lol:

Re: Remap an arbitrary key on keyboard to another output

Posted: Thu Sep 20, 2012 3:13 pm
by ecscott

Code: Select all

KeymapNotify event, serial 33, synthetic NO, window 0x0,
    keys:  2   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   
           0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   

FocusOut event, serial 33, synthetic NO, window 0x3600001,
    mode NotifyGrab, detail NotifyAncestor

FocusIn event, serial 33, synthetic NO, window 0x3600001,
    mode NotifyUngrab, detail NotifyAncestor

KeymapNotify event, serial 33, synthetic NO, window 0x0,
    keys:  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   
           0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   

FocusOut event, serial 33, synthetic NO, window 0x3600001,
    mode NotifyNormal, detail NotifyNonlinear
Thanks for the fantastic answer! It's been a few months, but I got reannoyed enough at this problem to try implimenting your solution.

Using xev, I can get other keys to give key codes, even from that same row of proprietary keys, but the calculator key doesn't?

when I try the key right above it, a printer key, I get something completely different:

Code: Select all

KeyPress event, serial 33, synthetic NO, window 0x3600001,
    root 0xaf, subw 0x0, time 57835909, (56,-17), root:(59,53),
    state 0x0, keycode 37 (keysym 0xffe3, Control_L), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyPress event, serial 33, synthetic NO, window 0x3600001,
    root 0xaf, subw 0x0, time 57835910, (56,-17), root:(59,53),
    state 0x4, keycode 33 (keysym 0x70, p), same_screen YES,
    XLookupString gives 1 bytes: (10) ""
    XmbLookupString gives 1 bytes: (10) ""
    XFilterEvent returns: False

KeyRelease event, serial 33, synthetic NO, window 0x3600001,
    root 0xaf, subw 0x0, time 57836047, (56,-17), root:(59,53),
    state 0x4, keycode 33 (keysym 0x70, p), same_screen YES,
    XLookupString gives 1 bytes: (10) ""
    XFilterEvent returns: False

KeyRelease event, serial 33, synthetic NO, window 0x3600001,
    root 0xaf, subw 0x0, time 57836050, (56,-17), root:(59,53),
    state 0x4, keycode 37 (keysym 0xffe3, Control_L), same_screen YES,
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False
When I hit the calculator key, another thing happens: I get a pop up dialog that states: "Couldn't execute command: gcalctool
Verify that this is a valid command."

So I went and installed gcalctool, the key now launches the calculator, and using xev gets me this readout:

Code: Select all

VisibilityNotify event, serial 33, synthetic NO, window 0x3600001,
    state VisibilityPartiallyObscured

FocusOut event, serial 33, synthetic NO, window 0x3600001,
    mode NotifyNormal, detail NotifyNonlinear

PropertyNotify event, serial 33, synthetic NO, window 0x3600001,
    atom 0x186 (_NET_WM_ICON_GEOMETRY), time 58314031, state PropertyNewValue
Stuck right at the beginning!

Re: Remap an arbitrary key on keyboard to another output

Posted: Sun Sep 23, 2012 4:53 pm
by ecscott
A single bump. I am so close it hurts! But not quite there yet.

Re: Remap an arbitrary key on keyboard to another output

Posted: Fri Jan 25, 2013 6:12 am
by sectionq
Hi guys, I hope someone still sees this as it's an old post. I originally put this on the newbies section but it was suggested that it was possibly a bit much for there, so hopefully it'll work better here. Anyway, I've been following the instructions for remapping and it's all working well but I'm struggling to get the keymappings to stick after I reboot.

The problem seems to be that when I simply put this in the keymap.sh file (q is my user name)
#!/bin/sh
PATH=/home/q/.keymap.conf
#Execute key re-mapping
xmodmap $PATH
and then write in terminal "
sudo update-rc.d keymap.sh defaults
"
And it tells me...
update-rc.d: warning: /etc/init.d/keymap.sh missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
System start/stop links for /etc/init.d/keymap.sh already exist.
So not knowing what I'm doing I add in what I figure to be LSB information at the start
### BEGIN INIT INFO
# Provides: scriptname
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
#!/bin/sh
PATH=/home/"user"/.keymap.conf
#Execute key re-mapping
xmodmap $PATH
I managed to get rid of the warnings but it stiil tells me "
System start/stop links for /etc/init.d/keymap.sh already exist.
"

So after a reboot my keymappings are all lost.

Any ideas?

Thanks

Jamie

Re: Remap an arbitrary key on keyboard to another output

Posted: Fri Jul 15, 2022 10:47 pm
by patrickbeattie
Lord_Eager wrote: Sun Jul 29, 2012 12:59 pm This is not what he's searching for.
It's very easy to achieve what you ask for.

First thing you need to know is the key code for the special key you want to configure, to achieve that write in a terminal:
xev

a small window will open and since then every key you press, or movement of mouse is recorded and print on terminal, so don't move mouse a press the key you are interested to know the code, and suddenly some values will appear in the terminal window. The one you are interested in is key code. Keep in mind that value or write it down somewhere.

Now write in terminal:
xmodmap -pke > $HOME/.keymap.conf
and press Enter.
Then:
sudo nano $HOME/.keymap.conf
search for the key code you previously wrote down, and write after the "=" :

Code: Select all

Control_L NoSymbol Control_L NoSymbol Control_L


If your keycode instead is not there in the list it means that the key is not mapped in the layout you choose, so try to add the line you need, something like:
keycode XX = Control_L NoSymbol Control_L NoSymbol Control_L
where "XX" is the key code you previously wrote down.

Save it by pressing Ctrl+o and exit by pressing Ctrl+x.
Now write in the terminal:
xmodmap $HOME/.keymap.conf
And check if your special key acts like Ctrl.

If it works fine you should in the end write a small script to execute at startup, so that you will have your key mapped every time you start your machine up.

Write in a terminal:
sudo nano /etc/init.d/keymap.sh
and copy inside the empty file modifying "user" with your account name:

Code: Select all

#!/bin/sh
PATH=/home/"user"/.keymap.conf
#Execute key re-mapping
xmodmap $PATH
save the file and exit.

Last thing to do is to write in terminal:
sudo update-rc.d keymap.sh defaults
Reboot machine and pray :D
it should work, however if it doesn't we could try to understand what's the matter.
I hope it's enough "5yrs old style" :lol:
I have successfully followed the above steps given by Lord_Eager but would like to change one of my keys to the copy command (Ctrl+C) what is the code I must add after the = for this.
If Ctrl is "Control_L NoSymbol Control_L NoSymbol Control_L"
keycode XX = Control_L NoSymbol Control_L NoSymbol Control_L
what is the copy command (Ctrl+C)?
Any help would be appreciated.