summaryrefslogtreecommitdiff
path: root/mpv.py
diff options
context:
space:
mode:
authorjaseg <code@jaseg.net>2017-08-21 15:36:03 +0200
committerjaseg <code@jaseg.net>2017-08-21 15:36:03 +0200
commit2ca5c7b9c88960994a9b1494f46bd01c0388a21f (patch)
tree9e35987d3ffcf389954cb748b62ca82f88461bce /mpv.py
parent14783b2193759ee5a45654634a0bb863576df4d9 (diff)
downloadpython-mpv-2ca5c7b9c88960994a9b1494f46bd01c0388a21f.tar.gz
python-mpv-2ca5c7b9c88960994a9b1494f46bd01c0388a21f.tar.bz2
python-mpv-2ca5c7b9c88960994a9b1494f46bd01c0388a21f.zip
Add on_key_press
Diffstat (limited to 'mpv.py')
-rw-r--r--mpv.py32
1 files changed, 31 insertions, 1 deletions
diff --git a/mpv.py b/mpv.py
index c53deb8..1c32e10 100644
--- a/mpv.py
+++ b/mpv.py
@@ -915,8 +915,38 @@ class MPV(object):
def _binding_name(callback_or_cmd):
return 'py_kb_{:016x}'.format(hash(callback_or_cmd)&0xffffffffffffffff)
+ def on_key_press(self, keydef, mode='force'):
+ """ Function decorator to register a simplified key binding. The callback is called whenever the key
+ given is *pressed*.
+
+ To unregister the callback function, you can call its ```unregister_mpv_key_bindings``` attribute:
+
+ ```
+ player = mpv.MPV()
+ @player.on_key_press('Q')
+ def binding():
+ print('blep')
+
+ binding.unregister_mpv_key_bindings()
+ ```
+
+ WARNING: For a single keydef only a single callback/command can be registered at the same time. If you register
+ a binding multiple times older bindings will be overwritten and there is a possibility of references leaking. So
+ don't do that.
+
+ The BIG FAT WARNING regarding untrusted keydefs from the key_binding method applies here as well. """
+
+ def register(fun):
+ @self.key_binding(keydef, mode)
+ @wraps(fun)
+ def wrapper(state='p-', name=None):
+ if state[0] in ('d', 'p'):
+ fun()
+ return wrapper
+ return register
+
def key_binding(self, keydef, mode='force'):
- """ Function decorator to register a key binding.
+ """ Function decorator to register a low-level key binding.
The callback function signature is ```fun(key_state, key_name)``` where ```key_state``` is either ```'U'``` for
"key up" or ```'D'``` for "key down".