How to create a global hotkey?

Paul Boddie paul at boddie.org.uk
Sun Dec 10 10:02:05 EST 2006


k04jg02 at gmail.com wrote:
> Paul Boddie wrote:

[DCOP]

> This is really annoying to setup and would be a KDE specific solution.

Searching for "PyKDE global shortcut" produced something at the PyQt
level:

http://lists.kde.org/?l=pykde&m=115451566321878&w=2

I don't know whether that is useful in this context, however.

> Ideally this would work under Gnome or KDE. Applications do this all
> the time -- Amarok for example adds global shortcuts for playing,
> pausing, jumping to next track, etc. that work whether or not you're in
> KDE or Gnome.

The source code for Amarok is available, so if you're not afraid of
looking at C++ code there may be an answer somewhere in there.

> > I suppose you could just plug in at the X level and have something
> > define and intercept a hot key before the desktop environment has a
> > chance to process it, but I doubt that this would be a particularly
> > nice solution.
>
> Why not? If I had to guess at this point I'd say it's the 'right' way
> to do it. I'm going to be dealing with keypresses that no other apps or
> the desktop environments have bound to anything, so I'm not sure it
> really qualifies as 'intercepting'. If multiple apps have registered to
> receive global keypresses, and the first app that looks at a keypress
> doesn't know how to handle it, shouldn't it just get passed on to the
> next app?

I'm not that familiar with the event model involved, but one
disadvantage of handling things at the X level is that people may not
be able to inspect or override any shortcuts or hot keys that your
application defines within their desktop environment's configuration
dialogues. That's why it may be best going through the desktop
environment, even though that might mean dealing with different
mechanisms in different cases.

Unfortunately, the freedesktop.org people haven't introduced standards
for global shortcuts, as far as I can tell. Here are some documents I
found (by searching for "global shortcut freedesktop standard"):

http://www.freedesktop.org/wiki/Standards_2fdefault_2dkeys_2dspec
http://portland.freedesktop.org/wiki/IntegrationTasks

Paul




More information about the Python-list mailing list