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

Repeatable key presses? #18

Open
hany opened this issue Jan 16, 2025 · 1 comment
Open

Repeatable key presses? #18

hany opened this issue Jan 16, 2025 · 1 comment

Comments

@hany
Copy link

hany commented Jan 16, 2025

Is it currently possible to configure repeatable key presses? Currently via the Lua add_key_binding call), you can pass a repeatable option so holding down the key will trigger the call back repeatedly. Does such functionality exist with this library?

@TnTora
Copy link

TnTora commented Jan 19, 2025

I don't think it is possible with the current implementation.

I am facing a similar problem (alongside a few others) so I just started working on a fork with a few changes and additions to help with some mpv scripts I've been woking on.

One of these changes is making on_key_press and bind_key_press work similarly to the Lua mp.add_forced_key_binding call as well as adding a remove_key_binding method.

However in order to achieve this I switched from the keybind command to using exclusively define-section, enable-section and disable-section but these commands (although they work) are technically deprecated, except for mpv-internal uses, so I am not sure if it's going to be a good long term solution.

If you are satisfied with how key bindings are handled by the current library and you just need the ability to configure repeatable key presses, you should be able to add just that option by slightly modifying the on_key_press and bind_key_press methods like this:

def on_key_press(self, name, repeatable=False):
        """
        Decorator to bind a callback to an MPV keypress event.

        @on_key_press(key_name)
        def my_callback():
            pass
        """
        def wrapper(func):
            self.bind_key_press(name, func, repeatable)
            return func
        return wrapper
def bind_key_press(self, name, callback, repeatable=False):
        """
        Bind a callback to an MPV keypress event.

        *name* is the key symbol.
        *callback()* is the function to call.
        """
        self.keybind_lock.acquire()
        keybind_id = self.keybind_id
        self.keybind_id += 1
        self.keybind_lock.release()

        prefix = ""
        if repeatable:
            prefix = "repeatable"

        bind_name = "bind{0}".format(keybind_id)
        self.key_bindings["bind{0}".format(keybind_id)] = callback
        try:
            self.keybind(name, "{0} script-message custom-bind {1}".format(prefix , bind_name))
        except MPVError:
            self.define_section(bind_name, "{0} {1} script-message custom-bind {2}".format(name, prefix , bind_name))
            self.enable_section(bind_name)

I haven't actually tried this code but based on the mpv documentation on input command prefixes it should work.

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