472,805 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

How to create a global hotkey?

I want to make a Python app that runs in the background, and when a
user hits a key combination, for a function to run. This sounds simple
enough, but all of the keypress detecting libraries I can find count on
you creating a window and then detecting keypresses while that window
has focus. I want my function to execute when the user presses the
hotkey anywhere. I searched the PyGTK documentation and found an old
newsgroup post where someone mentioned the C GTK+ library has it but
PyGTK does not, PyQT showed no results, not sure where else I should
look. I'd be willing to use a library that isn't a windowing toolkit --
I just want to be able to be able to globally detect a keypress. Any
ideas?

Dec 7 '06 #1
9 18739
k0*****@gmail.com wrote:
I want to make a Python app that runs in the background, and when a
user hits a key combination, for a function to run. This sounds simple
enough, but all of the keypress detecting libraries I can find count on
you creating a window and then detecting keypresses while that window
has focus. I want my function to execute when the user presses the
hotkey anywhere. I searched the PyGTK documentation and found an old
newsgroup post where someone mentioned the C GTK+ library has it but
PyGTK does not, PyQT showed no results, not sure where else I should
look. I'd be willing to use a library that isn't a windowing toolkit --
I just want to be able to be able to globally detect a keypress. Any
ideas?
What OS on? That is the key question here (sorry for the bad pun). It
heavily depends on that, and possibly it hasn't to do with python at all,
but instead configuring e.g. your window manager.

Diez
Dec 7 '06 #2
I want my function to execute when the user presses the
hotkey anywhere.
Hi!

- XLib unter Linux: http://python-xlib.sourceforge.net/
- wxPython unter Windows:
http://wxpython.org/docs/api/wx.Wind...RegisterHotKey

regards,
Gerold
:-)

--
__________________________________________________ ______________________
Gerold Penz - bcom - Programmierung
ge*********@tirol.utanet.at | http://gerold.bcom.at | http://sw3.at
Ehrliche, herzliche Begeisterung ist einer der
wirksamsten Erfolgsfaktoren. Dale Carnegie
Dec 7 '06 #3
XLib would really only see the keys pressed when under X, do you want
it to catch the keys directly from the keyboard?

Gerold Penz wrote:
I want my function to execute when the user presses the
hotkey anywhere.

Hi!

- XLib unter Linux: http://python-xlib.sourceforge.net/
- wxPython unter Windows:
http://wxpython.org/docs/api/wx.Wind...RegisterHotKey

regards,
Gerold
:-)

--
__________________________________________________ ______________________
Gerold Penz - bcom - Programmierung
ge*********@tirol.utanet.at | http://gerold.bcom.at | http://sw3.at
Ehrliche, herzliche Begeisterung ist einer der
wirksamsten Erfolgsfaktoren. Dale Carnegie
Dec 7 '06 #4
If you're using python 2.4, you can use the pyHook library (I don't
think it has been ported to 2.5 yet... I should email him about that),
which can hook both the mouse and keyboard, so your program can be
running in the background and still catch what keys (or mouse clicks)
are pressed. The pyHook extension comes with some very good examples
of how to use it, but if they aren't good enough examples (I can't
imagine that would happen), you should check out pykeylogger
(sourceforge) which uses the pyHook extension (it's a really good
program. Although not like conventional keyloggers built in C/C++ which
hide themselves from the OS - the purpose was not actually to spy on
the user but to create backups of what was typed - it still does a very
good job and the latest release has a lot of options and
extendability). If your OS is linux, unix, or mac... good luck ;D

Cheers,
Jordan
Gerold Penz wrote:
I want my function to execute when the user presses the
hotkey anywhere.

Hi!

- XLib unter Linux: http://python-xlib.sourceforge.net/
- wxPython unter Windows:
http://wxpython.org/docs/api/wx.Wind...RegisterHotKey

regards,
Gerold
:-)

--
__________________________________________________ ______________________
Gerold Penz - bcom - Programmierung
ge*********@tirol.utanet.at | http://gerold.bcom.at | http://sw3.at
Ehrliche, herzliche Begeisterung ist einer der
wirksamsten Erfolgsfaktoren. Dale Carnegie
Dec 7 '06 #5
Sorry, I should have mentioned that I'm running Linux, and I only will
be running this app while X is running. pyHook doesn't seem to be an
option because it's win32 only. I'm guessing python-xlib is the way to
go, but it seems to be that it relies on you understanding the C xlib
library, which is old and heavily obfuscated. Any advice on how to use
python xlib to register a global hotkey? In the docs I found the
KeyEvent event type, but it seems to want a specific window too.

