Skip to content

Commit 3e8e733

Browse files
committedAug 16, 2023
reworked modifier latching
1 parent 1bbcc41 commit 3e8e733

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed
 

‎blehid.py

+21-20
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,7 @@
109109
("DOWNARROW", 0x51),
110110
("UPARROW", 0x52),
111111
("APP", 0x65), # Keyboard Application
112-
("LCTRL", 0xE0), # Keyboard Left Control
113-
("LSHIFT", 0xE1), # Keyboard Left Shift
114-
("LALT", 0xE2), # Keyboard Left Alt
115112
("LGUI", 0xE3), # Keyboard Left GUI
116-
("RCTRL", 0xE4), # Keyboard Right Control
117-
("RSHIFT", 0xE5), # Keyboard Right Shift
118-
("RALT", 0xE6), # Keyboard Right Alt
119113
("RGUI", 0xE7), # Keyboard Right GUI
120114
("CUSTOM~", 0x32), # Keyboard Non-US # and ~
121115
("NONUSHASH", 0x32), # Alias
@@ -231,27 +225,30 @@ async def blehid_send_mousebutton(ser, btn, behavior=None):
231225

232226

233227
async def blehid_send_keyboardcode(ser, key, modifiers, down, keys):
234-
logging.debug('key:'+str(key)+' modifiers:'+str(modifiers))
235-
existingModifiers = list(map(
236-
lambda a: a[0],
237-
filter(lambda a: a[1] in keys,
238-
[("LCTRL", 0xe0), ("LSHIFT", 0xe1), ("LALT", 0xe2),
239-
("LMETA", 0xe3), ("RCTRL", 0xe4), ("RSHIFT", 0xe5),
240-
("RALT", 0xe6), ("RMETA", 0xe7)])))
241-
modifiers=list(set(modifiers+existingModifiers))
242-
logging.debug('existing modifiers:'+str(existingModifiers))
228+
if key in ["LCTRL", "LSHIFT", "LALT", "LMETA", "RCTRL", "RSHIFT", "RALT", "RMETA"]:
229+
modifiers.append(key)
230+
key = None
231+
232+
logging.debug('key:'+str(key)+' modifiers:'+str(modifiers)+' action:'+str(down))
233+
234+
# modifier handling
243235
hidmod = reduce(operator.or_, map(
244236
lambda a: a[1],
245237
filter(lambda a: a[0] in modifiers,
246238
[("LCTRL", 0x01), ("LSHIFT", 0x02), ("LALT", 0x04),
247239
("LMETA", 0x08), ("RCTRL", 0x10), ("RSHIFT", 0x20),
248240
("RALT", 0x40), ("RMETA", 0x80)])), 0)
241+
if down == True:
242+
keys[0] = keys[0] | hidmod
243+
else:
244+
keys[0] = keys[0] & (~hidmod)
245+
246+
# key handling
249247
keycode = keymap.get(key, 0)
250248
# if OS == 'ios' and keycode == 13:
251-
# hidcode = 0x58
252-
logging.debug("keycode: {:02x}, mod: {:02x}".format(keycode, hidmod))
249+
# hidcode = 0x58
253250
if key != 0:
254-
for i in range(0, 6):
251+
for i in range(2, 8):
255252
if keys[i] == 0:
256253
if down == True:
257254
keys[i] = keycode
@@ -261,9 +258,13 @@ async def blehid_send_keyboardcode(ser, key, modifiers, down, keys):
261258
keys[i] = 0
262259
else:
263260
break
264-
atcmd = "AT+BLEKEYBOARDCODE={:02x}-00".format(hidmod)
261+
262+
# preparing command
263+
logging.debug("keycode: {:02x}, mod: {:02x}".format(keycode, keys[0]))
264+
265+
atcmd = "AT+BLEKEYBOARDCODE={:02x}-00".format(keys[0])
265266
zerocmd = ""
266-
for i in range(0, 6):
267+
for i in range(2, 8):
267268
if keys[i] != 0:
268269
atcmd += "-{:02x}".format(keys[i])
269270
else:

‎relaykeys-cli.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ def type_char (char):
258258
else:
259259
type_char("\\")
260260
type_char(char)
261-
do_keyevent(client, None, [], False)
262261
elif name == "paste":
263262
data = pyperclip.paste()
264263
for char in data:
@@ -270,17 +269,21 @@ def type_char (char):
270269
do_keyevent(client, key, mods, False)
271270
if delay > 0:
272271
sleep(delay/1000.0)
273-
do_keyevent(client, None, [], False)
274272
elif name == "keyevent":
275273
parts = data.split(",")
276274
if len(parts) < 2:
277275
raise ValueError("Not enough params for keyevent command: {}".format(cmd))
276+
278277
key = parts[0]
278+
if len(parts) == 2:
279+
modifiers = None
280+
elif len(parts) > 2:
281+
modifiers = parts[1:-1]
279282
try:
280283
isdown = int(parts[-1]) == 1
281284
except ValueError:
282285
raise ValueError("Last param of keyevent command should be one of (0,1), in cmd: {}".format(cmd))
283-
modifiers = parts[1:]
286+
284287
do_keyevent(client, key, modifiers, isdown)
285288
if delay > 0:
286289
sleep(delay/1000.0)

