[HowTo] Use ModifierOnlyShortcuts on KDE

On KDE Plasma, you can use modifier keys (Meta, Alt, Shift, Control) to run a specific command when it used alone. For example, you can use Meta key to activate Krunner instead of the application menu. Or you can activate "present windows" kwin effect with the Alt key etc.

For this, you need to add the commands to ~/.config/kwinrc file as following scheme:

[ModifierOnlyShortuts]
Meta=
Shift=
Alt=
Control=

But in order to work, the command must be a dbus command. There is already dbus commands for some native KDE apps and you can use other commands with calling different key with dbus.

How to find Dbus commands?

You can use qdbusviewer to browse commands graphically or you can type qdbus in terminal and use tab completion to browse through commands. I prefer second way.

So, if I want to activate Krunner with Meta key, my kwinrc file should contain:

[ModifierOnlyShortuts]
Meta=org.kde.kglobalaccel,/component/krunner,,invokeShortcut,run command

Above command calls Krunner shortcut with dbus. Normally you can directly run krunner with following command, but this only activates krunner and you can't close with the same key:

org.kde.krunner,/App,,display

Activate present windows (Ctrl+F10 in default shortcuts) kwin effect with Alt:

Alt=org.kde.kglobalaccel,/component/kwin,,invokeShortcut,Expose

Using Custom Commands

In order to use custom command that can't be found in dbus, first you need to create cutom shortcut from System Settings. For example, I have ScreenOff shortcut running
"sleep 1 && xset -display :0.0 dpms force off" command with Ctrl+. trigger.

After setting your shortcut, open ~/.config/kglobalshortcutsrc and find the line containing your shortcut and use its hash:

{b86724c8-2c6a-432b-a93d-c43a381711f3}=Ctrl+.,none,ScreenOff

If I want to use the Ctrl key to turn screen off, my setting should be:

Control=org.kde.kglobalaccel,/component/khotkeys,,invokeShortcut,{b86724c8-2c6a-432b-a93d-c43a381711f3}

Modifying kwinrc (preferred way)

Prefer editing kwinrc from command line with kwriteconfig5 command.

kwriteconfig5 --file ~/.config/kwinrc --group ModifierOnlyShortcuts --key KEYNAME "CommandHere"

Example:

kwriteconfig5 --file ~/.config/kwinrc --group ModifierOnlyShortcuts --key Meta "org.kde.kglobalaccel,/component/krunner,,invokeShortcut,run command"

! NOTE: After modifying kwinrc file, you need to reconfigure it:

qdbus org.kde.KWin /KWin reconfigure
A few example commands
# Media control:
org.kde.kglobalaccel,/component/mediacontrol,,invokeShortcut,playpausemedia
org.kde.kglobalaccel,/component/mediacontrol,,invokeShortcut,previousmedia
org.kde.kglobalaccel,/component/mediacontrol,,invokeShortcut,nextmedia

# Change desktop:
org.kde.KWin,/KWin,,previousDesktop
org.kde.KWin,/KWin,,nextDesktop

# Desktop Grid:
org.kde.kglobalaccel,/component/kwin,,invokeShortcut,ShowDesktopGrid

# Activate Yakuake:
org.kde.yakuake,/yakuake/window,,toggleWindowState

# Lock session:
org.kde.screensaver,/ScreenSaver,,Lock

# Show System Activity:
org.kde.kglobalaccel,/component/kded5,,invokeShortcut,Show System Activity
Finding shortcut names (example)

List of Kwin shortcut names:

qdbus org.kde.kglobalaccel /component/kwin org.kde.kglobalaccel.Component.shortcutNames | sort
Default Config (Meta key to open Application Menu)
[ModifierOnlyShortcuts]
Meta=org.kde.plasmashell,/PlasmaShell,org.kde.PlasmaShell,activateLauncherMenu
Alt=
Control=
Shift=
References
8 Likes

Great post. Very informative.

1 Like

Forum kindly sponsored by