Home PYHK End User Documentation
PYHK End User Documentation
Cancel

PYHK End User Documentation

[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

pyhook, pywin32

Hotkeys

Hotkey notation

The data representation of a hotkey is a list of strings.

Example:

1
2
3
<pre lang="py" toggle="no">['Ctrl','7']

['mouse left','A']

Note: To set a single key as a hotkey you have to set it as a list.

Example

1
<pre lang="py" toggle="no">['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

1
2
3
<pre lang="py" toggle="no">['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

1
2
3
<pre lang="py" toggle="no">['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

1
<pre lang="py" toggle="no">['mouse wheel down']

Note

1
<pre lang="py" toggle="no">['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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<pre lang="py" toggle="no">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.

1
<pre lang="py" toggle="no">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.

1
<pre lang="py" toggle="no">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.

1
<pre lang="py" toggle="no">hot.removeHotkey(['Ctrl', 'Alt','7'])

Alternatively you can remove hotkeys with its id.

1
<pre lang="py" toggle="no">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:

1
<pre lang="py" toggle="no">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:

1
<pre lang="py" toggle="no">['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.

1
<pre lang="py" toggle="no">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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<pre lang="py" toggle="no">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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<pre lang="py" toggle="no">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.

1
<pre lang="py" toggle="no">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

Trending Tags