Dec 8 '06 #6
k0*****@gmail.com wrote:
Sorry, I should have mentioned that I'm running Linux, and I only will
be running this app while X is running.
Global "hot keys" are typically the domain of the desktop environment
(or window manager for archaic desktops). For example, KDE has a
configuration dialogue where you can define hot keys or key
combinations that do particular things: I've got the Windows key doing
useful stuff in combination with other keys, for example.

So programmatically setting hot keys may be something that requires
communication with the desktop environment, and it would appear that on
KDE you have to put hot key definitions in a file and then import them,
the latter possibly being achieved using the DCOP interprocess
communications mechanism provided in the environment (try "dcop
khotkeys khotkeys" at the shell). Here's the only documentation I've
found so far:

http://developer.kde.org/~seli/khotkeys/

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.

Paul

Dec 8 '06 #7

Paul Boddie wrote:
k0*****@gmail.com wrote:
Sorry, I should have mentioned that I'm running Linux, and I only will
be running this app while X is running.

Global "hot keys" are typically the domain of the desktop environment
(or window manager for archaic desktops).
Yep, that's why my first thought was to use PyQT or PyGTK.
Unfortunately neither seems to have the ability.
So programmatically setting hot keys may be something that requires
communication with the desktop environment, and it would appear that on
KDE you have to put hot key definitions in a file and then import them,
the latter possibly being achieved using the DCOP interprocess
communications mechanism provided in the environment (try "dcop
khotkeys khotkeys" at the shell).
This is really annoying to setup and would be a KDE specific solution.
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.
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?

Dec 9 '06 #8
On Thursday 07 December 2006 05:53, k0*****@gmail.com wrote:
I want to make a Python app that runs in the background, and when a
user hits a key combination, for a function to run. This sounds simple
enough, but all of the keypress detecting libraries I can find count on
you creating a window and then detecting keypresses while that window
has focus. I want my function to execute when the user presses the
hotkey anywhere. I searched the PyGTK documentation and found an old
newsgroup post where someone mentioned the C GTK+ library has it but
PyGTK does not, PyQT showed no results, not sure where else I should
look. I'd be willing to use a library that isn't a windowing toolkit --
I just want to be able to be able to globally detect a keypress. Any
ideas?
A little bit of googling revealed:

XGrabKey: http://tronche.com/gui/x/xlib/input/XGrabKey.html
Example: http://tigerdyr.wheel.dk/ctwm-archive/1328.html

It's done here in C, hopefully you can do the same with the python module for
xlib that was mentioned earlier.

- Jonathan

Google = best friend
Dec 9 '06 #9
k0*****@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/Stan..._2dkeys_2dspec
http://portland.freedesktop.org/wiki/IntegrationTasks

Paul

Dec 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nitesh Jain | last post by:
I want to write a simple utility, so that if I select a text, and press a hot key say (CTRL+ALT+G), then I should be able to open a google search page with the selected text. Could someone tell me...
5
by: jcrouse | last post by:
I have an application thats a viewer. I also have a form that is fired from a Tools/Options menu selection. In the Options form the user can select a hotkey to use to exit the main viewer...
3
by: Rsrany | last post by:
I've been working on a few gtk applications and need to tie a hot key catcher into a thread. I am currently finding threaded user32.GetMessageA do not work. I have included two programs: 1) a...
2
by: Marc Gravell | last post by:
Just a thought prompted by the chain "Splitting up a string"; ages ago a wrote (in VB via Windows API dll calls) a global hotkey app that runs silently, intercepts ++V, converts the clipboard to a...
1
by: Rune Jacobsen | last post by:
Hi all, I have some often-performed tasks in my applications that I want users to be able to specify hotkeys for. However, I don't want to hard code these hotkeys, as sooner or later some other...
9
by: kimiraikkonen | last post by:
Hi, Here is 2 questions: 1) I declared some global hotkeys located inside Windows API follows as: Private Const MOD_ALT As Integer = 1 Private Const MOD_CONTROL As Integer = 2 Private Const...
3
by: =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post by:
I want to have following hotkey used in asp.net application, how to do this especially for F5? ESC F3 F4 F5 F6 F9 F10
3
by: Appr3nt1c3 | last post by:
Hello everyone! I am new to VB .Net and currently i'm using vb .net 2005. My question is: Is there a way to assign a global key handler for a certain key, like if i press F9 or any other key...
18
by: AdamOnAccess | last post by:
Anyone know an easy way to toggle between Form and Design view with a hotkey? I know you can switch from Design view to Form view with hotkey F5. But is there a hotkey to switch back (from Form...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.