refactor mouse_keys module to use global pd

This commit is contained in:
xs5871 2022-10-22 20:18:33 +00:00 committed by xs5871
parent e84bbd0d75
commit fc8d5edd52

View File

@ -1,7 +1,7 @@
from supervisor import ticks_ms from supervisor import ticks_ms
from kmk.hid import HID_REPORT_SIZES, HIDReportTypes from kmk.hid import HID_REPORT_SIZES, HIDReportTypes
from kmk.keys import make_key from kmk.keys import make_key, make_mouse_key, Axis
from kmk.modules import Module from kmk.modules import Module
@ -24,31 +24,29 @@ class PointingDevice:
class MouseKeys(Module): class MouseKeys(Module):
def __init__(self): def __init__(self):
self.pointing_device = PointingDevice()
self._nav_key_activated = 0 self._nav_key_activated = 0
self._up_activated = False self._up_activated = False
self._down_activated = False self._down_activated = False
self._left_activated = False self._left_activated = False
self._right_activated = False self._right_activated = False
self._mw_up_activated = False
self._mw_down_activated = False
self.max_speed = 10 self.max_speed = 10
self.ac_interval = 100 # Delta ms to apply acceleration self.ac_interval = 100 # Delta ms to apply acceleration
self._next_interval = 0 # Time for next tick interval self._next_interval = 0 # Time for next tick interval
self.move_step = 1 self.move_step = 1
make_key( make_mouse_key(
names=('MB_LMB',), names=('MB_LMB',),
on_press=self._mb_lmb_press, code=1,
on_release=self._mb_lmb_release,
) )
make_key( make_mouse_key(
names=('MB_MMB',), names=('MB_MMB',),
on_press=self._mb_mmb_press, code=4,
on_release=self._mb_mmb_release,
) )
make_key( make_mouse_key(
names=('MB_RMB',), names=('MB_RMB',),
on_press=self._mb_rmb_press, code=2,
on_release=self._mb_rmb_release,
) )
make_key( make_key(
names=('MW_UP',), names=('MW_UP',),
@ -94,38 +92,40 @@ class MouseKeys(Module):
) )
def during_bootup(self, keyboard): def during_bootup(self, keyboard):
return keyboard.axes['W'] = Axis(2)
keyboard.axes['X'] = Axis(0)
def matrix_detected_press(self, keyboard): keyboard.axes['Y'] = Axis(1)
return keyboard.matrix_update is None
def before_matrix_scan(self, keyboard): def before_matrix_scan(self, keyboard):
return return
def after_matrix_scan(self, keyboard): def after_matrix_scan(self, keyboard):
if self._next_interval > ticks_ms():
return
self._next_interval = ticks_ms() + self.ac_interval
if self._nav_key_activated: if self._nav_key_activated:
if self._next_interval <= ticks_ms(): if self.move_step < self.max_speed:
# print("hello: ") self.move_step = self.move_step + 1
# print(ticks_ms())
self._next_interval = ticks_ms() + self.ac_interval
# print(self._next_interval)
if self.move_step < self.max_speed:
self.move_step = self.move_step + 1
if self._right_activated: if self._right_activated:
self.pointing_device.report_x[0] = self.move_step keyboard.axes['X'].delta += self.move_step
if self._left_activated: if self._left_activated:
self.pointing_device.report_x[0] = 0xFF & (0 - self.move_step) keyboard.axes['X'].delta -= self.move_step
if self._up_activated: if self._up_activated:
self.pointing_device.report_y[0] = 0xFF & (0 - self.move_step) keyboard.axes['Y'].delta -= self.move_step
if self._down_activated: if self._down_activated:
self.pointing_device.report_y[0] = self.move_step keyboard.axes['Y'].delta += self.move_step
self.pointing_device.hid_pending = True keyboard.hid_pending = True
return
if self._mw_up_activated:
keyboard.axes['W'].delta += self.move_step
keyboard.hid_pending = True
if self._mw_down_activated:
keyboard.axes['W'].delta -= self.move_step
keyboard.hid_pending = True
def before_hid_send(self, keyboard): def before_hid_send(self, keyboard):
if self.pointing_device.hid_pending and keyboard._hid_send_enabled:
keyboard._hid_helper.hid_send(self.pointing_device._evt)
self.pointing_device.hid_pending = False
return return
def after_hid_send(self, keyboard): def after_hid_send(self, keyboard):
@ -137,45 +137,17 @@ class MouseKeys(Module):
def on_powersave_disable(self, keyboard): def on_powersave_disable(self, keyboard):
return return
def _mb_lmb_press(self, key, keyboard, *args, **kwargs):
self.pointing_device.button_status[0] |= self.pointing_device.MB_LMB
self.pointing_device.hid_pending = True
def _mb_lmb_release(self, key, keyboard, *args, **kwargs):
self.pointing_device.button_status[0] &= ~self.pointing_device.MB_LMB
self.pointing_device.hid_pending = True
def _mb_mmb_press(self, key, keyboard, *args, **kwargs):
self.pointing_device.button_status[0] |= self.pointing_device.MB_MMB
self.pointing_device.hid_pending = True
def _mb_mmb_release(self, key, keyboard, *args, **kwargs):
self.pointing_device.button_status[0] &= ~self.pointing_device.MB_MMB
self.pointing_device.hid_pending = True
def _mb_rmb_press(self, key, keyboard, *args, **kwargs):
self.pointing_device.button_status[0] |= self.pointing_device.MB_RMB
self.pointing_device.hid_pending = True
def _mb_rmb_release(self, key, keyboard, *args, **kwargs):
self.pointing_device.button_status[0] &= ~self.pointing_device.MB_RMB
self.pointing_device.hid_pending = True
def _mw_up_press(self, key, keyboard, *args, **kwargs): def _mw_up_press(self, key, keyboard, *args, **kwargs):
self.pointing_device.report_w[0] = self.move_step self._mw_up_activated = True
self.pointing_device.hid_pending = True
def _mw_up_release(self, key, keyboard, *args, **kwargs): def _mw_up_release(self, key, keyboard, *args, **kwargs):
self.pointing_device.report_w[0] = 0 self._mw_up_activated = False
self.pointing_device.hid_pending = True
def _mw_down_press(self, key, keyboard, *args, **kwargs): def _mw_down_press(self, key, keyboard, *args, **kwargs):
self.pointing_device.report_w[0] = 0xFF self._mw_down_activated = True
self.pointing_device.hid_pending = True
def _mw_down_release(self, key, keyboard, *args, **kwargs): def _mw_down_release(self, key, keyboard, *args, **kwargs):
self.pointing_device.report_w[0] = 0 self._mw_down_activated = False
self.pointing_device.hid_pending = True
# Mouse movement # Mouse movement
def _reset_next_interval(self): def _reset_next_interval(self):
@ -191,56 +163,38 @@ class MouseKeys(Module):
self._nav_key_activated += 1 self._nav_key_activated += 1
self._reset_next_interval() self._reset_next_interval()
self._up_activated = True self._up_activated = True
self.pointing_device.report_y[0] = 0xFF & (0 - self.move_step)
self.pointing_device.hid_pending = True
def _ms_up_release(self, key, keyboard, *args, **kwargs): def _ms_up_release(self, key, keyboard, *args, **kwargs):
self._up_activated = False self._up_activated = False
self._nav_key_activated -= 1 self._nav_key_activated -= 1
self._check_last() self._check_last()
self.pointing_device.report_y[0] = 0
self.pointing_device.hid_pending = False
def _ms_down_press(self, key, keyboard, *args, **kwargs): def _ms_down_press(self, key, keyboard, *args, **kwargs):
self._nav_key_activated += 1 self._nav_key_activated += 1
self._reset_next_interval() self._reset_next_interval()
self._down_activated = True self._down_activated = True
# if not self.x_activated and not self.y_activated:
# self.next_interval = ticks_ms() + self.ac_intervalle
self.pointing_device.report_y[0] = self.move_step
self.pointing_device.hid_pending = True
def _ms_down_release(self, key, keyboard, *args, **kwargs): def _ms_down_release(self, key, keyboard, *args, **kwargs):
self._down_activated = False self._down_activated = False
self._nav_key_activated -= 1 self._nav_key_activated -= 1
self._check_last() self._check_last()
self.pointing_device.report_y[0] = 0
self.pointing_device.hid_pending = False
def _ms_left_press(self, key, keyboard, *args, **kwargs): def _ms_left_press(self, key, keyboard, *args, **kwargs):
self._nav_key_activated += 1 self._nav_key_activated += 1
self._reset_next_interval() self._reset_next_interval()
self._left_activated = True self._left_activated = True
self.pointing_device.report_x[0] = 0xFF & (0 - self.move_step)
self.pointing_device.hid_pending = True
def _ms_left_release(self, key, keyboard, *args, **kwargs): def _ms_left_release(self, key, keyboard, *args, **kwargs):
self._nav_key_activated -= 1 self._nav_key_activated -= 1
self._left_activated = False self._left_activated = False
self._check_last() self._check_last()
self.pointing_device.report_x[0] = 0
self.pointing_device.hid_pending = False
def _ms_right_press(self, key, keyboard, *args, **kwargs): def _ms_right_press(self, key, keyboard, *args, **kwargs):
self._nav_key_activated += 1 self._nav_key_activated += 1
self._reset_next_interval() self._reset_next_interval()
self._right_activated = True self._right_activated = True
self.pointing_device.report_x[0] = self.move_step
self.pointing_device.hid_pending = True
def _ms_right_release(self, key, keyboard, *args, **kwargs): def _ms_right_release(self, key, keyboard, *args, **kwargs):
self._nav_key_activated -= 1 self._nav_key_activated -= 1
self._right_activated = False self._right_activated = False
self._check_last() self._check_last()
self.pointing_device.report_x[0] = 0
self.pointing_device.hid_pending = False