Hi guys, I am using Pynput library to listen for the keyboard events and store typed keys in a variable, which I will use further for a text expansion.
However, I noticed, when I type too fast, that a simple code like the one below sometimes will place inside the accumulated_keys variable a different value from what I see on screen (which happens on any windows app). Sometimes I type "btw". I see "btw" on the screen but the variable shows "bwt" or vice-versa.
Why does this difference happen? Is there any workaround to solve it?
I am new to Python and programming, and I searched a lot about this. I know that inside specific apps (MS Word and others) I can get the value of the first word behind the cursor after I type, which is 100% reliable.
However, I want something more general and easy to implement in any input text window of the system. Is there a way to accomplish this? Any tips? My key listener must be fast and reliable. Thanks in advance.
However, I noticed, when I type too fast, that a simple code like the one below sometimes will place inside the accumulated_keys variable a different value from what I see on screen (which happens on any windows app). Sometimes I type "btw". I see "btw" on the screen but the variable shows "bwt" or vice-versa.
Why does this difference happen? Is there any workaround to solve it?
I am new to Python and programming, and I searched a lot about this. I know that inside specific apps (MS Word and others) I can get the value of the first word behind the cursor after I type, which is 100% reliable.
However, I want something more general and easy to implement in any input text window of the system. Is there a way to accomplish this? Any tips? My key listener must be fast and reliable. Thanks in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
from pynput.keyboard import Listener, Key # Initialize variables shift_pressed = False accumulated_keys = "" def press_on(key): global shift_pressed # Declare it global to modify it inside the function if key = = Key.shift or key = = Key.shift_r: shift_pressed = True print ( f "Press on {key}" ) def press_off(key): global shift_pressed, accumulated_keys # Declare them global to modify them inside the function if key = = Key.shift or key = = Key.shift_r: shift_pressed = False if hasattr (key, 'char' ) and key.char: # Check if the key has a printable character if shift_pressed: accumulated_keys + = key.char.upper() else : accumulated_keys + = key.char print ( f "Press OFF {key}, Accumulated keys: {accumulated_keys}" ) if key = = Key.esc: return False with Listener(on_press = press_on, on_release = press_off) as listener: listener.join() |
buran write Sep-18-2023, 03:40 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
This time I added the python tags for you
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
This time I added the python tags for you