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

Pause listener while using Controller().type #40

Closed
Jzege opened this issue Aug 8, 2017 · 5 comments
Closed

Pause listener while using Controller().type #40

Jzege opened this issue Aug 8, 2017 · 5 comments

Comments

@Jzege
Copy link

Jzege commented Aug 8, 2017

When I type something programatically, the listener records what I type and more:

from pynput import keyboard

def on_release(key):
    print key

# type A1 when I press tab
def expander(msg, data):
    if data.vkCode == 0x9 and msg == 256:
        listener.suppress_event()
    elif data.vkCode == 0x9 and msg == 257:
        keyboard.Controller().type("A1")
        listener.suppress_event()

with keyboard.Listener(on_release=on_release, win32_event_filter=expander) as listener:
    listener.join()

Types "A1" in a text editor, but the listener gives:

u'A'
u'1'
u'\x00'
u'1'

Process finished with exit code 1

Is there a way to stop listening while I programmatically type something with pynput?

@Jzege Jzege changed the title Pause listener while using Controller.type() Pause listener while using Controller().type Aug 8, 2017
@SpecialCharacter
Copy link

I have the same issue.
(See also: https://stackoverflow.com/questions/45250525/programmed-a-keylistener-but-it-executes-everything-twice?noredirect=1#comment77950594_45250525)

Additionally, I think this behavior messes with the deadkeys.

@moses-palmer
Copy link
Owner

@SpecialCharacter I have made a comment on StackOverflow, and also pushed a branch for testing here. If you find the time to test it, I would be grateful. The fix removed the additional event triggered by the controller, and the simple test case posted by you on StackOverflow appears to pass.

@Jzege Your issue is a bit more complicated. It appears that KBDLLHOOKSTRUCT::vkCode can be set to VK_PACKET. In this case, KBDLLHOOKSTRUCT::scanCode is set to the UTF-16 representation of the key.

The testing branch includes a fix for this as well; if KBDLLHOOKSTRUCT::vkCode is VK_PACKET, it will use KBDLLHOOKSTRUCT::scanCode as a raw unicode character. If you find any issues, please report them here.

@Jzege
Copy link
Author

Jzege commented Aug 10, 2017

Works great so far, thank you.

@Jzege Jzege closed this as completed Aug 10, 2017
@moses-palmer
Copy link
Owner

The fix has been merged in pynput 1.3.6.

@SpecialCharacter
Copy link

SpecialCharacter commented Aug 18, 2017 via email

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

3 participants