@@ -110,7 +110,7 @@ def __init__(self, keymap=(), pairs=(), verbose=True):
110110 self .adv_timeout = None
111111
112112 size = 4 + self .matrix .keys
113- self .data = array .array ("L" , microcontroller .nvm [:size * 4 ])
113+ self .data = array .array ("L" , microcontroller .nvm [: size * 4 ])
114114 if self .data [0 ] != 0x424B5950 :
115115 self .data [0 ] = 0x424B5950
116116 self .data [1 ] = 1
@@ -415,6 +415,33 @@ def send_consumer(self, keycode):
415415 except Exception as e :
416416 print (e )
417417
418+ def press_mouse (self , buttons ):
419+ try :
420+ if self .usb_status == 0x3 and usb_is_connected ():
421+ self .usb_hid .press_mouse (buttons )
422+ elif self .ble .connected :
423+ self .ble_hid .press_mouse (buttons )
424+ except Exception as e :
425+ print (e )
426+
427+ def release_mouse (self , buttons ):
428+ try :
429+ if self .usb_status == 0x3 and usb_is_connected ():
430+ self .usb_hid .release_mouse (buttons )
431+ elif self .ble .connected :
432+ self .ble_hid .release_mouse (buttons )
433+ except Exception as e :
434+ print (e )
435+
436+ def move_mouse (self , x = 0 , y = 0 , wheel = 0 ):
437+ try :
438+ if self .usb_status == 0x3 and usb_is_connected ():
439+ self .usb_hid .move_mouse (x , y , wheel )
440+ elif self .ble .connected :
441+ self .ble_hid .move_mouse (x , y , wheel )
442+ except Exception as e :
443+ print (e )
444+
418445 def get (self ):
419446 event = self .matrix .get ()
420447 key = event & 0x7F
@@ -432,10 +459,19 @@ def run(self):
432459 keys = [0 ] * matrix .keys
433460 ms = matrix .ms
434461 last_time = 0
462+ mouse_action = 0
463+ mouse_time = 0
435464 while True :
436- t = 20 if self .backlight .check () else 1000
465+ t = 20 if self .backlight .check () or mouse_action else 1000
437466 n = matrix .wait (t )
438467 self .check ()
468+
469+ if mouse_action :
470+ x , y , wheel = MS_MOVEMENT [mouse_action ]
471+ dt = 1 + (time .monotonic_ns () - mouse_time ) // 8000000
472+ mouse_time = time .monotonic_ns ()
473+ self .move_mouse (x * dt , y * dt , wheel * dt )
474+
439475 if n == 0 :
440476 continue
441477
@@ -495,8 +531,11 @@ def run(self):
495531 if action_code & 0x400 :
496532 self .send_consumer (action_code & 0x3FF )
497533 elif kind == ACT_MOUSEKEY :
498- # todo
499- pass
534+ if action_code & 0xF00 == 0 :
535+ self .press_mouse (action_code & 0xF )
536+ else :
537+ mouse_action = (action_code >> 8 ) & 0xF
538+ mouse_time = time .monotonic_ns ()
500539 elif kind == ACT_LAYER_TAP or kind == ACT_LAYER_TAP_EXT :
501540 layer = (action_code >> 8 ) & 0x1F
502541 mask = 1 << layer
@@ -599,7 +638,11 @@ def run(self):
599638 if action_code & 0x400 :
600639 self .send_consumer (0 )
601640 elif kind == ACT_MOUSEKEY :
602- pass
641+ if action_code & 0xF00 == 0 :
642+ self .release_mouse (action_code & 0xF )
643+ elif (action_code >> 8 ) & 0xF == mouse_action :
644+ mouse_action = 0
645+ self .move_mouse (0 , 0 , 0 )
603646 elif kind == ACT_LAYER_TAP or kind == ACT_LAYER_TAP_EXT :
604647 layer = (action_code >> 8 ) & 0x1F
605648 keycode = action_code & 0xFF
0 commit comments