Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When you press the control key and an alphabet key at the same time on Windows with pynput, a control character is detected. #64

Open
lacoroze opened this issue Sep 27, 2024 · 0 comments

Comments

@lacoroze
Copy link

lacoroze commented Sep 27, 2024

It was an issue that occurred in my Windows 10 22H2 environment, and I fixed it.
In pynput, when you press an alphabet key while holding down the control key, control characters (\x01 to \x1a) are detected. (The control key is detected properly, but the additional pressed alphabet key is recognized as a control character.)

The program's default setting is Control+Shift+Spacebar. Also, when an unknown character is detected, it defaults to the spacebar. Because of this, I realized that pressing Control, Shift, and any alphabet key triggers the activation function automatically.

(
Here are some related questions I found through my search
https://stackoverflow.com/questions/65167647/how-can-i-detect-ctrlc-in-python3-8
https://stackoverflow.com/questions/77618988/python-pynput-keyboardtracking-ctrl-second-key-doesnt-output-second-key
moses-palmer/pynput#202
)

So, I temporarily resolved the issue by adding control characters to the keymap.

(I think this issue might only be happening to me (other Windows users haven't reported similar issues — why?), and since I consider the code I added to be a temporary fix, I'm leaving it as an issue here instead of submitting a pull request.)

I added the following entries to the dictionary at /src/key_listener.py:815.

self.keyboard.KeyCode.from_char("\x01"): KeyCode.A,
self.keyboard.KeyCode.from_char("\x02"): KeyCode.B,
self.keyboard.KeyCode.from_char("\x03"): KeyCode.C,
self.keyboard.KeyCode.from_char("\x04"): KeyCode.D,
self.keyboard.KeyCode.from_char("\x05"): KeyCode.E,
self.keyboard.KeyCode.from_char("\x06"): KeyCode.F,
self.keyboard.KeyCode.from_char("\x07"): KeyCode.G,
self.keyboard.KeyCode.from_char("\x08"): KeyCode.H,
self.keyboard.KeyCode.from_char("\x09"): KeyCode.I,
self.keyboard.KeyCode.from_char("\x0a"): KeyCode.J,
self.keyboard.KeyCode.from_char("\x0b"): KeyCode.K,
self.keyboard.KeyCode.from_char("\x0c"): KeyCode.L,
self.keyboard.KeyCode.from_char("\x0d"): KeyCode.M,
self.keyboard.KeyCode.from_char("\x0e"): KeyCode.N,
self.keyboard.KeyCode.from_char("\x0f"): KeyCode.O,
self.keyboard.KeyCode.from_char("\x10"): KeyCode.P,
self.keyboard.KeyCode.from_char("\x11"): KeyCode.Q,
self.keyboard.KeyCode.from_char("\x12"): KeyCode.R,
self.keyboard.KeyCode.from_char("\x13"): KeyCode.S,
self.keyboard.KeyCode.from_char("\x14"): KeyCode.T,
self.keyboard.KeyCode.from_char("\x15"): KeyCode.U,
self.keyboard.KeyCode.from_char("\x16"): KeyCode.V,
self.keyboard.KeyCode.from_char("\x17"): KeyCode.W,
self.keyboard.KeyCode.from_char("\x18"): KeyCode.X,
self.keyboard.KeyCode.from_char("\x19"): KeyCode.Y,
self.keyboard.KeyCode.from_char("\x1a"): KeyCode.Z,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant