Skip to content

Commit

Permalink
Merge pull request #146 from f1andrew/master
Browse files Browse the repository at this point in the history
reworked modifier latching

Thanks again @f1andrew 👍
  • Loading branch information
willwade authored Aug 16, 2023
2 parents 9b24661 + 3e8e733 commit c34124e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
41 changes: 21 additions & 20 deletions blehid.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,7 @@
("DOWNARROW", 0x51),
("UPARROW", 0x52),
("APP", 0x65), # Keyboard Application
("LCTRL", 0xE0), # Keyboard Left Control
("LSHIFT", 0xE1), # Keyboard Left Shift
("LALT", 0xE2), # Keyboard Left Alt
("LGUI", 0xE3), # Keyboard Left GUI
("RCTRL", 0xE4), # Keyboard Right Control
("RSHIFT", 0xE5), # Keyboard Right Shift
("RALT", 0xE6), # Keyboard Right Alt
("RGUI", 0xE7), # Keyboard Right GUI
("CUSTOM~", 0x32), # Keyboard Non-US # and ~
("NONUSHASH", 0x32), # Alias
Expand Down Expand Up @@ -231,27 +225,30 @@ async def blehid_send_mousebutton(ser, btn, behavior=None):


async def blehid_send_keyboardcode(ser, key, modifiers, down, keys):
logging.debug('key:'+str(key)+' modifiers:'+str(modifiers))
existingModifiers = list(map(
lambda a: a[0],
filter(lambda a: a[1] in keys,
[("LCTRL", 0xe0), ("LSHIFT", 0xe1), ("LALT", 0xe2),
("LMETA", 0xe3), ("RCTRL", 0xe4), ("RSHIFT", 0xe5),
("RALT", 0xe6), ("RMETA", 0xe7)])))
modifiers=list(set(modifiers+existingModifiers))
logging.debug('existing modifiers:'+str(existingModifiers))
if key in ["LCTRL", "LSHIFT", "LALT", "LMETA", "RCTRL", "RSHIFT", "RALT", "RMETA"]:
modifiers.append(key)
key = None

logging.debug('key:'+str(key)+' modifiers:'+str(modifiers)+' action:'+str(down))

# modifier handling
hidmod = reduce(operator.or_, map(
lambda a: a[1],
filter(lambda a: a[0] in modifiers,
[("LCTRL", 0x01), ("LSHIFT", 0x02), ("LALT", 0x04),
("LMETA", 0x08), ("RCTRL", 0x10), ("RSHIFT", 0x20),
("RALT", 0x40), ("RMETA", 0x80)])), 0)
if down == True:
keys[0] = keys[0] | hidmod
else:
keys[0] = keys[0] & (~hidmod)

# key handling
keycode = keymap.get(key, 0)
# if OS == 'ios' and keycode == 13:
# hidcode = 0x58
logging.debug("keycode: {:02x}, mod: {:02x}".format(keycode, hidmod))
# hidcode = 0x58
if key != 0:
for i in range(0, 6):
for i in range(2, 8):
if keys[i] == 0:
if down == True:
keys[i] = keycode
Expand All @@ -261,9 +258,13 @@ async def blehid_send_keyboardcode(ser, key, modifiers, down, keys):
keys[i] = 0
else:
break
atcmd = "AT+BLEKEYBOARDCODE={:02x}-00".format(hidmod)

# preparing command
logging.debug("keycode: {:02x}, mod: {:02x}".format(keycode, keys[0]))

atcmd = "AT+BLEKEYBOARDCODE={:02x}-00".format(keys[0])
zerocmd = ""
for i in range(0, 6):
for i in range(2, 8):
if keys[i] != 0:
atcmd += "-{:02x}".format(keys[i])
else:
Expand Down
9 changes: 6 additions & 3 deletions relaykeys-cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ def type_char (char):
else:
type_char("\\")
type_char(char)
do_keyevent(client, None, [], False)
elif name == "paste":
data = pyperclip.paste()
for char in data:
Expand All @@ -270,17 +269,21 @@ def type_char (char):
do_keyevent(client, key, mods, False)
if delay > 0:
sleep(delay/1000.0)
do_keyevent(client, None, [], False)
elif name == "keyevent":
parts = data.split(",")
if len(parts) < 2:
raise ValueError("Not enough params for keyevent command: {}".format(cmd))

