- Arch Linux
- #1 2008-04-04 20:58:12
- Detecting current Xorg keyboard layout
- #2 2008-04-05 07:43:15
- Re: Detecting current Xorg keyboard layout
- #3 2008-04-05 20:46:43
- Re: Detecting current Xorg keyboard layout
- #4 2008-04-06 06:56:04
- Re: Detecting current Xorg keyboard layout
- #5 2008-04-06 07:17:38
- Re: Detecting current Xorg keyboard layout
- #6 2008-04-06 08:41:42
- Re: Detecting current Xorg keyboard layout
- How to get current keyboard layout from the command line?
- 2 Answers 2
- How do I change keyboards from the command line?
- 9 Answers 9
- Ubuntu 13.10 or Later
- Caveat
- Invoke at screen lock
- Xorg/Keyboard configuration
- Contents
- Viewing keyboard settings
- Third party utilities
- Setting keyboard layout
- Using setxkbmap
- Using X configuration files
- Using localectl
- Frequently used XKB options
- Switching between keyboard layouts
- Terminating Xorg with Ctrl+Alt+Backspace
- Swapping Caps Lock with Left Control
- Enabling mouse keys
- Configuring compose key
- Key combinations
- Currency sign on other key
- Switching state immediately when Caps Lock is pressed
- Workaround
- One-click key functions
- Adjusting typematic delay and rate
- Using xset
- Using XServer startup options
Arch Linux
You are not logged in.
#1 2008-04-04 20:58:12
Detecting current Xorg keyboard layout
I’m trying to figure out how to programmatically detect what is the current X keyboard layout. I use us, latam, and br and it’s a pain to never be sure what the active layout is without hitting a key. What I want to do is to be able to make a keyboard layout plugin for ruby-wmii that will show me the current layout and possibly let me change it. Though the target language is eventually Ruby, using C will work too, since I could just make a Ruby extension if necessary. I’m currently looking at some code to see if I can figure out how to read the current layout and then possibly change it, but it’d be great if someone could point out the correct way.
Thanks and regards,
j
#2 2008-04-05 07:43:15
Re: Detecting current Xorg keyboard layout
I was wondering about the same thing. I use 4 keyboard layouts, never sure which one I’m using at the moment.
#3 2008-04-05 20:46:43
Re: Detecting current Xorg keyboard layout
Well, I think I’ve got it figured out. I studied up on some other programs and created a program that will now let me retrieve the current layout. There are three things that are missing:
1. A way to get the layout abbreviation. My program will return the full name of the group. In my case I get «USA», «Latin American» or «Brazil» when something like «us», «latam» or «br» would be better for a status bar indicator. I am working on this right now.
2. A way to set the layout. This way I can use a ruby-wmii bar applet to also change the group by clicking with the mouse or rolling the wheel. It would be nice to have it integrated into my code, but I may just end up scripting something with setxkbmap.
3. Error handling — the program is more proof of concept right now. I will clean it up to do at least some error handling.
What window manager do you use? If you use ruby-wmii I’d be glad to give you the bar applet once its done. If you’re using something else I could give you some code that would dump the current layout name to stdout.
#4 2008-04-06 06:56:04
Re: Detecting current Xorg keyboard layout
Would the output of ‘setxkbmap -print’ be any use?
Maybe something like:
setxkbmap -print | grep xkb_symbols | sed ‘s|.*+\([^»]\+\).*|\1|’
You can send this sort of thing back to setxkbmap using the -layout option to set the map, e.g.:
setxkbmap -layout ‘de(nodeadkeys)’
#5 2008-04-06 07:17:38
Re: Detecting current Xorg keyboard layout
The first file seems more interesting. The 2nd one is mostly GTK2 GUI stuff.
#6 2008-04-06 08:41:42
Re: Detecting current Xorg keyboard layout
Would the output of ‘setxkbmap -print’ be any use?
Maybe something like:
setxkbmap -print | grep xkb_symbols | sed ‘s|.*+\([^»]\+\).*|\1|’
I looked at setxkbmap, but it didn’t pan out. Here’s the xkb_symbols line from the output of setxkbmap -print for me:
When I ran your command, I got «group(shifts_toggle).» Note the xkb_symbols line contains all three of the layouts I am using. This presents two problems. First, I did some experiments and there is no way to know which is the active layout. Second, I can get the layout abbreviations (maybe) but it’s not clear that I could reliably associate the abbreviations with a layout, given the other noise in the line. Thanks for the suggestion, though.
Xfce’s xkb plugin knows all about short country codes and switching layouts. It might be worth a browse.
http://svn.xfce.org/index.cgi/xfce-good … ugin/xkb.c
Thanks for the suggestion, I was looking at some of KDE’s stuff and that’s how I’ve gotten as far as I have, but I couldn’t find where it set the layout (I believe it is managing the layouts via Qt rather than directly through libX11.) Perhaps the XFCE stuff will be a bit simpler.
Источник
How to get current keyboard layout from the command line?
How to get current keyboard layout in command line on all distributions ? Is it possible ?
I.e. xset -q | grep LED| awk ‘< print $10 >‘ shows different output with different keyboard layouts for me, but I’ve asked other people and I’ve noticed that for them it shows the same output even if their layout was changed. Is it possible, or did they do something wrong? Is there a clear way to get the current keyboard layout ?
I’ve started topic here, but it’s clear that the solution depends on the distribution. There you can see that I’ve tried a lot of variants of getting this, but only xset worked.
By the way what command for each distribution ? I.e. for script which will execute command to get current keyboard layout depending on distribution. I.e. which command will output CURRENT toggled layout for Fedora, which one for Ubuntu, KDE Neon, Arch, Gentoo etc.
2 Answers 2
The reason you didn’t find a solution is that there isn’t one. There is no such thing as “the current keyboard layout”, if by this you mean something like “US QWERTY layout”. The keyboard layout doesn’t have to be any particular predefined layout. The only way to describe it unambiguously is to dump the whole set of mappings (which you can do with xkbcomp :0 — ).
You can find out which layout is configured in this or that configuration interface. All that will tell you is which layout is configured in that particular interface. It won’t tell you whether this layout is current. The user might have used a different interface to change the layout, or might have set a completely custom keymap with a tool like xmodmap or xkbcomp .
The output you get from setxkbmap -query is as close as possible. It gets information from the X server, so you don’t run into the problem of configuration files that may or may not have been applied. However, there are two ways to tell the X server to change the keyboard layout: by giving a layout name (and options), or by setting individual keys. If setxkbmap -query says us , it means that the last time somebody successfully asked the X server to load a specific layout, the request was to load the us layout. But if there was a subsequent request to change the configuration of individual keys, the layout may now be completely different.
Nobody is doing anything wrong. It’s just that there’s no concept of “current layout” in the system in general, because the keyboard layout doesn’t have to be a predefined layout.
Источник
How do I change keyboards from the command line?
How I change keyboard layouts from the command-line? For example, I want to switch from English to Hebrew. I’m able to do it from lightdm with the little icon in the corner of my screen.
9 Answers 9
You can find all the different keymaps in the following locations:
To change the keyboard layout (e.g. to Spanish) in the Linux command line, type the following command:
To make these changes system wide, assuming you’re using Ubuntu, you can use the following:
English to Hebrew and vise versa with Alt + Shift
You can see all locale alias with this command
More info about setxkbmap in manual
Ubuntu 13.10 or Later
This works better then setxkbmap with Gnome/Unity keyboard layout indicator.
0 is the layout index (0 default or top layout). Layouts indexed starting from 0.
For easy use, create an alias.
this command enable you toggle between English and Hebrew only through by right alt + shift and rescue you from left alt + shift .
There seems to be no standard CLI tool, but xkb-switch is a small open-source tool to do the job. Just tested it with Ubuntu 16.04.4 LTS and it works flawelessly.
This looks better than the other solutions because setxkbmap ruins all options, and setxkbmap -layout , while it preserves the options, it still fails to change the taskbar indicator nor the grp_led if set.
Caveat
/.config/kxkbrc uses LayoutLoopCount (e.g. it is set to less than the total number of layouts listed in LayoutList ), like this:
then xkb-switch would only let you switch between the layouts in current group.
Invoke at screen lock
I noticed your comment:
Right before my screensaver turns on, I want to switch to English so that it will be easy to type my password when I get back in.
This was precisely my goal, so here’s the answer:
To do that, you only need wrap it into a script hooking to dbus-monitor like discussed here.
Using gsettings in 18.04+ with Gnome, you can both add add keyboard layouts and switch between them from the command line or from the keyboard layout switcher on the desktop.
1.Present keyboard layouts and options:
2.Adding keyboard layouts:
3.Changing the current keyboard layout from the command line:
As we now have 3 different languages, we have three choices from 0 to 2, with 1 being Spanish, 2 being Norwegian and 3 being Greek. Thus, setting current to 1 above makes Norwegian the current keyboard layout.
4.Listing all possible keyboard layouts:
5.Changing the keyboard layout with a keyboard shortcut:
A keyboard shortcut is preconfigured for changing the keyboard layout. The present shortcut:
A new shortcut can be set by using gsetting set, but take care not to use a shortcut that is already in use. To list all shortcuts in use:
For Ubuntu 18.04 + Gnome the other answers did not work for me, but this did:
The argument in «inputSources[0]» is the index of the keyboard layout, the same as for gsettings solution.
I was able to find the best and most stable combination of xkb-switch , setxkbmap and ibus . IBus allows pinyin input and xkb-switch is stable, while setxkbmap returns back set of keyboard layouts.
This is important when switching directly to a language, for example Shift + Alt + 1 . 4 . Why not using only IBus? It gets stuck all the time because it requires a restart to bring keyboard layouts back to life after switching to non-latin keyboard layout. At least in FireFox, see I need to exit and restart ibus-daemon to make ibus-chewing work. #2319.
Disclaimer: Tested on OpenSuse Leap 15.3 xfce4 and xkb-switch is compiled from source
To make this work you should make script files with those commands and assign keyboard shortcuts to run them. Also you might want to disable Alt+Shift shortcut in keyboard switch settings to remove unexpected behavior.
So, here are my set of commands:
Shift + Alt + 1 — switch to English:
Shift + Alt + 2 — switch to Russian:
Shift + Alt + 3 — switch to Chinese Mandarin Pinyin:
Shift + Alt + 4 — switch to Portugese:
Also assign the following script to run on system autostart.
Источник
Xorg/Keyboard configuration
This article describes the basics of Xorg keyboard configuration. For advanced topics such as keyboard layout modification or additional key mappings, see X keyboard extension or Extra keyboard keys respectively.
The Xorg server uses the X keyboard extension (XKB) to define keyboard layouts. Optionally, xmodmap can be used to access the internal keymap table directly, although this is not recommended for complex tasks. Also systemd’s localectl can be used to define the keyboard layout for both the Xorg server and the virtual console.
Contents
Viewing keyboard settings
You can use the following command to see the actual XKB settings:
Third party utilities
There are some «unofficial» utilities which allow to print specific information about the currently used keyboard layout.
Setting keyboard layout
Keyboard layout in Xorg can be set in multiple ways. Here is an explanation of used options:
- XkbModel selects the keyboard model. This has an influence only for some extra keys your keyboard might have. The safe fallback are pc104 or pc105 . But for instance laptops usually have some extra keys, and sometimes you can make them work by simply setting a proper model.
- XkbLayout selects the keyboard layout. Multiple layouts may be specified in a comma-separated list, e.g. if you want to quickly switch between layouts.
- XkbVariant selects a specific layout variant available for the XkbLayout . For instance, the default sk (Slovak) variant is qwertz , but you can manually specify qwerty , etc.
- XkbOptions contains some extra options (comma-separated). Used for specifying layout switching, notification LED, compose mode etc. See the #Frequently used XKB options section for examples.
The layout name is usually a 2-letter country code. To see a full list of keyboard models, layouts, variants and options, along with a short description, open /usr/share/X11/xkb/rules/base.lst . Alternatively, you may use one of the following commands to see a list without a description:
- localectl list-x11-keymap-models
- localectl list-x11-keymap-layouts
- localectl list-x11-keymap-variants [layout]
- localectl list-x11-keymap-options
Examples in the following subsections will have the same effect, they will set pc104 model, cz as primary layout, us as secondary layout, dvorak variant for us layout and the Win+Space combination for switching between layouts. See xkeyboard-config(7) for more detailed information.
Using setxkbmap
This article or section needs expansion.
setxkbmap sets the keyboard layout for the current X session only, but can be made persistent in xinitrc or xprofile. This overrides system-wide configuration specified following #Using X configuration files. However, persistence does not apply to keyboards such as external keyboards, unless their device id was specified when setxkbmap was run. Or that they are the core keyboard device of the X server.
The usage is as follows (see setxkbmap(1) ):
To change just the layout ( -layout is the default flag):
For multiple customizations:
Using X configuration files
The syntax of X configuration files is explained in Xorg#Configuration. This method creates system-wide configuration which is persistent across reboots.
Here is an example:
Using localectl
For convenience, the tool localectl may be used instead of manually editing X configuration files. It will save the configuration in /etc/X11/xorg.conf.d/00-keyboard.conf , this file should not be manually edited, because localectl will overwrite the changes on next start.
The usage is as follows:
To set a model, variant or options, all preceding fields need to be specified, but the preceding fields can be skipped by passing an empty string with «» . Unless the —no-convert option is passed, the specified keymap is also converted to the closest matching console keymap and applied to the console configuration in vconsole.conf . See localectl(1) for more information.
To create a /etc/X11/xorg.conf.d/00-keyboard.conf like the above:
Frequently used XKB options
Switching between keyboard layouts
To be able to easily switch keyboard layouts, first specify multiple layouts between which you want to switch (the first one is the default). Then specify a key (or key combination), which will be used for switching. For example, to switch between a US and a Swedish layout using the CapsLock key, use us,se as an argument of XkbLayout and grp:caps_toggle as an argument of XkbOptions . The number of XkbLayout s should match that of the XkbVariant s — if you want to switch solely between different variants, then duplicate the layout accordingly (e.g. de,de ).
You can use other key combinations than CapsLock , they are listed in /usr/share/X11/xkb/rules/base.lst , start with grp: and end with toggle . To get the full list of available options, run the following command:
Note that the grp:alts_toggle option is unreliable and unlikely to be fixed; prefer other combinations!
Terminating Xorg with Ctrl+Alt+Backspace
By default, the key combination Ctrl+Alt+Backspace is disabled. You can enable it by passing terminate:ctrl_alt_bksp to XkbOptions . This can also be done by binding a key to Terminate_Server in xmodmap (which undoes any existing XkbOptions setting). In order for either method to work, one also needs to have DontZap set to «off» in ServerFlags ; however, from at least version R6.8.0 (year 2004) [1] this is the default.
Swapping Caps Lock with Left Control
To swap Caps Lock with Left Control key, add ctrl:swapcaps to XkbOptions . Run the following command to see similar options along with their descriptions:
Enabling mouse keys
Mouse keys, not to be confused with the keys of the mouse, is disabled by default and has to be manually enabled by passing keypad:pointerkeys to XkbOptions . This will make the Shift+NumLock shortcut toggle mouse keys.
See also X keyboard extension#Mouse control for advanced configuration.
Configuring compose key
Though typically not on traditional keyboards, a Compose key can be configured to an existent key.
The Compose key begins a keypress sequence that involves (usually two) additional keypresses. Usage is typically either for entering characters in a language that the keyboard was not designed for, or for other less-used characters that are not covered with the AltGr modifier. For example, pressing Compose ‘ e produces é , or Compose — — — will produce an «em dash»: — .
Though a few more eccentric keyboards feature a Compose key, its availability is usually through substituting an already existing key to it. For example, to make the Menu key a Compose key use the Desktop environment configuration, or pass compose:menu to XkbOptions (or setxkbmap: setxkbmap -option compose:menu ). Allowed key substitutions are defined in /usr/share/X11/xkb/rules/base.lst :
If the desired mapping is not found in that file, an alternative is to use xmodmap to map the desired key to the Multi_key keysym, which acts as a compose key by default (note that xmodmap settings are reset by setxkbmap).
Key combinations
The default combinations for the compose keys depend on the locale configured for the session and are stored in /usr/share/X11/locale/used_locale/Compose , where used_locale is for example en_US.UTF-8 .
You can define your own compose key combinations by copying the default file to
/.XCompose and editing it. Alternatively, create an empty
/.XCompose and include the default one using include «%L» , for example:
The compose key (denoted as in the
/.XCompose file) works with any of the thousands of valid Unicode characters, including those outside the Basic Multilingual Plane. Take a look at the Compose(5) man page, it explains the format of the XCompose files.
However, GTK does not use XIM by default and therefore does not follow
/.XCompose keys. This can be fixed by forcing GTK to use XIM by adding export GTK_IM_MODULE=xim and/or export XMODIFIERS=»@im=none» to
Currency sign on other key
Most European keyboards have a Euro sign (€) printed on on the 5 key. For example, to access it with Alt+5 , use the lv3:lalt_switch and eurosign:5 options.
The Rupee sign (₹) can be used the same way with rupeesign:4 .
Switching state immediately when Caps Lock is pressed
Those who prefer typing capital letters with the Caps Lock key may experience a short delay when Caps Lock state is switched, resulting in two or more capital letters (e.g. THe, ARch LInux). This occurs because Caps Lock is enabled immediately once the Caps Lock key is pressed, but is only disabled upon release of the second key-press. This behaviour stems from typewriters where a Caps Lock function was achieved by physically locking the shifted typebars in place, and the release of a shift key-press was the action that caused the release of the lock.
Some more popular operating systems have removed this behaviour, either voluntarily (as it can be confusing to some) or by mistake, however this is a question of preference. Bug reports have been filed on the Xserver bug tracker, as there is currently no easy way to switch to the behaviour reflected by those other operating systems. For anyone who would like to follow up the issue, bug reports and latest working progress can be found at [2] and [3].
Workaround
First, export your keyboard configurations to a file:
In the file xkbmap, locate the Caps Lock section which begins with key :
and replace whole section with the following code:
Save and reload keyboard configurations:
Consider making it a service launching after X starts, since reloaded configurations do not survive a system reboot.
One-click key functions
To assign an additional one-click function to a modifier key, you can use xcape . For example it is possible to have CapsLock work as Escape when pressed alone, and as Control when used with another key. First set the Control swapping using setxkbmap as mentioned earlier, and xcape to set the Escape association:
You can set multiple associations separated with a semicolon, e.g.: Caps_Lock=Escape;Shift_L=Escape .
If you hold a key for longer than the timeout value (default 500 ms), xcape will not generate a key event.
Adjusting typematic delay and rate
The typematic delay indicates the amount of time (typically in milliseconds) a key needs to be pressed and held in order for the repeating process to begin. After the repeating process has been triggered, the character will be repeated with a certain frequency (usually given in Hz) specified by the typematic rate. Note that these settings are configured separately for Xorg and for the virtual console.
Using xset
The tool xset can be used to set the typematic delay and rate for an active X server, certain actions during runtime though may cause the X server to reset these changes and revert instead to its seat defaults.
For example to set a typematic delay to 200ms and a typematic rate to 30Hz, use the following command (use xinitrc to make it permanent):
Issuing the command without specifying the delay and rate will reset the typematic values to their respective defaults; a delay of 660ms and a rate of 25Hz:
Using XServer startup options
A more resistant way to set the typematic delay and rate is to make them the seat defaults by passing the desired settings to the X server on its startup using the following options:
- -ardelay milliseconds — sets the autorepeat delay (length of time in milliseconds that a key must be depressed before autorepeat starts).
- -arinterval milliseconds — sets the autorepeat interval (length of time in milliseconds that should elapse between autorepeat-generated keystrokes).
See Xserver(1) for a full list of X server options and refer to your display manager for information about how to pass these options.
Источник