forked from JeffHoogland/pyxhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_with_parameters.py
38 lines (32 loc) · 997 Bytes
/
example_with_parameters.py
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
"""
A simple example of hooking the keyboard on Linux using pyxhook
Any key pressed prints out the keys values, program terminates when spacebar
is pressed.
"""
from __future__ import print_function
# Libraries we need
import pyxhook
import time
# This function is called every time a key is presssed
def kbevent(event, params):
# print key info
print(event)
# If the ascii value matches spacebar, terminate the while loop
if event.Ascii == 32:
params['running'] = False
parameters={'running':True}
# Create hookmanager
hookman = pyxhook.HookManager(parameters=True)
# Define our callback to fire when a key is pressed down
hookman.KeyDown = kbevent
# Define our parameters for callback function
hookman.KeyDownParameters = parameters
# Hook the keyboard
hookman.HookKeyboard()
# Start our listener
hookman.start()
# Create a loop to keep the application running
while parameters['running']:
time.sleep(0.1)
# Close the listener when we are done
hookman.cancel()