Remap an arbitrary key on keyboard to another output

Quick to answer questions about finding your way around Linux Mint as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums in the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
ecscott

Remap an arbitrary key on keyboard to another output

Post 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?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Jaws

Re: Remap an arbitrary key on keyboard to another output

Post 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.
ecscott

Re: Remap an arbitrary key on keyboard to another output

Post 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).
ecscott

Re: Remap an arbitrary key on keyboard to another output

Post 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?
Lord_Eager

Re: Remap an arbitrary key on keyboard to another output

Post 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:
ecscott

Re: Remap an arbitrary key on keyboard to another output

Post 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!
ecscott

Re: Remap an arbitrary key on keyboard to another output

Post by ecscott »

A single bump. I am so close it hurts! But not quite there yet.
sectionq

Re: Remap an arbitrary key on keyboard to another output

Post 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
patrickbeattie
Level 1
Level 1
Posts: 25
Joined: Mon Mar 22, 2021 4:01 am

Re: Remap an arbitrary key on keyboard to another output

Post 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.
Locked

Return to “Beginner Questions”