Score:1

How to set each keyboard layout to its very own shortcut combination (imitate Windows functional)?

no flag

On Windows I always had the possibility to set each keyboard layout to some global hotkey (look at the picture). For example, due to multilanguage scope of work, I need 5 keyboard layouts - so in Windows I can just give to each one of them its own keyboard key combination like Ctrl + Shift + 2, Ctrl + Shift + 5 and so on

In Ubuntu you can change the hotkey for keyboard layout switching to almost any option you want - but only for switching from previous to next and not to direct switch on.

So if you have 3 or 5 or more layouts - you need to spare tons of time for switching processes: you need to revolve your layouts by hotkey forward or backward or use mouse and layout menu - which is very uncomfortable and need your time and attention to control what exactly keyboard layout you are having right now.

So maybe somebody know - how in Ubuntu to do the custom global hotkey for direct switch on to the very one certain keyboard layout?

I just want to have possibility to press Ctrl + Shift + 3 and be sure - now I have egnlish keyboard layout and the same behaviour with other languages/layouts.

enter image description here

vanadium avatar
cn flag
You will need to give details on your current system and desktop environment because options, if any, can depend on it.
Score:1
uz flag

It can be done with a script.

  • Save the below script as $HOME/bin/mylayouts

  • Make it executable:

    chmod +x $HOME/bin/mylayouts
    
  • Relogin

Now you can switch layout by running for instance

mylayouts us
mylayouts ro_std

And that in turn allows you to create custom shortcuts for the purpose.

This is the example script:

#!/bin/bash -e

# layouts
us="('xkb', 'us')"
ro_std="('xkb', 'ro+std')"
latam="('xkb', 'latam')"
es="('xkb', 'es')"

case $1 in
  us)     NEW=us
    ;;
  ro_std) NEW=ro_std
    ;;
  latam)  NEW=latam
    ;;
  es)     NEW=es
    ;;
  *)      echo "Invalid argument"
          exit 1
    ;;
esac

NEWLIST="[${!NEW}"
for L in 'us' 'ro_std' 'latam' 'es'; do
    if [ "$L" = "$NEW" ]; then
        continue
    else
        NEWLIST="$NEWLIST, ${!L}"
    fi
done
NEWLIST="$NEWLIST]"

gsettings set org.gnome.desktop.input-sources sources "[${!NEW}]"
gsettings set org.gnome.desktop.input-sources sources "$NEWLIST"

If you adapt the script to your needs, you can now from Ubuntu Settings create custom shortcut like:
name: us_colemak
command: mylayouts.sh us_colemak
shortcut: Ctrl + Shift + 5

Caveat:

Using numerical keys as part of the shortcut is a bit tricky. If you for instance use the English (US) layout when creating a shortcut by pressing Ctrl+Shift+2, the shortcut actually created is Ctrl + Shift + @ (since Shift+2 results in the @ character with the English (US) layout). Then, if you for instance are on a latin layout, where Shift+2 results in the " character, the shortcut Ctrl+Shift+2 won't give you the expected result.

Probably it's less error prone to use letters instead of numbers.

In any case, this answer is not a perfect solution. There is apparently room for improvement.

Green Joffer avatar
no flag
thanks, it works! p.s: I added the shortcut example in your answer, hope, you don't mind
Gunnar Hjalmarsson avatar
uz flag
@GreenJoffer: Well, your proposed edit was not consistent with some parts of the answer, so I modified it.
Green Joffer avatar
no flag
you are right, that's why when I was setting the shortcuts - I was checking before setting that current layout is **default 'us'**. I added 4 layouts, all works, but somehow, don't know why, the shortcut **Ctrl + Shift + 4** just does not work, so I used the next number (maybe bug or reserved for some system needings)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.