Fixing a periodically stuck AlrGr key on my mechanical keyboard
I bought a used Ducky One 2 Mini recently because the new ones cost a fortune in my country and it seemed in a good condition. But as life goes it was not without it's flaws.
It had the usual key chatter, nothing a little cleaning and contact cleaner can't fix. But after a while I was experiencing a strange behavior, the AltGr key gets stuck for no reason.
Debuging the problem
To rule out any software issue I tried the keyboard with different operating systems. The issue occurred on Windows too, same as on other Linux distros.
There were some threads online that it can be caused by a Logitech mouse and as I use my G500 as my daily driver, so I tried without it for a while. The issue still occurred.
Tried to refresh the firmware on it in hope that maybe this will solve the issue. But I didn't had any luck in that. The firmware updater from Ducky just won't detect my keyboard at all.
So at that point I was sure the problem is a faulty PCB on my newly acquired 60% keyboard. That's my luck buying used.
Solving the problem
For most of the time it was not so annoying. It gets stuck daily a few times, but lucky for me it gets unstuck when I press the AltGr key. So when I experience weird characters while typing I just hit AltGr and it would be fine again.
The annoying part started when I played games. If the key gets stuck it would lead to all sorts of funny scenarios, not to mention the quick chats becoming a mess easily. So I concluded that I needed to replace this keyboard or find a software solution for it.
I thought if I can find a way to detect the stuck state of the key I could write something to trigger a keyup event for this key to get it unstuck. Seemed simple but took some trial and error to get it working.
I used python to do this script, I start it automatically on startup and it runs in the background. It goes something like this:
import keyboard # pip3 install keyboard
from datetime import datetime
while True: # making a loop
time.sleep(0.05) # reduce loop frequency to eat less memory
if keyboard.is_pressed('altgr'): # if key 'altgr' is pressed
print(datetime.now(), " - Alt pressed") # for logs
time.sleep(0.5) # wait 500ms to see it was intentional or not
keyboard.send("altgr") # send a keypress to get the key unstuck
# i tried to trigger a keypress with xdotool at first, but it wouldn't do the trick
# but in case you want to that, here is how you could
# subprocess.run(["xdotool", "key", "ISO_Level3_Shift"])
print(datetime.now(), " - Alt released") # for logs
So this solved it for me. Well almost.
Because it is triggered by the software, the computer will think the key is not pressed anymore. That's good, of course. But the hardware where the issue lies will still think the key is pressed constantly. This can cause issues like when you are trying to access certain macros with the Fn button an entirely different macro will trigger. So that's why it is only a half measure, but it is certainly enough for me not to abandon my newly purchased keyboard. (until I finally build my own)