6

Can a command be set to run when switching to a specific virtual desktop (or an activity) in KDE environment?

I want to run a command for a desktop (e.g. desktop2) so that when I switch to that desktop, the keyboard language layout will change to English US automatically.

Zanna
  • 72,312

3 Answers3

3

You can change to virtual desktop two using the following command line argument:

wmctrl -s 2

Thus executing

wmctrl -s 2 && setxkbmap en

should switch your to desktop 2 and change your language to english as suggested in your example.

You can use a keybinding program like autokey, or KDE custom shortcuts to set a keybinding for the above command. Alternatively, you can save the above command to a text file ending in .sh, and then make the file executable to have a clickable script that will switch desktops and change languages for you.

should switch your to desktop 2 and change your language to english as suggested in your example.


Switching activities is a bit more lengthy command.

Going to go to the next activity is simple enough using:

qdbus org.kde.kglobalaccel /component/plasma_desktop invokeShortcut "Next Activity"

However to switch to a specific activity, you must fist know it's ID which can be obtained by:

qdbus org.kde.kactivitymanagerd /ActivityManager/Activities ListActivities

Once you know the activity id you can switch to that activity via:

qdbus org.kde.kactivitymanagerd /ActivityManager/Activities SetCurrentActivity activity-key-in-previous-step

then couple that with the '&& setxkbmap en' command as shown above when switching virtual desktops to have activity switching coupled with a language change.


Also of note:

export LC_ALL=C

will change the system language to english

virtualxtc
  • 3,324
2
  • Devil's Pie 2 lets you launch a script when that Window #n is created, which is not quite what you wanted, but simple;
  • you can use Autokey to run a script which switches the window then changes the language, and launch another script to revert to the original settings, all by hotkey, or
  • there's also a GUI-aware Scripting tool called Sikuli you might find interesting.
K7AAY
  • 17,705
2

Thanks to virtualxtc's answer above and don_crissti's answer. To change layout download this zip file linked in this page. Extract it to a folder in home folder. Then run terminal inside the folder and run these commands:

cmake CMakeLists.txt
make CMakeLists.txt
cmake CMakeLists.txt
sudo make install CMakeLists.txt

(you may need to install make and cmake)

then run this command to switch to English:

xkb-switch -s us

and a combination can be

wmctrl -s 1 && xkb-switch -s us

which one can assign a hotkey to it to switch to desktop2 and English.