1

I am using CentOS 7 without GUI. How can I create a custom keyboard layout and use it as default permanently? Which file should I edit?

EDIT:

I can get it working under GUI, setting keymaps by editing files under /usr/share/X11/xkb/symbols and reboot, but it is not affecting terminal(at least not the one I launch with Ctrl+Alt+F5. In this terminal, I still use US standard. Launching terminal under GNOME uses my layout.

I put my keymap files under /lib/kbd/keymaps/legacy/i386/xxx/ as xxx.map.gz, and it actually loads fine with loadkeys, and when I run localectl I see:

# localectl
System Locale: LANG=en_US.UTF-8
   VC Keymap: xxx
  X11 Layout: us

But in the terminal opened with Ctrl+Alt+F5, the keyboard layout is the old one.

2 Answers2

2

To start with the existing configuration:

  • The current keyboard layout can be dumped using dumpkeys.
  • Other default layouts are available usually below the /lib/kbd/keymaps directory as gzipped files, use zcat or zless to easilly access their content.

To get the code of some of your keyboard keys, use showkey.

To manually load a keyboard layout file, use loadkeys. It accepts the path to a customized file as parameter.

Once you are happy with the result, you can set the name of the new layout to use by default in /etc/vconsole.conf:

KEYMAP="your-layout"
  • Thanks @WhiteWinterWolf! So, I think I have GNOME installed but deactivated(because when launching, I see a grey screen of GNOME and then I enter bash) , and I read somewhere that I must enter /usr/share/X11/xkb to change settings for GNOME. The changes to files /lib/kbd/keymaps applies to xkb, too? Or they are independent? Under my situation, which takes precedence? – WesternGun Oct 26 '17 at 10:19
  • @FaithReaper The commands and changes described in this post will only work with a virtual console (no X server, meaning no Gnome, KDE, or whatever, just the pure command-line I assumed you were using when stating "without GUI"). When a graphical interface is used (even if it is to display a terminal in a window!), you may try to use X configuration tools (like setxkbmap) as long as your desktop manager does not overwrite it (for Gnome, check this). – WhiteWinterWolf Oct 26 '17 at 10:35
  • Hi, I see similar files under /usr/lib/kbd/keymaps/xkb/, and /lib/kbd/keymaps/xkb/ as well. Any difference? – WesternGun Nov 14 '17 at 17:03
  • Sorry but at last I didn't get it to work. I load the keymap with loadkeys <name>, and it is loaded, and when I run localectl, I see VC is using my keymap, but when I press the keys, they are in US-keyboard position. – WesternGun Nov 16 '17 at 12:04
  • At last I see this answer the only valid way... it seems that I have to edit every line of my xxx.map file to meet my needs, substitute every keycode to what I want (in one line there are more than 30 repeative key codes). – WesternGun Jan 15 '18 at 17:02
1

Just to add some detail of the accepted answer, here is what I see and what I did to make it work; will add more tricks and twists if I encounter them.

Using sudo dumpkeys will give you output like this:

keycode  12 = minus
    shift   keycode  12 = underscore
    shift   altgr   keycode  12 = underscore
    control keycode  12 = Control_underscore
    shift   control keycode  12 = Control_underscore
    altgr   control keycode  12 = Control_underscore
    shift   altgr   control keycode  12 = Control_underscore
    alt     keycode  12 = Meta_minus
    ...

And, if you open the keymap files under /lib/kbd/keymaps/xkb/, you can see that in a line there is several (sometimes too many) columns like:

keycode 12 = U+002d U+005f U+002d U+005f Control_underscore Control_underscore Control_underscore ...

According to the man page of keymaps: (man 5 keymaps), we have:

Which of the actions bound to a given key is taken when it is pressed depends on what modifiers are in effect at that moment. The keyboard driver supports 9 modifiers. These modifiers are labeled (completely arbitrarily) Shift, AltGr, Control, Alt, ShiftL, ShiftR, CtrlL, CtrlR and CapsShift. Each of these modifiers has an associated weight of power of two according to the following table:

modifier   weight
Shift           1
AltGr           2
Control         4
Alt             8
ShiftL         16
ShiftR         32
CtrlL          64
CtrlR         128
CapsShift     256

The effective action of a key is found out by adding up the weights of all the modifiers in effect. By default, no modifiers are in effect, so action number zero, i.e. the one in the first column in a key definition line, is taken when the key is pressed or released. When e.g. Shift and Alt modifiers are in effect, action number nine (from the 10th column) is the effective one.

Changing the state of what modifiers are in effect can be achieved by binding appropriate key actions to desired keys. For example, binding the symbol Shift to a key sets the Shift modifier in effect when that key is pressed and cancels the effect of that modifier when the key is released. Binding AltGr_Lock to a key sets AltGr in effect when the key is pressed and cancels the effect when the key is pressed again. (By default Shift, AltGr, Control and Alt are bound to the keys that bear a similar label; AltGr may denote the right Alt key.)

And we know U+002d is Hyphen-Minus, and U+005f is low-line, so we now see it clear: every line in keymap file, is the keycode and their keysys chars output when the key itself, shift+key, shift+altgr+key, ... is pressed, in that order.

(In case you forget the table, we can use dumpkeys --long-info to show it).

They corresponds with each other: the lines in the first part corresponds with columns in the second part.

Better to view it in a table:

+----------+------------------------+----------------------------+
|          |                        |                            |
|  col.    | key to press(+ keycode)| chars to produce (keysys)  |
|          |                        |                            |
+----------------------------------------------------------------+
|          |                        |                            |
|   0      | keycode 12             | minus                      |
|          |                        |                            |
+----------------------------------------------------------------+
|          |                        |                            |
|   1      | shift                  | underscore                 |
|          |                        |                            |
+----------------------------------------------------------------+
|          |                        |                            |
|   2      | altgr                  | underscore                 |
|          |                        |                            |
+----------------------------------------------------------------+
|          |                        |                            |
|   3(1+2) | shift + altgr          | underscore                 |
|          |                        |                            |
+----------------------------------------------------------------+
|          |                        |                            |
|   4      | ctrl                   | ctrl + underscore          |
|          |                        |                            |
+----------------------------------------------------------------+
|          |                        |                            |
|   5(4+1) | ctrl + shift           | ctrl + underscore          |
|          |                        |                            |
+----------+------------------------+----------------------------+
|          |                        |                            |
|   6(4+2) | ctrl + alt             | ctrl + underscore          |
|          |                        |                            |
+----------+------------------------+----------------------------+