boppreh / keyboard

Hook and simulate global keyboard events on Windows and Linux.
MIT License
3.74k stars 432 forks source link

CTRL key hangs after pressing CTRL + LEFT #536

Open nnako opened 2 years ago

nnako commented 2 years ago

Hi,

I'm experiencing something similar to #512 :

I use the following code on a Windows 10 operating system with Python v3.8.8 and keyboard v0.13.5 installed:

import time
import keyboard

# globals
request_quit = False

# callback function definitions
def on__ctrl_shift_alt_t():
    global request_quit
    keyboard.write('__TEST__')
    request_quit = True

# preparations
keyboard.add_hotkey("ctrl+shift+alt+t", on__ctrl_shift_alt_t, suppress=True)

# endless loop
while not request_quit:
    print(keyboard.is_pressed("ctrl"))
    time.sleep(0.5)

starting the Python script, then change to another application window where the key sequence makes sense (in this case, moving the selected node one position to the left), pressing <CTRL> + <LEFT> moves the selected node as expected. But, unfortunately, the <CTRL> key keeps being "pressed", so that all subsequent presses of the <LEFT> key alone, moves the selected node further to the left, instead of moving just the selection one position to the left.

trying <CTRL> + <RIGHT>, this also seems to cause the <CTRL> key to hang, eventually. althouh it does not seem to happen as frequently as when doing <CTRL> + <LEFT> .

The most interesting thing: keyboard.is_pressed("ctrl") returns False when the <CTRL> key is indeed lifted while the effect "hanging CTRL key" is still visible. so, there seems to be no way to "clear" the key / buffer, eh?

Is there a plausible explanation for this behavior? Anything I can do against it?

nnako commented 1 year ago

for more than half a year, there has been not even the slightest response to my question.

does anybody have any experience with these kinds of "invisibly hanging control keys"? is there maybe another Python module realizing a cleaner / more reliable keyboard handling mechanism?

gitgrub commented 1 year ago

Just to confirm, I get hanging keys sometimes, too. Win10, python 3.9.5. I have no good solution unfortunately. What I do is pressing keys like ctrl, alt, shift, num-lock and such. That releases the hanging key most of the time, makes me think I am missing something. But there sure is something left for this useful module.

nnako commented 1 year ago

I'd like to try to correct this sporadic faulty behavior, myself. As I am about to develop an application that needs a 100% stable key recognition / evaluation, I'd like to find my way through the code and provide a fix.

Is there a way to lead me to the place in code where I would have to look into, first? Or maybe, there was some kind of structural documentation (diagrams) about this corner of the precious keyboard module? Where is the raw (low-level) keyboard interface to the operating system realized?

superDMS commented 2 months ago

你找了解决方案了吗? 我的ctrl键也被经常挂起, 明明已经放开.