-
Notifications
You must be signed in to change notification settings - Fork 17
/
pymouse.py
45 lines (39 loc) · 1.2 KB
/
pymouse.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
43
44
45
import threading
XSIGN = 1<<4
YSIGN = 1<<5
class MouseThread ( threading.Thread ):
def __init__(self):
threading.Thread.__init__(self)
self.fd = open('/dev/input/mouse0','r')
self.x = 800
self.y = 400
self.height=1080
self.width=1920
self.finished=False
def run ( self ):
while 1:
while 1:
buttons,dx,dy=map(ord,self.fd.read(3))
if buttons&8:
break # This bit should always be set
self.fd.read(1) # Try to sync up again
if buttons&3:
self.finished=True
break # Stop if mouse button pressed!
if buttons&XSIGN:
dx-=256
if buttons&YSIGN:
dy-=256
self.x+=dx
self.y+=dy
if self.x<0: self.x=0
if self.y<0: self.y=0
self.x=min(self.x,self.width)
self.y=min(self.y,self.height)
print self.x,self.y
def start_mouse():
"""Start a thread to read the PS2 mouse stream.
Returns a mouse object, can get m.x and m.y mouse position"""
m=MouseThread()
m.start()
return m