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

How to detect key being held down instead of a press? #36

Closed
Miail opened this issue Jul 4, 2017 · 3 comments
Closed

How to detect key being held down instead of a press? #36

Miail opened this issue Jul 4, 2017 · 3 comments

Comments

@Miail
Copy link

Miail commented Jul 4, 2017

I am currently working on implementing a feature which "does something while a key is being held down".. As I understand pynput, it seems impossible to implement such feature? or am i missing something?

This implementation:

from pynput import keyboard

def on_press(key):
    if key == keyboard.Key.cmd_l:
        try:
            print('- Started recording -'.format(key))
        except IOError:
            print "Error"
    else:
        print('incorrect character {0}, press cmd_l'.format(key))


def on_release(key):
    print('{0} released'.format(key))
    if key == keyboard.Key.cmd_l:
        print('{0} stop'.format(key))
        keyboard.Listener.stop
        return False


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

Only detect one press, also when the key is being held down.. It doesn't seem to read the stream..

I thought maybe the use of a while loop would help, also giving the way the problem is stated, so i tried this...

from pynput import keyboard

def on_press(key):
    while key == keyboard.Key.cmd_l:
        try:
            print('- Started recording -'.format(key))
        except IOError:
            print "Error"
    else:
        print('incorrect character {0}, press cmd_l'.format(key))


def on_release(key):
    print('{0} released'.format(key))
    if key == keyboard.Key.cmd_l:
        print('{0} stop'.format(key))
        keyboard.Listener.stop
        return False

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

Which resulted in being stuck in a while loop.. and not come outside..
can the library be used like this, can it detect "a key being held down"?

@moses-palmer
Copy link
Owner

moses-palmer commented Jul 4, 2017 via email

@Miail
Copy link
Author

Miail commented Jul 4, 2017

Sorry?.. Not sure I understand how the timer should be involved, and how on_release should turn it off..

Are thinking of a solution in which on_press and on_release toggle a boolean, and the the boolean either start/stops an while loop outside the function defintion... if thats the case, how will it update the state of the boolean when it is being stuck inside the while loop?

@moses-palmer
Copy link
Owner

You could use, for example, threading.Timer. It has a cancel method to stop the timer. You could keep a dict of pynput keys to timers.

I am currently on a flight, so I can't really help you more; as this is out of scope for this library, I'll close this issue.

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

2 participants