Is there a way to add a key shortcut using D-Bus?

Hi everybody! I’m developing a Java App , that will need create a global key shortcut in KDE in the first run, in a automatic way but I’m not pretty sure how start to do this. I’m wondering if there is a way to do it using D-Bus…Is this possible?

Thanks!

Assuming that by “create a global key shortcut” you mean “add a shortcut to the System Settings Keyboard Shortcuts page”, you don’t need to use D-Bus.

Just expose the function you want under a flag, have a desktop entry file (com.yourname.yourappname.desktop) that has an additional application action, then use X-KDE-Shortcuts=keycombination.

See for example Spectacle’s Capture Active Window which runs spectacle -a under the hood and is triggered with Meta + Print: desktop/org.kde.spectacle.desktop.cmake · master · Graphics / Spectacle · GitLab (ignore ${KDE_INSTALL_FULL_BINDIR}, that’s just the full path of the bin directory on the system, like /usr/bin for example)

In other words, you only need four lines:

[Desktop Action PingAction]
Name=Ping!
Exec=/usr/bin/myjavaapp --ping
X-KDE-Shortcuts=Ctrl+Alt+P
2 Likes

Thanks Herzenschein! I appreciate the response!

1 Like

Didn´t work :sweat_smile:

I will comment what I done , but without positive results

  • I created a file (com.turing.P8582.desktop) in /usr/share/applications (the same place where is placed org.kde.spectacle.desktop)
  • That file has this format
[Desktop Entry]
Name=P8582
Comment=Translator
Exec=java -jar <Path to jar file>
Type=Application

[Desktop Action P8582Action]
Name=P8582Action
Exec=java -jar <Path to jar file>
X-KDE-Shortcuts=Ctrl+Alt+P
  • I logged out from my session and I logged in and I pressed Ctrl+Alt+P (I tried lowercase and uppercase p) and nothing happened…

  • I was checking the Spectacle code (a program that I really love it) and I checked as you posted here that .desktop file exists and it is in my system , with key shortcuts , but if I change that shortcuts in /usr/share/applications/org.kde.spectacle.desktop manually that doesn´t have impact :pensive:

I know there is two file khotkeysrc & kglobalshortcutsrc that I believe has impact in the system , but also I suspect that change those files I need to do it from another program

Maybe I need read further documentation.

Thanks!

1 Like

Right, I was missing something.

You need to send a copy of the desktop file to /usr/share/kglobalaccel too.

See Reworking how shortcuts are registered and saved (#3) · Issues · Frameworks / KGlobalAccel · GitLab

I just tested it here with a QML + Rust app and it worked, but it required running kbuildsycoca6 so that it would show up in System Settings without needing a reboot.

1 Like