forked from JeffHoogland/pyxhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
42 lines (30 loc) · 833 Bytes
/
example.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
39
40
41
42
"""
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):
global running
# print key info
print(event)
# If the ascii value matches spacebar, terminate the while loop
if event.Ascii == 32:
running = False
def mouse_event(event):
print(event)
# Create hookmanager
hookman = pyxhook.HookManager()
hookman.KeyDown = kbevent
hookman.MouseMovement = mouse_event
hookman.start()
# Create a loop to keep the application running
running = True
while running:
time.sleep(0.1)
# Close the listener when we are done
hookman.cancel()