How to avoid key auto repeat if key is held down?

362 Views Asked by At

I am currently coding a program, with the library 'keyboard' in python 3 on Debian.

So if i hold down a Key. The keyboard-library gets multiple KeyDownEvents instead of one at KeyPress but I only want the first event. Is there anyway to suppress the auto-repeat?

I have already tried multiple terminal commands in python which should stop the keyboard auto-repeat but that mostly only works for printable Chars but I also need only 1 event if for example shift is pressed
My conclusion is that the terminal commands don't effect the keyboard library at all.

Any ideas?

Best regards

2

There are 2 best solutions below

0
On

If you're okay with making it a system-wide change, you can turn off key repeat with:

import os
os.system('xset r off')

You can change it back on program exit with:

os.system('xset r on')
0
On
if keyboard.is_pressed("b"):
    while keyboard.is_pressed("b"):
        print("")#something to do otherwise error occurs

The program will wait until the key is released for it to execute code below. You could experiment with threading if you need your program to continue to run code while the key is pressed. "b" is used as a key in the code but you can change it to another letter.