11

I wanna setup a global hotkey in python 2.6 that listens to the keyboard shortcut ctrl + D or ctrl+ alt+ D on windows, please help me

Patryk
  • 18,244
  • 37
  • 110
  • 212
arthur
  • 111
  • 1
  • 1
  • 3
  • This topic is also covered [here][1], however using Python 3. [1]: http://stackoverflow.com/questions/16615087/python-how-to-create-a-global-hotkey-on-windows-with-3-arguments/24654577#24654577 – Maxxim Jul 10 '14 at 07:09
  • https://github.com/boppreh/keyboard#keyboard.add_hotkey – iMath Jul 01 '18 at 12:12

4 Answers4

9

Tim Golden's python/win32 site is a useful resource for win32 related programming in python. In particular, this example should help:

ars
  • 106,073
  • 21
  • 135
  • 131
6

I suggest pyhk. It allows for global wide hotkey registration in python and comes with examples and documentation. Pyhk builds on pyhook.

Hotkey registration is as simple as:

pyhk.addHotkey(SomeHotkey,SomeFunction)
schurpf
  • 559
  • 6
  • 6
3

The RegisterHotKey method of the wx.Window class is what you're looking for -- as the docs say,

Registers a system wide hotkey. Every time the user presses the hotkey registered here, this window will receive a hotkey event. It will receive the event even if the application is in the background and does not have the input focus because the user is working with some other application. To bind an event handler function to this hotkey use EVT_HOTKEY with an id equal to hotkeyId. Returns True if the hotkey was registered successfully.

So, make an instance of `wx.Window, register the hotkey you want with this method, and possibly do a PushEventHandler if ypu'd rather handle the event(s) in a separate event handler rather than in the window itself (the latter being the default).

Is there anything else in this procedure that is not entirely clear to you...? If so, please edit your question to add whatever further problems you may have!

Alex Martelli
  • 762,786
  • 156
  • 1,160
  • 1,345
  • Since RegisterHotKey accepts keys from win32con that solution will not work for him cause he needs VK_ which is absent in win32con! – Unicorn Jan 25 '12 at 09:13
0

If you want hotkeys in your wxPython program (which I assume you do because of the wxPython tag), then you should use a wx.AcceleratorTable.

Mike Driscoll
  • 31,394
  • 6
  • 39
  • 83