My C key types twice, so I taught Linux to ignore the second one
TL;DR: The C key on my Akko keyboard started typing
ccon a single press. That is called key chatter: a worn or dirty switch makes contact, bounces, and makes contact again a few milliseconds later. I wrote a small Python service that grabs the keyboard at the evdev level, forwards every event to a virtual keyboard through uinput, and drops a press of a chattering key if it comes less than 50 ms after that key's last release. It works everywhere, in every app, X11 or Wayland. After a week of logs it had blocked 279 phantom C presses, and showed me that O, E, X and L are going the same way. Code is on GitHub.
The problem
This is a sentence I typed while describing the problem, with the bad key doing its thing:
sdf cc dfsfs c dsfdsfdsfsfdsf c sdfsfs cc dfsfds c ...
Every c there is one press. Some of them came out twice. Not every time, just often enough to be in almost every line I wrote.
A keyboard switch is two bits of metal that touch when you press. When the switch is worn or there is dirt inside, the contact is not clean. It closes, opens for a moment, and closes again. The keyboard's firmware is supposed to filter that out (this is called debouncing), but when the bounce is longer than the firmware's window, the computer sees two separate presses.
The real fix is hardware: clean the switch, or replace it. Until then I wanted the computer to ignore the second press, everywhere, not just in one editor.
Where to put the filter
On Linux, keyboard input travels like this:
keyboard -> kernel -> /dev/input/eventN (evdev) -> X11 or Wayland -> apps
If I filter in X11, it does not work on Wayland or in the TTY. If I filter in an app, it only works in that app. The one layer that everything goes through is evdev, so that is where the filter goes.
Linux makes this possible with two pieces:
- An exclusive grab. A program can open
/dev/input/eventNand callEVIOCGRAB. After that, nobody else gets events from the real keyboard, not even the display server. - uinput. A program can create a virtual input device. To the rest of the system it looks like any other keyboard.
So the program grabs the real keyboard, creates a virtual one, and copies every event from one to the other. Except the ones it decides to drop.
What chatter looks like
From the kernel's point of view, chatter is a key that goes press, release, press, release, with a very short gap between the first release and the second press. A real double press, like the cc in "access", has the same shape, just slower.
So the rule is: remember when each key was last released. If the same key goes down again less than N ms later, drop that press, and also drop the release that comes with it, so no app ever sees a key stuck down.
The core of it, using python-evdev:
if ev.value == 1: # key down
lu = last_up.get(ev.code)
if lu is not None and (t - lu) * 1000 < THRESHOLD_MS:
suppressing.add(ev.code)
continue # drop the chatter press
elif ev.value == 0: # key up
if ev.code in suppressing:
suppressing.discard(ev.code)
continue # drop its release too
last_up[ev.code] = t
ui.write_event(ev) # everything else goes through
t is the kernel's timestamp on the event, not the time my script happened to read it, so a slow moment in Python does not change the measurement.
Picking the number
The first test run had a 40 ms threshold and a 600 second timeout. The timeout matters: a script holding an exclusive grab on your only keyboard is a script that can lock you out if it hangs. With a timer, the worst case is a few minutes without a keyboard.
It dropped four presses in that test: 18, 28, 37 and 9 ms.
37 is close to 40, which means some chatter could be landing just over the line. A human pressing the same key twice does not come anywhere near that speed, so I moved the threshold to 50 ms. Later data confirmed it was safe (more on that below).
Making it permanent
A systemd service runs it at boot, as root, because it needs /dev/uinput and the grab:
[Service]
Type=simple
ExecStart=/usr/bin/python3 -u /usr/local/lib/key-chatter-fix/filter.py 50 0 cqelox
Restart=always
RestartSec=3
The arguments are the threshold in ms, the run time (0 is forever) and the letters to filter.
A few things I had to handle:
- The device path changes between boots.
/dev/input/event7today can beevent9tomorrow. The script uses the stable/dev/input/by-id/...-event-kbdpath instead. - Unplugging the keyboard. The read fails, the script exits, and systemd restarts it. If the keyboard is not there yet, the script waits for it and logs that once, instead of crashing every 3 seconds and filling the journal.
- Wireless mode. My keyboard also does 2.4G and Bluetooth, and in those modes it is a different device. The service just waits until I am back on USB.
- The script lives in
/usr/local/lib, not my home folder. A root service running a file I can edit as a normal user is a privilege escalation path. The install script copies it into a root-owned place.
Logging, so I could check later
I did not want to guess whether it was working. So the script logs three kinds of lines to the journal:
DROPPED c chatter: 18.0 msfor a press it blocked.passed c fast press: 92.0 msfor a fast press on a filtered key that it let through.watched o fast press: 21.0 msfor any other letter pressed twice within 150 ms.
That third one turned out to be the useful one. A few days in, Q and E started acting up too, so I made the script watch all 26 letters, not only the ones it filters. Unfiltered keys are only logged, never blocked.
A week of data
After a week, the numbers sorted into a clear picture. Everything under 30 ms is almost certainly chatter, because a finger cannot release and press the same key that fast. Here is that count per day:
| Day | c | o | e | x | l |
|---|---|---|---|---|---|
| 21/11 | 24 | 2 | 0 | 0 | 1 |
| 22/11 | 46 | 7 | 7 | 2 | 4 |
| 23/11 | 45 | 0 | 1 | 0 | 0 |
| 24/11 | 15 | 1 | 1 | 1 | 0 |
| 25/11 | 7 | 2 | 2 | 1 | 0 |
| 26/11 | 22 | 5 | 4 | 1 | 0 |
| 27/11 | 44 | 13 | 5 | 3 | 2 |
| 28/11 | 22 | 8 | 0 | 3 | 0 |
C and E were filtered the whole time. O, X and L were not until the evening of 28/11, so before that every one of those reached my screen as a doubled letter.
A few things stood out:
- C got 279 presses blocked in total. 225 of them under 30 ms, the rest between 30 and 50.
- Not one real double press was under 50 ms. 554 fast C presses were let through, and every single one was 50 ms or slower. Same for E. So 50 ms is not eating real typing.
- O is getting worse. 13 in one day on 27/11 and 8 on 28/11, after mostly low single digits before that.
- X is the clearest case. 9 of its 16 fast presses were under 30 ms. Nobody types "xx".
- Q, which I had added because it seemed flaky, was clean. 4 fast presses all week, all over 80 ms.
Some keys, like G, R and D, had a lot of presses between 30 and 50 ms but almost none under 30. For G that is most likely gg in vim. That is fast typing, not chatter, and it is exactly why I did not just filter every key.
So the list went from cqe to cqelox. That went live at 17:17 on 28/11, and in its first few hours it had already dropped 2 chatter presses on O, 2 on X and 3 on L.
"Isn't this a keylogger?"
When I put it on GitHub, I realised what the repo looked like from the outside: a root service that grabs the keyboard and writes to a log. That is also a description of a keylogger.
So the README starts with what the program does with your keystrokes:
- It does not store or send what you type. Each event is forwarded and forgotten. The only thing kept in memory is the time each letter was last released.
- It makes no network connections. The imports are
os,select,sys,timeandevdev, and that is all. - It only logs when the same letter is pressed twice within 150 ms. Normal typing, where letters are different from one to the next, never appears in the log, so text cannot be rebuilt from it.
- The whole filter is about 80 lines, and the README asks you to read it before installing.
That last point is the real answer. You should not run a root service that reads your keyboard because a README says it is fine. You should run it because you read the 80 lines.
Be honest about what this is
This is a workaround. Five switches showing chatter in one week does not look like one bad switch, it looks like the whole keyboard, either dirt or wear. The service buys time, and the logs tell me which keys are next, but the fix is compressed air and isopropyl alcohol, or new switches.
Until then, my C key presses once.
The code is at github.com/2u841r/key-chatter-fix.