4
4
from dataclasses import dataclass
5
5
from collections import deque # for storing x, y time series
6
6
import numpy as np # for smoothing moving average
7
- import ctypes # for windows mouse
7
+ import ctypes # for windows mouse POINT struct
8
8
import socket # udp networking
9
9
import struct # binary unpacking
10
+ # import keyboard # keyboard shortcuts
11
+
12
+ from pynput import keyboard
13
+
14
+
15
+
16
+
17
+ # Collect events until released
18
+ # with keyboard.Listener(
19
+ # on_press=on_press,
20
+ # on_release=on_release) as listener:
21
+ # listener.join()
22
+
23
+ # ...or, in a non-blocking fashion:
24
+
25
+
26
+ enabled = True
27
+
28
+ def toggle ():
29
+ global enabled
30
+ logging .info ("\n Toggled PhilNav on/off\n " )
31
+ enabled = not enabled
32
+
33
+ # https://pynput.readthedocs.io/en/latest/keyboard.html#global-hotkeys
34
+
35
+ def for_canonical (f ):
36
+ return lambda k : f (listener .canonical (k ))
37
+
38
+ hotkey = keyboard .HotKey (
39
+ [keyboard .Key .shift , keyboard .Key .f7 .value ],
40
+ toggle )
41
+
42
+ listener = keyboard .Listener (
43
+ on_press = for_canonical (hotkey .press ),
44
+ on_release = for_canonical (hotkey .release ))
45
+
46
+ listener .start ()
47
+
48
+ # keyboard.add_hotkey('space', toggle)
49
+ # keyboard.add_hotkey('space', toggle)
50
+
51
+ # keyboard.write('The quick brown fox jumps over the lazy dog.')
52
+ # keyboard.add_hotkey('space', lambda: print('space was pressed!'))
53
+
54
+ # returned from ctypes.windll.user32.GetCursorPos
55
+ # simple point.x, point.y
56
+ class POINT (ctypes .Structure ):
57
+ _fields_ = [("x" , ctypes .c_long ), ("y" , ctypes .c_long )]
58
+
59
+ import platform
10
60
# Done: Windows
11
61
# TODO: Mac, Linux
62
+ match platform .system ():
63
+ case "Darwin" : # macOS
64
+ from mouse_mac import getCursorPos , setCursorPos
65
+
12
66
13
67
print ("\n \n CLIENT: Starting PhilNav\n " )
14
68
43
97
logging .info (" Logging verbosely\n " )
44
98
45
99
46
- # returned from ctypes.windll.user32.GetCursorPos
47
- # simple point.x, point.y
48
- class POINT (ctypes .Structure ):
49
- _fields_ = [("x" , ctypes .c_long ), ("y" , ctypes .c_long )]
50
100
51
101
52
102
# initialize networking
@@ -65,8 +115,7 @@ class POINT(ctypes.Structure):
65
115
# would allow discovery, but when you are *in public* - like at a coffee shop -
66
116
# you don't want strangers to access your PC.
67
117
text_listening = (
68
- f"Listening on {
69
- sock .getsockname ()} for mouse data from Raspberry Pi server..."
118
+ f"Listening on { sock .getsockname ()} for mouse data from Raspberry Pi server..."
70
119
)
71
120
print (ctime () + " - " + text_listening )
72
121
print ("\n Press Ctrl-C to exit\n " )
@@ -137,16 +186,15 @@ def smooth(q):
137
186
y_smooth = y
138
187
139
188
# The Magic Happens Now! eg. move mouse cursor =P
140
- pt = POINT ()
141
- ctypes .windll .user32 .GetCursorPos (
142
- ctypes .byref (pt )
143
- ) # get current mouse position by reference (C++ thing)
189
+ x_cur , y_cur = getCursorPos ()
190
+ # get current mouse position by reference (C++ thing)
144
191
# I'm moving the Y axis slightly faster because looking left and right
145
192
# is easier than nodding up and down. Also, monitors are wider than they
146
193
# are tall.
147
- x_new = round (pt .x + x_smooth * args .speed )
148
- y_new = round (pt .y + y_smooth * args .speed * 1.25 )
149
- ctypes .windll .user32 .SetCursorPos (x_new , y_new ) # move mouse cursor
194
+ x_new = round (x_cur + x_smooth * args .speed )
195
+ y_new = round (y_cur + y_smooth * args .speed * 1.25 )
196
+ if enabled :
197
+ setCursorPos (x_new , y_new ) # move mouse cursor
150
198
151
199
# I'm trying to measure the total time from capturing the frame on the
152
200
# camera to moving the mouse cursor on my PC. This isn't super accurate.
@@ -161,10 +209,8 @@ def smooth(q):
161
209
# display legend every 5 seconds
162
210
if PhilNav .debug_num % 5 == 1 :
163
211
logging .info (
164
- f" { ctime ()} - Received: ({ 'x_diff' :>8} ,{ 'y_diff' :>8} ,{
165
- 'n/a' :>8} ,{ 'n/a' :>8} ,{ 'loc ns' :>8} ,{ 'net ms' :>8} )"
212
+ f" { ctime ()} - Received: ({ 'x_diff' :>8} ,{ 'y_diff' :>8} ,{ 'n/a' :>8} ,{ 'n/a' :>8} ,{ 'loc ns' :>8} ,{ 'net ms' :>8} )"
166
213
)
167
214
logging .info (
168
- f" { ctime ()} - Received: ({ x :> 8.2f} ,{ y :> 8.2f} ,{ z :> 8.2f} ,{ pitch :> 8.2f} ,{
169
- (time () - PhilNav .msg_time_start )* 1000 :> 8.2f} ,{ time_diff_ms :> 8} )"
215
+ f" { ctime ()} - Received: ({ x :> 8.2f} ,{ y :> 8.2f} ,{ z :> 8.2f} ,{ pitch :> 8.2f} ,{ (time () - PhilNav .msg_time_start )* 1000 :> 8.2f} ,{ time_diff_ms :> 8} )"
170
216
)
0 commit comments