zmkfirmware / zmk

ZMK Firmware Repository
https://zmk.dev/
MIT License
2.83k stars 2.86k forks source link

[Docs] Resource for diagnosing keypress timings #1968

Open felix-hilden opened 1 year ago

felix-hilden commented 1 year ago

Hi, and thanks for ZMK! It's been awesome to use and the latest addition of idle wait to holds was sweet ❤️

While stumbling to get my config set up I didn't really know how I should set my hold and toggle timings. 200 ms seemed common, sure. It was first guesswork and then tweaking. But I thought a tool for actually seeing my own timings while writing might help me to decide on what's best. And I'd like to share it with you in hopes that it will help others, perhaps in the documentation.

It's a short Python script with no dependencies that spawns an empty window and records events:

from tkinter import Tk
from time import perf_counter

times = {}
current = perf_counter()
print("Directn. Sym        C PDly RDly Duration")

def key_pressed(e):
    if e.char in times:
        return
    global current
    times[e.char] = t = perf_counter()
    elapsed = round((t - current) * 1000)
    print(f"Pressed: {e.keysym:10} {e.char:1} {elapsed:4}")
    current = t

def key_released(e):
    global current
    t = perf_counter()
    down = times.pop(e.char, None)
    if not down:
        return
    elapsed = round((t - current) * 1000)
    since_down = round((t - down) * 1000)
    print(f"Release: {e.keysym:10} {e.char:1}      {elapsed:4} {since_down:4}")
    current = t

root = Tk()
root.bind("<Key>", key_pressed)
root.bind("<KeyRelease>", key_released)
root.mainloop()

Sample output:

Directn. Sym        C PDly RDly Duration
Pressed: h          h 1030
Release: h          h        60   60
Pressed: e          e   44
Release: e          e        55   55
Pressed: l          l  186
Release: l          l         2    2
Pressed: l          l   73
Release: l          l        90   90
Pressed: o          o  585
Release: o          o        75   75
Pressed: space         342
Release: space                2    2
Pressed: Shift_R       856
Pressed: W          W   48
Release: W          W        22   22
Release: Shift_L             80  150
Pressed: o          o  195
Release: o          o        75   75
Pressed: r          r 1158
Release: r          r        76   76
Pressed: l          l  221
Release: l          l         2    2
Pressed: d          d  748
Release: d          d         3    3
Pressed: Shift_L       514
Pressed: exclam     !  103
Release: exclam     !         2    2
Release: Shift_L             67  172

I'd be happy to make a PR if you think this is worth doing. If not (or not here), feel free to close the issue right away! All the best anyway and thank you for your work.

manna-harbour commented 1 year ago

Also see 𝑥MK, which can record and play back timed keystrokes, and can be used to test keymap changes, compare behaviour between QMK, ZMK, and KMK, or debug firmware timing issues.