Bernd
Bernd

Reputation: 57

Programmed a keylistener, but it executes everything twice

I programmed a standard keylistener with pynput, but when using keyboard.type, it seems like the keys are released twice.

from pynput.keyboard import Controller, Listener

keyboard = Controller()

def on_release(key):
    print('key {} released'.format(key))
    if key.char == 'a':
        keyboard.type('b')

with Listener(on_release=on_release) as listener:
    listener.join()

# Pressing 'a' yields:
# 
# key u'a' released
# key u'b' released
# key u'b' released

It doesn't seem like the key is pressed twice, only released twice. Is this the intended behavior? If not, what should be done to avoid this?

Upvotes: 1

Views: 536

Answers (1)

Moses Palmér
Moses Palmér

Reputation: 93

This is a bug in pynput.

As you have noticed, events can reach a Listener both from the system and when Controllers are invoked. The latter is true only for Xorg and win32, as they do no propagate synthetic events to listeners.

Or so I thought. It turns out that on win32, this is true only for mouse events; keyboard events appear to propagate normally. In any case, I have pushed a proposed solution here.

If you have the opportunity to test it, I would be grateful, otherwise I will merge it into master in a couple of days and make a new release. I only have access to win32 through VirtualBox, so I would appreciate some more bare-metal testing.

Upvotes: 1

Related Questions