Swap ctrl caps linux

Tech Stuff and Notes

on software development, computing science, software technologies, learning, etc.

2021-02-10

Remap one key to another in Linux, like Capslock to Control

Linux is known for choice and customizability.

So it’s ironic but when it comes to keyboard and mouse key remapping and scripting, Linux is the more challenging than Windows (using AutoHotKey) or Macs (using Karabiner-Elements).

I’ll link to some guides and answers below, but some big picture ideas to be aware of in 2020 for Linux key remapping:

xmodmap is deprecated

Prefer using setxkbmap if it’ll do what you want.

Some xmodmap functionality remains, but it’s clearly a deprecated system and it sometimes doesn’t work.

I tried to do some simple key remapping with it on Debian LXDE and it didn’t work for me.

setxkbmap is preferred but complicated

It has many common remappings like turning capslock to control [1]:

setxkbmap -option -option caps:ctrl_modifier

The first blank «-option» clears any previously set. Then the second option sets the capslock to control.

Swapping the left control and alt keys can be easy to do as there’s a direct option for it too.

Running setxkbmap only affects the current session, so logging out will wipe the settings. To make it auto set on login, put it in a script run from .bashrc or similar.

But what if you want to swap the right control and alt keys instead? Or some weird 4 way switch of keys?

There many be some set of xkeyboard-config options that’ll work, but it’s not straightforward the way it is on a Mac with Karabiner-Elements.

You also may have to find and use keycodes or events generated by your keyboard or mouse interactively. Do this using the xev command line app [3].

If you want to go down this route, be sure to check out: A simple, humble but comprehensive guide to XKB for linux. But if you want an easier way, check out the last section below because, of course, there’s an app for that!

Show setxkbmap options

To find what options are supported by setxkbmap, you can run [2]:

man xkeyboard-config is not complete: e.g. right alt to right control

More annoyingly, turns out running man xkeyboard-config does not list a complete list of what setxkbmap can do!

For example, let’s say you want to make the right alt another right control key.

It’s not listed in man xkeyboard-config but it is easily possible! Just run:

setxkbmap -option ctrl:ralt_rctrl

It is listed in a lower level description file at however [4] :

Читайте также:  Ctrl command in linux

And if you’re up to it, you can change the low level description files that setxkbmap uses to give you great control over the keyboard remappings. But it’s not easy the way it is on Macs and Windows!

Remapping single keys using hwdb files

This is lower level than setxkbmap, but it’s not deprecated like xmodmap, and for single key to single key remapping, it seems to work well.

Use an app to intercept and inject keys

This method allows more complex hotkeys and combos to be remapped to almost arbitrary input.

This is really new. Try out sezanzeb’s key-mapper. It works with X11 and Wayland, and has an easy GUI interface to it.

Since key-mapper is a Python program, you’ll need Python to run it anyway. So on Debian, you might as well just install Python and pip, then use pip to install it.

Key-mapper is not available in Debian’s package system, so you’ll have to download and install it from the key-mapper Github repository.

In fact, Debian has a package called «keymapper» that is not the key-mapper you’re looking for. Using apt to install key-mapper, even key-mapper’s own pre-packaged .deb file, kept suggesting the wrong «keymapper» package instead.

Probably easiest to just install it with pip!

(NEW!) Check out kinto if you’re interested in mac-style hotkeys on Linux and Windows!

Источник

How to swap Ctrl and Caps Lock keys in Linux

For many people who’ve been computer users for (let’s just say) «quite some time now,» the Ctrl and Caps Lock keys have been in the wrong place since shortly after the first PC keyboards rolled off the production line. For me, the correct positioning appears in this image of a vintage 1995 Sun Workstation keyboard. (Forgive me for the blurriness of the image; it was taken with a Minox spy camera in low light.)

If you’re interested, you can read about the history of the Ctrl key location. I’m not going to discuss the various rationales for placing the Ctrl key next to the «a» key versus below the Shift key; I’m not going to comment on the overall uselessness of the Caps Lock key (whoops); and I’m not going to argue with those who advocate using the heel of the hand to activate the Ctrl key, even though it’s impossible to do on some laptop keyboards where the keys are inset below the level of the wrist rest (whoops).

Rather, I’m going to assume I’m not the only one who prefers the Ctrl key next to the «a» and describe how to use the wonderful flexibility that comes with Linux to swap the Ctrl and Caps Lock keys on various desktop environments. Note that this kind of advice seems to have a limited shelf life, as tools for tweaking desktop settings change fairly often. But I hope this offers a good place for you to start.

With GNOME 3

GNOME 3 desktop environment users can use the Tweaks tool to swap their Caps Lock and Ctrl keys, as you can see below.

Читайте также:  Linux ssh указать порт

GNOME Tweaks Additional Layout Options

  1. Install the Tweaks tool from your distribution’s repositories.
  2. Start the Tweaks application.
  3. Select «Keyboard & Mouse» from the left-hand menu.
  4. Click «Additional Layout Options».
  5. Click «Ctrl position» on the window that opens and choose «Swap Ctrl and Caps Lock.»

That’s it! By the way, you can do lots of cool stuff with the Tweaks tool. For example, I set my right Ctrl key to be a Compose key, which allows me to type all sorts of characters with keyboard shortcuts—such as ç, é, ô, and ñ and with the keystrokes Compose+c+Comma; Compose+e+Right quote; Compose+o+Circumflex; and Compose+n+Tilde.

With KDE

