Skip to content

Commit e39c618

Browse files
committed
fix if a key is pressed but not released before pressing a tap-key
1 parent 8d184d9 commit e39c618

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

keyboard/__init__.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def reset_into_bootloader():
8282
microcontroller.reset()
8383

8484

85-
8685
class Device:
8786
def __init__(self, kbd):
8887
self.kbd = kbd
@@ -248,21 +247,38 @@ def stop_advertising(self):
248247
except Exception as e:
249248
print(e)
250249

251-
def is_tap_key(self, matrix, key):
250+
def is_tapping_key(self, key):
252251
"""Check if the key is tapped (press & release quickly)"""
252+
matrix = self.matrix
253253
n = len(matrix)
254254
if n == 0:
255-
n = matrix.wait(self.tap_delay - matrix.ms(matrix.time() - matrix.get_keydown_time(key)))
255+
n = matrix.wait(
256+
self.tap_delay - matrix.ms(matrix.time() - matrix.get_keydown_time(key))
257+
)
256258
target = key | 0x80
257-
if n == 1:
258-
if target == matrix.view(0):
259+
if n >= 1:
260+
new_key = matrix.view(0)
261+
if new_key == target:
259262
return True
260-
else:
263+
if new_key >= 0x80:
264+
# Fast Typing - B is a tap-key
265+
# A↓ B↓ A↑ B↑
266+
# --+-------+-------+-------+------> t
267+
# | dt1 |
268+
# dt1 < tap_delay
269+
return True
270+
271+
if n == 1:
261272
n = matrix.wait(
262-
self.fast_type_thresh - matrix.ms(matrix.time() - matrix.get_keydown_time(key))
273+
self.fast_type_thresh
274+
- matrix.ms(matrix.time() - matrix.get_keydown_time(new_key))
263275
)
264-
if n == 2 and target == matrix.view(1):
265-
# Fast typing: A down, B down, A up, B up
276+
if n >= 2 and target == matrix.view(1):
277+
# Fast Typing - B is a tap-key
278+
# B↓ C↓ B↑ C↑
279+
# --+-------+-------+-------+------> t
280+
# | dt1 | dt2 |
281+
# dt1 < tap_delay && dt2 < fast_type_thresh
266282
return True
267283

268284
return False
@@ -399,7 +415,8 @@ def run(self):
399415
key = matrix.view(0)
400416
if key < 0x80 and key in self.pair_keys:
401417
n = matrix.wait(
402-
self.pair_delay - ms(matrix.time() - matrix.get_keydown_time(key))
418+
self.pair_delay
419+
- ms(matrix.time() - matrix.get_keydown_time(key))
403420
)
404421

405422
if n >= 2:
@@ -441,7 +458,7 @@ def run(self):
441458
self.press(*keycodes)
442459
elif kind < ACT_USAGE:
443460
# MODS_TAP
444-
if self.is_tap_key(matrix, key):
461+
if self.is_tapping_key(key):
445462
log("TAP")
446463
keycode = action_code & 0xFF
447464
keys[key] = keycode
@@ -465,7 +482,7 @@ def run(self):
465482
keycodes = mods_to_keycodes(mods)
466483
self.press(*keycodes)
467484
self.layer_mask |= mask
468-
elif self.is_tap_key(matrix, key):
485+
elif self.is_tapping_key(key):
469486
log("TAP")
470487
keycode = action_code & 0xFF
471488
if keycode == OP_TAP_TOGGLE:

0 commit comments

Comments
 (0)