key = parts[0]
if len(parts) == 2:
modifiers = None
elif len(parts) > 2:
modifiers = parts[1:-1]
try:
isdown = int(parts[-1]) == 1
except ValueError:
raise ValueError("Last param of keyevent command should be one of (0,1), in cmd: {}".format(cmd))
modifiers = parts[1:]

do_keyevent(client, key, modifiers, isdown)
if delay > 0:
sleep(delay/1000.0)
Expand Down
19 changes: 14 additions & 5 deletions relaykeys-qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,8 @@ def __init__(self, args, config):
helpMenuAceCentre.triggered.connect(self.openAceCentreUrl)
helpMenu.addAction(helpMenuAceCentre)

self.send_action('ble_cmd', 'keyboard_release')

self.send_action('ble_cmd', 'devname')
self.send_action('ble_cmd', 'devlist')

Expand Down Expand Up @@ -1018,9 +1020,13 @@ def onQuit(self):
"""

def closeEvent(self, event):
self._client_queue.put(("EXIT",))
self._keyboard_listener.stop()
self._mouse_listener.stop()
sleep(0.2)
self.send_action('ble_cmd', 'keyboard_release')
sleep(0.2)
self._client_queue.put(("EXIT",))


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

def _keyboardToggleCheck(self, key):
if self.checkShortcutTrigger(key, self._modifiers, self._keyboard_toggle_key, self._keyboard_toggle_modifiers):
self.send_action('ble_cmd', 'keyboard_release')
self._keyboard_disabled = not self._keyboard_disabled
self.keyboardStatusWidget.updateStatusSignal.emit([], [], [])
self.updateTogglesStatus()
Expand Down Expand Up @@ -1240,10 +1247,10 @@ def onKeyboardDown(self, key_ev):
self._last_n_chars.pop(0)
self._last_n_chars.append(chr)
self.updateShowLastChars()
self.send_action('keyevent', key, self._modifiers, True)
self.send_action('keyevent', key, [], True)
elif mod is not None:
# set the modifiers
self.send_action('keyevent', None, self._modifiers, False)
self.send_action('keyevent', None, [mod], True)

def onKeyboardUp(self, key_ev):
key = None
Expand All @@ -1267,10 +1274,10 @@ def onKeyboardUp(self, key_ev):
return
self.updateKeyboardState()
if key is not None:
self.send_action('keyevent', key, self._modifiers, False)
self.send_action('keyevent', key, [], False)
elif mod is not None:
# set the modifiers
self.send_action('keyevent', None, self._modifiers, False)
self.send_action('keyevent', None, [mod], False)

def mouse_on_move(self, x, y):
if not self._mouse_disabled:
Expand Down Expand Up @@ -1418,6 +1425,8 @@ def executeMacroBuffer(self):
continue

sleep(0.05)

self.send_action('ble_cmd', 'keyboard_release')



Expand Down
13 changes: 9 additions & 4 deletions relaykeysd.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,8 @@ async def hardware_serial_loop(queue, args, config, interrupt):

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

keys = arr.array('B', [0, 0, 0, 0, 0, 0, 0, 0])

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

keys = arr.array('B', [0, 0, 0, 0, 0, 0])
keys = arr.array('B', [0, 0, 0, 0, 0, 0, 0, 0])

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

elif cmd[1] == "switch_mode":
await blehid_switch_mode(ser)

elif cmd[1] == "keyboard_release":
print("keyboard relase")
for i in range(0, 8):
keys[i] = 0
await blehid_send_keyboardcode(ser, None, [], False, keys)

elif cmd[0] == "check_dongle":
return await blehid_get_at_response(ser)
Expand Down

0 comments on commit c34124e

Please sign in to comment.