-
Notifications
You must be signed in to change notification settings - Fork 69
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
Question: How to waif for key event? #155
Comments
The "pythonic" way to do this through python-mpv's interfaces I think would be to register two key press handlers that notify your interaction thread through a condition object from python's threading module. Here's some (untested) code as a starting point: import threading
cond = threading.Condition()
key = None
@player.on_key_press("a")
def on_a():
nonlocal key
key = 'a'
with cond:
cond.notify()
@player.on_key_press("b")
def on_b():
nonlocal key
key = 'b'
with cond:
cond.notify()
with cond:
cond.wait(timeout=None)
on_a.unregister_mpv_key_bindings()
on_b.unregister_mpv_key_bindings()
print(f'User pressed {key}') Execution will stop at |
Thanks for the help! This was what I was looking for. When trying this out I did notice something I am unsure if it is intended behavior. import threading
import mpv
def wait_a_or_b(player: mpv.MPV) -> str:
cond = threading.Condition()
key = None
@player.on_key_press("a")
def on_a():
nonlocal key
key = 'a'
with cond:
cond.notify()
@player.on_key_press("b")
def on_b():
nonlocal key
key = 'b'
with cond:
cond.notify()
with cond:
cond.wait(timeout=None)
on_a.unregister_mpv_key_bindings()
on_b.unregister_mpv_key_bindings()
return key
player = mpv.MPV(input_default_bindings=False, input_vo_keyboard=True, osc=True, volume=10)
# Register a keybinding
@player.on_key_press("r")
def my_r_binding():
pass
player.loop = True
player.play("ThreeEditions.mkv")
player.wait_until_playing()
keyPressed = wait_a_or_b(player) # <----
#print(f'User pressed {keyPressed}')
my_r_binding.unregister_mpv_key_bindings() # <----
input("Press enter to continue...") This code runs fine and does what is expected. However, if we comment out exception:
|
Oh, that looks like a bug. I'll try to reproduce this and commit a fix. |
Let me know if you need any more input from me. |
How would I get my script to wait for one of two key presses in mpv?
I want to wait for the user to press one of two keys and then do things based on what key was pressed.
Currently I have hacked together a an ugly way of waiting for keypresses using wait_for_event and on_key_press sending some text to the osd.
You might be able to tell I am fairly new to python.
The text was updated successfully, but these errors were encountered: