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 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
@ -24,31 +24,29 @@ class PointingDevice:
class MouseKeys(Module):
def __init__(self):
self.pointing_device = PointingDevice()
self._nav_key_activated = 0
self._up_activated = False
self._down_activated = False
self._left_activated = False
self._right_activated = False
self._mw_up_activated = False
self._mw_down_activated = False
self.max_speed = 10
self.ac_interval = 100 # Delta ms to apply acceleration
self._next_interval = 0 # Time for next tick interval
self.move_step = 1
make_key(
make_mouse_key(
names=('MB_LMB',),
on_press=self._mb_lmb_press,
on_release=self._mb_lmb_release,
code=1,
)
make_key(
make_mouse_key(
names=('MB_MMB',),
on_press=self._mb_mmb_press,
on_release=self._mb_mmb_release,
code=4,
)
make_key(
make_mouse_key(
names=('MB_RMB',),
on_press=self._mb_rmb_press,
on_release=self._mb_rmb_release,
code=2,
)
make_key(
names=('MW_UP',),
@ -94,38 +92,40 @@ class MouseKeys(Module):
)
def during_bootup(self, keyboard):
return
def matrix_detected_press(self, keyboard):
return keyboard.matrix_update is None
keyboard.axes['W'] = Axis(2)
keyboard.axes['X'] = Axis(0)
keyboard.axes['Y'] = Axis(1)
def before_matrix_scan(self, keyboard):
return
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._next_interval <= ticks_ms():
# print("hello: ")
# 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.move_step < self.max_speed:
self.move_step = self.move_step + 1
if self._right_activated:
self.pointing_device.report_x[0] = self.move_step
keyboard.axes['X'].delta += self.move_step
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:
self.pointing_device.report_y[0] = 0xFF & (0 - self.move_step)
keyboard.axes['Y'].delta -= self.move_step
if self._down_activated:
self.pointing_device.report_y[0] = self.move_step
self.pointing_device.hid_pending = True
return
keyboard.axes['Y'].delta += self.move_step
keyboard.hid_pending = True
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):
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
def after_hid_send(self, keyboard):
@ -137,45 +137,17 @@ class MouseKeys(Module):
def on_powersave_disable(self, keyboard):
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):
self.pointing_device.report_w[0] = self.move_step
self.pointing_device.hid_pending = True
self._mw_up_activated = True
def _mw_up_release(self, key, keyboard, *args, **kwargs):
self.pointing_device.report_w[0] = 0
self.pointing_device.hid_pending = True
self._mw_up_activated = False
def _mw_down_press(self, key, keyboard, *args, **kwargs):
self.pointing_device.report_w[0] = 0xFF
self.pointing_device.hid_pending = True
self._mw_down_activated = True
def _mw_down_release(self, key, keyboard, *args, **kwargs):
self.pointing_device.report_w[0] = 0
self.pointing_device.hid_pending = True
self._mw_down_activated = False
# Mouse movement
def _reset_next_interval(self):
@ -191,56 +163,38 @@ class MouseKeys(Module):
self._nav_key_activated += 1
self._reset_next_interval()
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):
self._up_activated = False
self._nav_key_activated -= 1
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):
self._nav_key_activated += 1
self._reset_next_interval()
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):
self._down_activated = False
self._nav_key_activated -= 1
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):
self._nav_key_activated += 1
self._reset_next_interval()
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):
self._nav_key_activated -= 1
self._left_activated = False
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):
self._nav_key_activated += 1
self._reset_next_interval()
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):
self._nav_key_activated -= 1
self._right_activated = False
self._check_last()
self.pointing_device.report_x[0] = 0
self.pointing_device.hid_pending = False