Skip to content

Commit 8fd32d4

Browse files
committed
add mouse keycodes
1 parent ef6cdb1 commit 8fd32d4

File tree

3 files changed

+98
-6
lines changed

3 files changed

+98
-6
lines changed

keyboard/__init__.py

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

keyboard/action_code.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,30 @@ def mods_to_keycodes(mods):
545545
ACTION_USAGE_CONSUMER = lambda n: ACTION(ACT_USAGE, 1 << 10 | (n))
546546
ACTION_MOUSEKEY = lambda key: ACTION(ACT_MOUSEKEY, key)
547547

548+
549+
MS_BTN1 = MOUSEKEY(1 << 0)
550+
MS_BTN2 = MOUSEKEY(1 << 1)
551+
MS_BTN3 = MOUSEKEY(1 << 2)
552+
MS_BTN4 = MOUSEKEY(1 << 3)
553+
MS_BTN5 = MOUSEKEY(1 << 4)
554+
MS_UP = MOUSEKEY(1 << 8)
555+
MS_DN = MOUSEKEY(2 << 8)
556+
MS_LT = MOUSEKEY(3 << 8)
557+
MS_RT = MOUSEKEY(4 << 8)
558+
MS_UL = MOUSEKEY(5 << 8)
559+
MS_UR = MOUSEKEY(6 << 8)
560+
MS_DL = MOUSEKEY(7 << 8)
561+
MS_DR = MOUSEKEY(8 << 8)
562+
MS_W_UP = MOUSEKEY(9 << 8)
563+
MS_W_DN = MOUSEKEY(10 << 8)
564+
565+
MS_MOVEMENT = (
566+
(0, 0, 0),
567+
(0, -2, 0), (0, 2, 0), (-2, 0, 0), (2, 0, 0),
568+
(-1, -1, 0), (1, -1, 0), (-1, 1, 0), (1, 1, 0),
569+
(0, 0, 1), (0, 0, -1)
570+
)
571+
548572
MACRO = lambda n: ACTION(ACT_MACRO, n)
549573
BACKLIGHT = lambda n: ACTION(ACT_BACKLIGHT, n)
550574

keyboard/hid.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class HID:
1616
def __init__(self, devices):
1717
self.keyboard = find_device(devices, usage_page=0x1, usage=0x06)
1818
self.consumer_control = find_device(devices, usage_page=0x0C, usage=0x01)
19-
# self.mouse = find_device(devices, usage_page=0x1, usage=0x02)
19+
self.mouse = find_device(devices, usage_page=0x1, usage=0x02)
2020

2121
self.consumer_report = bytearray(2)
2222

@@ -26,6 +26,8 @@ def __init__(self, devices):
2626
self.report = bytearray(8)
2727
self.report_keys = memoryview(self.report)[2:]
2828

29+
self.mouse_report = bytearray(4)
30+
2931
for device in devices:
3032
if (
3133
device.usage_page == 0x1
@@ -75,11 +77,34 @@ def send_consumer(self, keycode):
7577
struct.pack_into("<H", self.consumer_report, 0, keycode)
7678
self.consumer_control.send_report(self.consumer_report)
7779

80+
def press_mouse(self, buttons):
81+
self.mouse_report[0] |= buttons
82+
self.mouse_report[1] = 0
83+
self.mouse_report[2] = 0
84+
self.mouse_report[3] = 0
85+
self.mouse.send_report(self.mouse_report)
86+
87+
def release_mouse(self, buttons):
88+
self.mouse_report[0] &= ~buttons
89+
self.mouse_report[1] = 0
90+
self.mouse_report[2] = 0
91+
self.mouse_report[3] = 0
92+
self.mouse.send_report(self.mouse_report)
93+
94+
def move_mouse(self, x=0, y=0, wheel=0):
95+
self.mouse_report[1] = x & 0xFF
96+
self.mouse_report[2] = y & 0xFF
97+
self.mouse_report[3] = wheel & 0xFF
98+
self.mouse.send_report(self.mouse_report)
99+
78100
def release_all(self):
79101
self.send_consumer(0)
80102
for i in range(8):
81103
self.report[i] = 0
82104
self.keyboard.send_report(self.report)
105+
for i in range(4):
106+
self.mouse_report[i] = 0
107+
self.mouse.send_report(self.mouse_report)
83108

84109
@property
85110
def leds(self):

0 commit comments

Comments
 (0)