‎relaykeys-qt.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,8 @@ def __init__(self, args, config):
743743
helpMenuAceCentre.triggered.connect(self.openAceCentreUrl)
744744
helpMenu.addAction(helpMenuAceCentre)
745745

746+
self.send_action('ble_cmd', 'keyboard_release')
747+
746748
self.send_action('ble_cmd', 'devname')
747749
self.send_action('ble_cmd', 'devlist')
748750

@@ -1018,9 +1020,13 @@ def onQuit(self):
10181020
"""
10191021

10201022
def closeEvent(self, event):
1021-
self._client_queue.put(("EXIT",))
10221023
self._keyboard_listener.stop()
10231024
self._mouse_listener.stop()
1025+
sleep(0.2)
1026+
self.send_action('ble_cmd', 'keyboard_release')
1027+
sleep(0.2)
1028+
self._client_queue.put(("EXIT",))
1029+
10241030

10251031
def initHooks(self):
10261032
self._keyboard_listener = keyboard.Listener(on_press=self.onKeyboardDown, on_release=self.onKeyboardUp)
@@ -1185,6 +1191,7 @@ def checkShortcutTrigger(self, key, mods, tkey, tmods):
11851191

11861192
def _keyboardToggleCheck(self, key):
11871193
if self.checkShortcutTrigger(key, self._modifiers, self._keyboard_toggle_key, self._keyboard_toggle_modifiers):
1194+
self.send_action('ble_cmd', 'keyboard_release')
11881195
self._keyboard_disabled = not self._keyboard_disabled
11891196
self.keyboardStatusWidget.updateStatusSignal.emit([], [], [])
11901197
self.updateTogglesStatus()
@@ -1240,10 +1247,10 @@ def onKeyboardDown(self, key_ev):
12401247
self._last_n_chars.pop(0)
12411248
self._last_n_chars.append(chr)
12421249
self.updateShowLastChars()
1243-
self.send_action('keyevent', key, self._modifiers, True)
1250+
self.send_action('keyevent', key, [], True)
12441251
elif mod is not None:
12451252
# set the modifiers
1246-
self.send_action('keyevent', None, self._modifiers, False)
1253+
self.send_action('keyevent', None, [mod], True)
12471254

12481255
def onKeyboardUp(self, key_ev):
12491256
key = None
@@ -1267,10 +1274,10 @@ def onKeyboardUp(self, key_ev):
12671274
return
12681275
self.updateKeyboardState()
12691276
if key is not None:
1270-
self.send_action('keyevent', key, self._modifiers, False)
1277+
self.send_action('keyevent', key, [], False)
12711278
elif mod is not None:
12721279
# set the modifiers
1273-
self.send_action('keyevent', None, self._modifiers, False)
1280+
self.send_action('keyevent', None, [mod], False)
12741281

12751282
def mouse_on_move(self, x, y):
12761283
if not self._mouse_disabled:
@@ -1418,6 +1425,8 @@ def executeMacroBuffer(self):
14181425
continue
14191426

14201427
sleep(0.05)
1428+
1429+
self.send_action('ble_cmd', 'keyboard_release')
14211430

14221431

14231432

‎relaykeysd.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,8 @@ async def hardware_serial_loop(queue, args, config, interrupt):
417417

418418
#logging.info("INIT MSG: {}".format(str(ser.readline(), "utf8")))
419419
await blehid_init_serial(ser)
420-
# Six keys for USB keyboard HID report
421-
# uint8_t keys[6] = {0,0,0,0,0,0}
422-
keys = arr.array('B', [0, 0, 0, 0, 0, 0])
420+
421+
keys = arr.array('B', [0, 0, 0, 0, 0, 0, 0, 0])
423422

424423
#Get intial ble device List
425424
await process_action(ser, keys, ['ble_cmd','devlist'])
@@ -488,7 +487,7 @@ def handle_disconnect(_: BleakClient):
488487
ser = BLESerialWrapper(client)
489488
await ser.init_receive()
490489

491-
keys = arr.array('B', [0, 0, 0, 0, 0, 0])
490+
keys = arr.array('B', [0, 0, 0, 0, 0, 0, 0, 0])
492491

493492
print("Device connected.")
494493
serial_loop_opened = True
@@ -591,6 +590,12 @@ async def process_action(ser, keys, cmd):
591590

592591
elif cmd[1] == "switch_mode":
593592
await blehid_switch_mode(ser)
593+
594+
elif cmd[1] == "keyboard_release":
595+
print("keyboard relase")
596+
for i in range(0, 8):
597+
keys[i] = 0
598+
await blehid_send_keyboardcode(ser, None, [], False, keys)
594599

595600
elif cmd[0] == "check_dongle":
596601
return await blehid_get_at_response(ser)

0 commit comments

Comments
 (0)
Please sign in to comment.