[mwm-aal-display]
Intro
This is the pyhk end user documentation. It is aimed at end users of PYHK-Python Hotkey Module. Developers please see function descriptions within pyhk.py. You get a good overview of all functions by using help(pyhk) in the command line (after import pyhk).
You can find a current version and a brief intro on my PYHK python hotkey module site.
Dependencies
Hotkeys
Hotkey notation
The data representation of a hotkey is a list of strings.
Example:
['Ctrl','7']
['mouse left','A']
Note: To set a single key as a hotkey you have to set it as a list.
Example
['F7']
Hotkey types
There are three different types of hotkeys in pyhk:
Normal hotkeys
Merged hotkeys
Single use hotkeys
Normal hotkeys
The majority of all hotkeys belongs to this type. You can combine any number of normal hotkeys to form a hotkey. A list of all normal hotkeys is at the end.
Example
['mouse left','A']
['Lcontrol','7'] #defines only left Ctrl key as hotkey
Merged hotkeys
Merged hotkeys are hotkeys that are created by combining two or more hotkeys from normal hotkeys together into a single hotkey. They are build from normal hotkeys and behave just like them. This group contains Ctrl, Alt and Shift.
Example
['Ctrl','7']
['Ctrl','Shift','Q']
Single use hotkeys
The hotkeys mouse move, mouse wheel up and mouse wheel down are in this group. You can not combine these hotkeys with others. They can only be used on their own.
Example
['mouse wheel down']
Note
['mouse wheel down','4'] #does not work
Hotkey registration
You can add as many hotkeys as you want with function addHotkey. To register a hotkey, define your function and hotkey, create an instance of pyhk, add your hotkey and finally start pyhk.
Example
import pyhk
def fun():
print "Do something"
#create pyhk class instance
hot = pyhk.pyhk()
#add hotkey
id1 = hot.addHotkey(['Ctrl', 'Alt','7'],fun)
#start looking for hotkey.
hot.start()
The return value of addHotkey is the hotkeys ID.
Hotkey registration options
Thread
You can execute a function in a seperate thread by using isThread = True.
id1 = hot.addHotkey(['Ctrl', 'Alt','7'],fun, isThread=True)
This is useful if you function takes a while to execute.
Key up events
You can register single key up events by using the option up = True.
id1 = hot.addHotkey(['F7'],fun, up=True)
Hotkey removal
You can remove a hotkey with the function removeHotkey by removing all functions assigned to a hotkey.
hot.removeHotkey(['Ctrl', 'Alt','7'])
Alternatively you can remove hotkeys with its id.
hot.removeHotkey(id=id1)
Removing hotkeys by their ids is useful when you register several functions to the same hotkey.
To remove all hotkeys use:
hot.removeHotkey()
End hotkey
PYHK has a special hotkey called end hotkey. This hotkey stops pyhk from waiting for hotkeys. The end hotkey is set to:
['Ctrl','Shift','Q']
and stops waiting for other hotkey inputs. It breaks the hot.start() waiting loop.
Should you not want an end hotkey, you can remove it with removeHotkey.
The end hotkey works like every other hotkey. The only difference is that it has its own function to add it. You can add the end hotkey with setEndHotkey. Note that you dont have to pass a function.
hot.setEndHotkey(['Alt','Q'])
Passing arguments in hotkey function
You can not pass arguments on hotkey function execution. An elegant solution to create a similar effect is to create a class that contains data you want to be passed and define your function in that class.
Example
import pyhk
class test:
def __init__(self):
self.data =0
def fun(self):
self.data += 1
print self.data
hot = pyhk.pyhk()
t = test()
hot.addHotkey(['Ctrl', 'Alt','7'],t.fun)
hot.start()
PYHK in wxpython
You can use pyhk in wxpython and set global hotkeys. I expect pyhk to also work with other toolkits, but I didnt get a chance to test it yet.
To use pyhk in wxpython you have to make sure you initiate pyhk outside of your frame class. Furthermore the pyhk start() command is not necessary anymore since wxpython will do that for you.
Example
import wx
import pyhk
#run outside of frame class
hot = pyhk.pyhk()
class Test(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(250, 100))
self.count = 0
HotkeyCounterLbl = wx.StaticText(self, wx.ID_ANY, "Hotkey counter:", (20,20))
self.HotkeyCounter = wx.TextCtrl(self, wx.ID_ANY, "0",(100,20))
hot.addHotkey(['Ctrl', 'Alt','7'],self.hotkeyFun)
def hotkeyFun(self):
"""Execute when hotkey is pressed"""
self.count += 1
self.HotkeyCounter.SetValue(str(self.count))
# Run the program
if __name__ == "__main__":
app = wx.App()
frame = Test(None, -1, 'Test pyhk in wxpython')
frame.Show(True)
app.MainLoop()
Time consuming functions
If you have functions that take a lot of time, I suggest you let them execute in a thread. I have experienced problems that pyhk would stop triggering hotkeys after some function executions. Just use isThread=True when you register a hotkey.
id1 = hot.addHotkey(['Ctrl', 'Alt','7'],fun, isThread=True)
Pyhk freezes IDLE development environment
I have noticed that the IDLE development environment freezes when I run pyhk from the IDLE. It is best to test your programs with python.exe from the command line. Either set your .py files to run with python.exe or run your code from the command line.
Hotkey list
Normal hotkeys
Back
Tab
Return
Capital
Escape
Space
Prior
Next
End
Home
Left
Up
Right
Down
Snapshot
Delete
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Numpad0
Numpad1
Numpad2
Numpad3
Numpad4
Numpad5
Numpad6
Numpad7
Numpad8
Numpad9
Multiply
Add
Subtract
Decimal
Divide
F1
F2
F3
F4
F5
F6
F7
F8
F9
F10
F11
F12
Numlock
mouse left
mouse right
mouse middle
Merged hotkeys
Ctrl
Alt
Shift
Single use hotkeys
mouse move
mouse wheel up
mouse wheel down
The perfect period to resolve health problem is before any visible sign appears. Actually there isn’t anything you can’t purchase on the Internet anymore. Whereas erectile disfunction is more common among older men, that doesn’t something you simply have to live with. What about “canadian pharmacy cialis 5 mg” and “cialis canadian pharmacy“? In our generation, there are divers options for those ones who suffer from erectile dysfunction. You may have heard about “canadian pharmacy online cialis“. More data about the question available at “canadian pharmacy cialis 20mg“. Why it happen? Can sexual disorders in men be prevented? If you get any prescription remedies like Viagra, check with a druggist that they are sure to take with your other remedies.
Thanks for this. Just what I was looking for. You might want to add that pywin32 and pyhook are dependencies. On the page. Once you try to use it, it becomes obvious.
Thanks Ken for the feedback. Added the dependencies to the documentation.
For some reason I can’t use “Shift” to bind with my keys….
Nevermind… Apparently trying to bind a hot key with Shift and a Numpad number doesn’t work…
Thanks a lot, well done! One suggestion: I could imagine to extend the function “addHotKey” by an additional value:
addHotkey(self,hotkey,fhot, isThread=False, up=False, passEvt=True)
–> passEvt is then used in OnKeyDown/OnKeyUp as return value. So you could avoid to handle some events by other handlers.
Wonderful work. I like the documentation for the most part. I was wondering if you would mind putting the other names for Prior, Next, Back (I’m assuming backspace), and snapshot (I’m assuming Print Screen)? I could look them up, but I thought that it might be easier for others to know in the future.
Also, what about the ‘Pause’ key? I use that key frequently while running scripts and was just wondering if that could be an option?
Thank you for you hard work with this. I love it.
Hi There,
Thanks for all your work. I’m running into the error “runtime error R6031
– Attempt to initialize the CRT more than once.” Any idea what could be behind that?
Thanks!