Skip to content

Commit 8d184d9

Browse files
authored
Merge pull request #7 from mehalter/main
Added ability to adjust tap and pair key delays
2 parents ed762e7 + 6ecf6b7 commit 8d184d9

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

keyboard/__init__.py

+25-22
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,6 @@ def reset_into_bootloader():
8282
microcontroller.reset()
8383

8484

85-
def is_tapped(matrix, key):
86-
"""Check if the key is tapped (press & release quickly)"""
87-
n = len(matrix)
88-
if n == 0:
89-
n = matrix.wait(500 - matrix.ms(matrix.time() - matrix.get_keydown_time(key)))
90-
target = key | 0x80
91-
if n == 1:
92-
if target == matrix.view(0):
93-
return True
94-
else:
95-
n = matrix.wait(
96-
200 - matrix.ms(matrix.time() - matrix.get_keydown_time(key))
97-
)
98-
if n == 2 and target == matrix.view(1):
99-
# Fast typing: A down, B down, A up, B up
100-
return True
101-
102-
return False
103-
10485

10586
class Device:
10687
def __init__(self, kbd):
@@ -157,6 +138,9 @@ def __init__(self, keymap=KEYMAP, pairs=(), verbose=True):
157138
self.uid = microcontroller.cpu.uid * 2
158139
self.usb_status = 0
159140
self.leds = None
141+
self.tap_delay = 500
142+
self.fast_type_thresh = 200
143+
self.pair_delay = 10
160144

161145
self._current_conn = ""
162146

@@ -264,6 +248,25 @@ def stop_advertising(self):
264248
except Exception as e:
265249
print(e)
266250

251+
def is_tap_key(self, matrix, key):
252+
"""Check if the key is tapped (press & release quickly)"""
253+
n = len(matrix)
254+
if n == 0:
255+
n = matrix.wait(self.tap_delay - matrix.ms(matrix.time() - matrix.get_keydown_time(key)))
256+
target = key | 0x80
257+
if n == 1:
258+
if target == matrix.view(0):
259+
return True
260+
else:
261+
n = matrix.wait(
262+
self.fast_type_thresh - matrix.ms(matrix.time() - matrix.get_keydown_time(key))
263+
)
264+
if n == 2 and target == matrix.view(1):
265+
# Fast typing: A down, B down, A up, B up
266+
return True
267+
268+
return False
269+
267270
def change_bt(self, n):
268271
if self.ble.connected:
269272
for c in self.ble.connections:
@@ -396,7 +399,7 @@ def run(self):
396399
key = matrix.view(0)
397400
if key < 0x80 and key in self.pair_keys:
398401
n = matrix.wait(
399-
10 - ms(matrix.time() - matrix.get_keydown_time(key))
402+
self.pair_delay - ms(matrix.time() - matrix.get_keydown_time(key))
400403
)
401404

402405
if n >= 2:
@@ -438,7 +441,7 @@ def run(self):
438441
self.press(*keycodes)
439442
elif kind < ACT_USAGE:
440443
# MODS_TAP
441-
if is_tapped(matrix, key):
444+
if self.is_tap_key(matrix, key):
442445
log("TAP")
443446
keycode = action_code & 0xFF
444447
keys[key] = keycode
@@ -462,7 +465,7 @@ def run(self):
462465
keycodes = mods_to_keycodes(mods)
463466
self.press(*keycodes)
464467
self.layer_mask |= mask
465-
elif is_tapped(matrix, key):
468+
elif self.is_tap_key(matrix, key):
466469
log("TAP")
467470
keycode = action_code & 0xFF
468471
if keycode == OP_TAP_TOGGLE:

0 commit comments

Comments
 (0)