I don’t use KDE, but item 5 in this article about KDE tweaks that will change your life by my colleague Seth Kenlon will show you how to remap your keys.

With Xfce

As far as I can tell, the Xfce desktop environment doesn’t have a handy tool for managing these kinds of settings. However, the ctrl:swapcaps option to the setxkbmap command will help you make these changes. This type of modification has two parts:

  1. Figuring out the command’s usage;
  2. Figuring out where to invoke the command so it is activated as the desktop comes up.

The first part is pretty straightforward: the command is:

/usr/bin/setxkbmap -option "ctrl:nocaps"

It’s worth executing this in a terminal window to make sure the results are what you expect.

Assuming it works, where should you invoke the command? That requires some experimentation; one possibility is in the file .profile in the user’s home directory. Another option is to add the command to the autostart facility in Xfce (look for «Session and Startup» in the Settings Manager).

Another possibility is to use the same option in the file /etc/default/keyboard, which might end up looking like this:

# KEYBOARD CONFIGURATION FILE # Consult the keyboard(5) manual page. XKBMODEL="pc105" XKBLAYOUT="us" XKBVARIANT="" XKBOPTIONS="ctrl:swapcaps" BACKSPACE="guess"

Note that this kind of change will affect all users, so if you share your computer, be prepared to do some explaining. Also, system updates may overwrite this file, so you’ll need to edit it again if your setup stops working. Putting the same information in the file .keyboard in the user’s home directory might accomplish the same task on the user’s behalf.

Finally, note that these kinds of changes require you to restart Xfce (except when running the command on the command line in the terminal window, but that won’t stick past the end of the session).

With LXQt and other desktop environments

I haven’t tried LXQt, but if my memory serves from LXDE, I would try the same recipe used above for Xfce. I’d also expect that the Xfce recipe could work for other Linux desktop environments, but, of course, your favorite search engine is always your friend.

The console

I haven’t tried this, as I have very few opportunities to interact with the console (what you see on a server or when your window system doesn’t come up properly). The recipes presented above affect the terminal window in the way one would hope, i.e., consistently with other applications.

Читайте также:  Network script in linux

However, if the file /etc/default/keyboard or ~/.keyboard has already been edited (as described above), the utility setupcon is intended to change the console keyboard setup so it functions the same way. This StackExchange article, this other one, and this third one give some ideas on how to effect these changes from both of these files. The third article also talks about using dumpkeys and loadkeys. It’s also worthwhile to read the setupcon man page — it’s short and to the point, and combined with the comments from the StackExchange articles, should be enough to get a solution in place.

Finally, it’s worth emphasizing here the point mentioned in the StackExchange articles — configuring the console IS NOT THE SAME as configuring terminal windows; the latter are configured through the desktop manager as described previously.

When all else fails

The manual pages for setxkbmap, xkeyboard-config, keyboard, console-setup, and setupcon are all useful references. Or, if you don’t like reading manual pages, there’s this great article.

Источник

how to swap ctrl and capslock using xmodmap?

And this script doesn’t work.
Could some explain this(why the 1st script works and the other one doesn’t) in simple words?

3 Answers 3

man xmodmap shows how to swap the left control key and the CapsLock key:

! ! Swap Caps_Lock and Control_L ! remove Lock = Caps_Lock remove Control = Control_L remove Lock = Control_L remove Control = Caps_Lock keysym Control_L = Caps_Lock keysym Caps_Lock = Control_L add Lock = Caps_Lock add Control = Control_L 

Those keysym lines are important since they’re the ones that are mapping the keycodes to the opposing keys, i.e. keycode for Capslock goes to Control L and vice versa.

excerpt from the xmodmap man page*

 keysym KEYSYMNAME = KEYSYMNAME . The KEYSYMNAME on the left hand side is translated into matching keycodes used to perform the corresponding set of keycode expressions. Note that if the same keysym is bound to multiple keys, the expression is executed for each matching keycode. 

Seeing the effect

You can use the tool xev to see that the keys have been literally remapped. So Capslock now sends the scancode for Control L .

Example

Pressing Capslock sends Control L .

$ xev KeyPress event, serial 36, synthetic NO, window 0x3e00001, root 0x86, subw 0x0, time 890946390, (803,237), root:(804,294), state 0x2, keycode 66 (keysym 0xffe3, Control_L), same_screen YES, XLookupString gives 0 bytes: XmbLookupString gives 0 bytes: XFilterEvent returns: False KeyRelease event, serial 36, synthetic NO, window 0x3e00001, root 0x86, subw 0x0, time 890946462, (803,237), root:(804,294), state 0x6, keycode 66 (keysym 0xffe3, Control_L), same_screen YES, XLookupString gives 0 bytes: XFilterEvent returns: False 

Pressing Control L sends Capslock .

$ xev KeyPress event, serial 36, synthetic NO, window 0x3e00001, root 0x86, subw 0x0, time 891083183, (793,9), root:(794,66), state 0x0, keycode 37 (keysym 0xffe5, Caps_Lock), same_screen YES, XLookupString gives 0 bytes: XmbLookupString gives 0 bytes: XFilterEvent returns: False KeyRelease event, serial 36, synthetic NO, window 0x3e00001, root 0x86, subw 0x0, time 891083302, (793,9), root:(794,66), state 0x2, keycode 37 (keysym 0xffe5, Caps_Lock), same_screen YES, XLookupString gives 0 bytes: XFilterEvent returns: False 

References

Источник

Оцените статью
Adblock
detector