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
- As mentioned, first install
xprintidle
- Copy the script below into an empty file
- 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)
- Save the script as
change_kb.py
Test-run it from a terminal by the command:
python3 /path/to/change_kb.py
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
gsettings get org.gnome.desktop.input-sources sources
?