LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 12-14-2020, 03:30 PM   #1
PaulExordium
Member
 
Registered: Dec 2020
Distribution: Debian, Tails, Kodachi, Mint DE
Posts: 92

Rep: Reputation: Disabled
Setting keyboard layouts programmatically


I have the following bash script in Debian 9/Gnome:

Code:
#!/bin/bash
gsettings set org.gnome.desktop.input-sources sources "[('"xkb"','"us"'),('"xkb"','"fr"'),('"xkb"','"it"')]"
gsettings set org.gnome.desktop.input-sources current 1
exit
1) The layouts are added as expected, but the selection for the current one has no effect. What am I missing?

2) Where can I find the abbreviations for the variants of a given language?
 
Old 12-15-2020, 12:52 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
I have no idea if these are the commands required to change keyboard layout in GNOME. Do they work when invoked 1 by 1, manually?
Can't you just use "setxkbmap" instead?
The 2-letter codes are here
 
Old 12-15-2020, 12:04 PM   #3
DavidMcCann
LQ Veteran
 
Registered: Jul 2006
Location: London
Distribution: PCLinuxOS, Debian
Posts: 6,142

Rep: Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314
You should definitely use setxkbmap. See the man page and also Arch guide. Look at the language file in /usr/share/X11/xkb/symbols/ to see the variant names, like "alternative" or "Dvorak" in "fr".
 
Old 12-15-2020, 02:12 PM   #4
PaulExordium
Member
 
Registered: Dec 2020
Distribution: Debian, Tails, Kodachi, Mint DE
Posts: 92

Original Poster
Rep: Reputation: Disabled
Programmatically executing the script, manually executing it or manually running the individual commands have all the same behavior. They all show the desired layouts but the setting of the "current" one is without effect.

Yes, I can set one layout at a time but the last set overrides the previous one, and that led me to the following workaround (eliminating some useless double quotes):

Code:
#!/bin/bash
gsettings set org.gnome.desktop.input-sources sources "[('xkb','fr')]"
gsettings set org.gnome.desktop.input-sources sources "[('xkb','us'),('xkb','fr'),('xkb','it')]"
exit
But, I still don't know what are the values to use for variants, for example "French (Canada)" as a variant of "French".

I will definitely look at "setxkbd" as suggested, it looks like "localectl" could be the easiest.
Thank you all.
 
Old 12-16-2020, 12:51 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Actually, setxkbmap is only relevant if you run GNOME under Xorg/X11.
Quote:
Originally Posted by PaulExordium View Post
But, I still don't know what are the values to use for variants, for example "French (Canada)" as a variant of "French".
Read the links we provided.

Quote:
Originally Posted by PaulExordium View Post
it looks like "localectl" could be the easiest.
That does way more than just change the keyboard layout. But it might help.
 
Old 12-16-2020, 11:46 AM   #6
DavidMcCann
LQ Veteran
 
Registered: Jul 2006
Location: London
Distribution: PCLinuxOS, Debian
Posts: 6,142

Rep: Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314Reputation: 2314
Quote:
Originally Posted by ondoho View Post
Actually, setxkbmap is only relevant if you run GNOME under Xorg/X11.
What are you talking about? It works with any X setup — I certainly don't have Gnome — and in fact the OP does.
 
Old 12-16-2020, 01:11 PM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by DavidMcCann View Post
What are you talking about? It works with any X setup — I certainly don't have Gnome — and in fact the OP does.
GNOME is a given in this thread.
What I meant is that setxkbmap only works under X11/Xorg.
That's what I'm talking about; but I see how the phrasing could be ambiguous if one didn't read everything carefully.
 
Old 12-17-2020, 09:34 AM   #8
PaulExordium
Member
 
Registered: Dec 2020
Distribution: Debian, Tails, Kodachi, Mint DE
Posts: 92

Original Poster
Rep: Reputation: Disabled
On a Debian 10, Gnome, Kernel 5.9 I've tried this:
Code:
#!/bin/bash
#On Debian 10 / Gnome / Kernel 5.9

setxkbmap -layout fr
setxkbmap -layout ca -variant fr
setxkbmap -layout ch -variant fr
setxkbmap -layout us,fr,it
setxkbmap -layout de
setxkbmap -layout gb
#no layout change visible in "Region & Language Settings" on the desktop!

#changes by last "setxkbmap" visible here:
setxkbmap -print -verbose 10

exit
No success. What can I do to pinpoint the cause?
 
Old 12-17-2020, 12:05 PM   #9
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
You didn't show us the output!
Also, run every command verbose.

I'm not a fan of GNOME and don't know much about its inner workings. It's quite possible that as a desktop environment it thinks it should override these setting somehow. :shrugs:

Also you didn't clarify whether you run under X or wayland. If the latter, setxkbmap will be useless, I think.
 
Old 12-17-2020, 12:49 PM   #10
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Well, this is what I get on GNOME 2.32 (I highlighted red the relevant part):
Code:
$ gsettings describe org.gnome.desktop.input-sources current
The zero-based index into the input sources list specifying the current one in effect. The value is automatically capped to remain in the range [0, sources_length) as long as the sources list isn’t empty.

DEPRECATED: This key is deprecated and ignored.

Last edited by shruggy; 12-17-2020 at 12:57 PM.
 
Old 12-18-2020, 04:33 AM   #11
PaulExordium
Member
 
Registered: Dec 2020
Distribution: Debian, Tails, Kodachi, Mint DE
Posts: 92

Original Poster
Rep: Reputation: Disabled
ondoho,
sorry for the lapse. Here is the ouput with a reduced number of commands typed individually:
Code:
$ setxkbmap -layout ch -variant fr -verbose
Warning! Multiple definitions of keyboard layout
         Using command line, ignoring X server
Trying to build keymap using the following components:
keycodes:   evdev+aliases(qwerty)
types:      complete
compat:     complete
symbols:    pc+ch(fr)+inet(evdev)
geometry:   pc(pc105)

$ setxkbmap -layout us,fr,it -verbose
Warning! Multiple definitions of keyboard layout
         Using command line, ignoring X server
Trying to build keymap using the following components:
keycodes:   evdev+aliases(qwerty)
types:      complete
compat:     complete
symbols:    pc+us+fr:2+it:3+inet(evdev)
geometry:   pc(pc105)

$ setxkbmap -layout gb -verbose 10
Setting verbose level to 10
locale is C
Warning! Multiple definitions of keyboard layout
         Using command line, ignoring X server
Trying to load rules file ./rules/evdev...
Trying to load rules file /usr/share/X11/xkb/rules/evdev...
Success.
Applied rules from evdev:
rules:      evdev
model:      pc105
layout:     gb
Trying to build keymap using the following components:
keycodes:   evdev+aliases(qwerty)
types:      complete
compat:     complete
symbols:    pc+gb+inet(evdev)
geometry:   pc(pc105)

$ setxkbmap -print -verbose 10
Setting verbose level to 10
locale is C
Trying to load rules file ./rules/evdev...
Trying to load rules file /usr/share/X11/xkb/rules/evdev...
Success.
Applied rules from evdev:
rules:      evdev
model:      pc105
layout:     gb
Trying to build keymap using the following components:
keycodes:   evdev+aliases(qwerty)
types:      complete
compat:     complete
symbols:    pc+gb+inet(evdev)
geometry:   pc(pc105)
xkb_keymap {
	xkb_keycodes  { include "evdev+aliases(qwerty)"	};
	xkb_types     { include "complete"	};
	xkb_compat    { include "complete"	};
	xkb_symbols   { include "pc+gb+inet(evdev)"	};
	xkb_geometry  { include "pc(pc105)"	};
};

echo $XDG_SESSION_TYPE
wayland
That seems to confirm your thought about the uselessness of "setxkbmap" under wayland.
 
Old 12-18-2020, 04:35 AM   #12
PaulExordium
Member
 
Registered: Dec 2020
Distribution: Debian, Tails, Kodachi, Mint DE
Posts: 92

Original Poster
Rep: Reputation: Disabled
hruggy,
with the command you suggested on GNOME 3.30.2 I get the exact same message. That answers the first question in my original post.

I've found the answer to my second question with a list of layouts and this command explaining the proper format when adding a variant:
Code:
$ gsettings describe org.gnome.desktop.input-sources sources
List of input source identifiers available. Each source is specified as a tuple of 2 strings. The first string is the type and can be one of “xkb” or “ibus”. For “xkb” sources the second string is “xkb_layout+xkb_variant” or just “xkb_layout” if a XKB variant isn’t needed. For “ibus” sources the second string is the IBus engine name. An empty list means that the X server’s current XKB layout and variant won’t be touched and IBus won’t be used.
Here is an example of a functional script, assuming the following:
us layout
ch layout, fr variant
it layout
in that order on the desktop with the second in the list set as the current layout.
Code:
#!/bin/bash
gsettings set org.gnome.desktop.input-sources sources "[('xkb','ch+fr')]"
gsettings set org.gnome.desktop.input-sources sources "[('xkb','us'),('xkb','ch+fr'),('xkb','it')]"
exit
 
Old 03-01-2024, 03:02 PM   #13
mad.head
LQ Newbie
 
Registered: Mar 2024
Posts: 1

Rep: Reputation: 0
I didn't find any well-working solutions in 2024, so I've made one. Search for the "Shyriiwook" extension in the GNOME Shell Extensions store. The extension uses getInputSourceManager and exposes it as a D-Bus interface without relying on org.gnome.Shell.Eval. It doesn't have any GUI. After installing it, a new D-Bus interface would be exposed in your GNOME Shell session. You could query it for the current configuration or call a method to activate the desired layout and that's it:

Code:
$ gdbus introspect \
    --session \
    --dest org.gnome.Shell \
    --object-path /me/madhead/Shyriiwook \
    --only-properties

node /me/madhead/Shyriiwook {
  interface me.madhead.Shyriiwook {
    properties:
      readonly as availableLayouts = ['us', 'de', 'jp'];
      readonly s currentLayout = 'us';
  };
};

$ gdbus call \
    --session \
    --dest org.gnome.Shell \
    --object-path /me/madhead/Shyriiwook \
    --method me.madhead.Shyriiwook.activate "de"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
programmatically setting the default player creativemadhan Linux - General 1 01-16-2007 06:43 AM
Switching between keyboard layouts ??? GloVe Linux - General 1 10-13-2003 10:26 AM
Modifier keys + non-latin keyboard layouts angryziber Linux - Hardware 0 10-08-2003 02:35 AM
Una Pregunta Keyboard Layouts Español jimdaworm Linux - Hardware 0 08-24-2003 09:06 AM
switching keyboard layouts linuxfond Linux - Newbie 1 05-28-2003 10:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 12:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration