Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

BC
BC

Posted on

     

Remap keys in the keyboard in Ubuntu

We could usexmodmap to remap keys in ubuntu.

First, generate the current keycode map:

xmodmap -pke > .Xmodmap
Enter fullscreen modeExit fullscreen mode

Then open the.Xmodmap file, find the key you want to remap. In my case, the PageUp key is too close to the left arrow key and the PageDown key is too close to the right arrow key. Many times when I want to press the left arrow key, I pressed the PageUp key by accident. And I barely use the PageUp and PageDown key, so I want to remap the PageUp to LeftArrow and PageDown to RightArrow.

Once you open the .Xmodmap file, you will see a list of keycode and their functions, for example:

keycode  38 = a A a A
Enter fullscreen modeExit fullscreen mode

And it is easy to find the keycode for the LeftArrow and RightArrow keys:

keycode  83 = KP_Left KP_4 KP_Left KP_4keycode  85 = KP_Right KP_6 KP_Right KP_6
Enter fullscreen modeExit fullscreen mode

But I don't know the keycode for the PageUp and PageDown key, so I need to find that first.

To detect what the keycode is for specific key, use command:

$ xev -event keyboard
Enter fullscreen modeExit fullscreen mode

In my system, when I press PageUp, it shows 112:
Alt Text

So we can find that in the .Xmodmap file:

keycode 112 = Prior NoSymbol Priorkeycode 117 = Next NoSymbol Next
Enter fullscreen modeExit fullscreen mode

Now to remap those keys, we just need to change them to the Left and Right arrow:

keycode 112 = KP_Left KP_4 KP_Left KP_4keycode 117 = KP_Right KP_6 KP_Right KP_6
Enter fullscreen modeExit fullscreen mode

Now save the file, and activate the key map:

$ xmodmap .Xmodmap
Enter fullscreen modeExit fullscreen mode

Now if you press the "PageUp" key, it will work like the LeftArrow key.

But this change will only work in this login session. After you reboot the system, the keyboard will be reset to the default keys. To make it work on every reboot, we need to set an command to the "Startup Application":

$ xmodmap .Xmodmap$ xkbcomp $DISPLAY $HOME/.xkbmap
Enter fullscreen modeExit fullscreen mode

Now open "Startup Application":
Alt Text

Click Add, set name to "Remap Keys", and the command to:

xkbcomp /home/user/.xkbmap ":0"
Enter fullscreen modeExit fullscreen mode

Make sure you replace theuser above to your username. Then reboot your system, the remap should work on next boot.

Reference

Top comments(14)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
blackbeerben profile image
blackbeerben
  • Joined
• Edited on• Edited

$ xkbcomp /home/user/.xkbmap ":0"
Error: Cannot open display ":0"
Exiting

Gave up on terminal. Unsure what the command ":0" is supposed to do. I read this article-linuxuprising.com/2020/12/remap-ke...
Then I installed input-mapper-github.com/sezanzeb/input-remapper

Got this funky UK keyboard on a Starbook laptop with a narrow left hand shift key where there is an extra ">" less than key which I changed to be an additional left shift key. Now I can type at a normal speed again.

Running- Pop!_OS 21.10 x86_64 on a Starbook-mk-v

Thanks for the efforts. Couldn't get it to work for me.

CollapseExpand
 
abidkhairyak profile image
AbidKhairyAK
Just an ordinary programmer. mainly focused on laravel, vue, and react native.
  • Location
    Yogyakarta, Indonesia
  • Work
    Junior Fullstack Programmer at PT Mahir Techno
  • Joined

Pop OS use display ":1"

CollapseExpand
 
blackbeerben profile image
blackbeerben
  • Joined

Thanks for this

CollapseExpand
 
thefluxapex profile image
Ian Pride
You do not want to follow me. I'm a master gatekeeper and web devs aren't programmers. 😛
  • Location
    Decatur, Ill, U.S. of A.
  • Education
    Some college; Self taught
  • Work
    Command Line Programmer, Scripter at Self
  • Joined
• Edited on• Edited

Nice, you can also use the xdotool/AutoKey combo which I've been using for many years and have been planning a tutorial for it.
Actually it'll be for window management as well with wmctrl.
I'll also be doing one on AutoHotkey in Windows.

CollapseExpand
 
mulleresposito profile image
Müller Esposito Nunes
  • Joined

Hello, I can get to remap colon and semicolon keys to same key?

CollapseExpand
 
velcrine profile image
velcrine
  • Joined

yes you can

CollapseExpand
 
mulleresposito profile image
Müller Esposito Nunes
  • Joined

When I done the steps from that tutorial my Ubuntu didn't booting. I had to change the modifications that I made via the safe mode boot.

Thread Thread
 
velcrine profile image
velcrine
  • Joined

Well i do it this way:
Assigned hyphen, to another key. Only this. Now, normal key press prints hyphen + when I shift press that key, I get underscore too.
Right now _ - I just typed using those keys.
Better to try and test, before making them permanent.

CollapseExpand
 
xaander1 profile image
Alexander
In the darkest times, hope is something you give yourself. That is the meaning of inner strength.
  • Work
    Software Engeneer
  • Joined

Saved my day. Had keys that had stopped working.

CollapseExpand
 
henriquepgomide profile image
Henrique Gomide
  • Joined

Just loved your tutorial!

CollapseExpand
 
sefatanam profile image
Sefat Anam
actively looking for knowledge...

million dollar's article, man !

CollapseExpand
 
estacientral profile image
estación central
  • Joined

I'm having trouble setting it as a startup aplication. I am able to succesfully edit .Xmodmap but I cant make xkbcomp /home/user/.xkbmap ":0" work.

CollapseExpand
 
thucdev profile image
thucdev
  • Joined

Can I change Ctrl + C to Alt? And how can I do that?
Thanks.

CollapseExpand
 
vuducvi1999 profile image
Vuducvi1999
  • Joined

I just have only one thing to say: Thank you so much!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Coder, Learner
  • Location
    New York
  • Joined

More fromBC

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp