moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.81k stars 251 forks source link

pynput listens to automatic keystrokes #495

Open Abelcanc3rhack3r opened 2 years ago

Abelcanc3rhack3r commented 2 years ago

Hi, I have this issue:

Say I register a keyboard listener like this :

keyboard = pynput.keyboard.Controller()
def on_each_key_press(key):
print(key)

if(key==Key.enter):#make the pynput type a whenever Enter is pressed

    keyboard.type("a")

if key == Key.esc:
    # Stop listener
    return False
def run():

with Listener(
    on_press = on_each_key_press,
    ) as listener:
    listener.join()`

This is the output: (I pressed the shift and # and ENTER keys myself)

Key.shift pressed

 '#' pressed

 Key.enter pressed

 pynput types a

 'a' pressed

Then the pynput will not only listen to the keys the user types, but it will also listen to the keys that pynput auto types (i.e the "a" after the ENTER key is pressed)

is there a way to stop pynput from tracking its own input?

IGalat commented 2 years ago

@Abelcanc3rhack3r If you're on Windows, you can use win32_event_filter like this

Alternatively, you can try my more high-level version, which does this automatically :)