3

I'm using Ubuntu for several years now and enjoy it immensly. I'm developing software in PHP and Javascript for a living.

The only minor annoyance I have is that I would like the PC to switch back for me to English as the main keyboard language since I sometimes forget it in Hebrew and then when I return to type some shell command or just unlock it I notice it's in the other language after I press Enter.

You might dismiss this as a really edge case but I'm curious if there's a script I can write to set a timer which after a certain amount of keyboard inactivity (I might be reading some ebook and use the mouse) switches back to my default language which is English on the keyboard settings.

A bonus would be if I could exclude arrow keys and page-down and similar keys from the script and let the timer keep on ticking if those keys were pressed.

1
  • That is very well possible, at least the "basic" option, not sure about the extra option have to give it a thought. What is the output of gsettings get org.gnome.desktop.input-sources sources? Mar 12, 2015 at 7:32

1 Answer 1

2

That is very well possible, with the help of a background script, using xprintidle, which returns the current idle time.

You will need to install it first:

sudo apt-get install xprintidle

The script below reads the output of the command xprintidle and sets your keyboard whenever the set time is passed.

How to get the keyboard to be set (index)

To create the correct command, the script needs to know the index of the targeted keyboard. To get that, run the command:

gsettings get org.gnome.desktop.input-sources sources

The output will be something like:

[('xkb', 'us+intl'), ('xkb', 'us'), ('xkb', 'nl')]

Now if you want to set the last one after x time, its index is 2, since the first item in a list is always 0.

How to use the script

  1. As mentioned, first install xprintidle
  2. Copy the script below into an empty file
  3. In the head section of the script, set:
    • the index of the targeted keyboard language, as explained above
    • the idle time after which you will change the keyboard language (in seconds)
  4. Save the script as change_kb.py
  5. Test-run it from a terminal by the command:

    python3 /path/to/change_kb.py
    
  6. If all works as expected, add it to your startup applications: Dash > Startup Applications > Add

The script

#!/usr/bin/env python3
import subprocess
import time

#--- 
kb_id = 2       # set the targeted keyboard index (first = 0)
set_time = 300  # idle tim in seconds
#---

get_t = lambda cmd: subprocess.check_output(cmd).decode("utf-8").strip()

t1 = int(get_t(["xprintidle"]))
idle_t = set_time*1000

while True:
    time.sleep(1)
    t2 = int(get_t(["xprintidle"]))
    if all([t1<idle_t, t2>idle_t]):
        cmd = "gsettings set org.gnome.desktop.input-sources current "+str(kb_id)
        subprocess.Popen(["/bin/bash", "-c", cmd])
    else:
        pass
    t1 = t2
3
  • You're welcome! I'll give the -keys-to-ignore- a shot soon. It is possible anyway, but I am looking for a more elegant way than I have in mind now. Mar 12, 2015 at 15:32
  • This is elegant! The only thing I could come up with was a script that runs just before the screen-saver gets activated before I noticed there was already a more elegant solution! :P ;-)
    – Fabby
    Mar 16, 2015 at 10:07
  • 1
    @Fabby Senks! I think xprintidle is relatively unknown, but an easy solution for a variety of tasks. Mar 16, 2015 at 10:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .