mpv icon indicating copy to clipboard operation
mpv copied to clipboard

How to get keyboard and mouse input

Open ahaoboy opened this issue 1 year ago • 3 comments

I noticed that in the js script, it seems to use define-section to obtain input, but this command is marked as deprecated. I would like to know if there are other ways to capture user keyboard and mouse input.

I tried to emulate the execution logic of JavaScript in a C plugin, but I did not receive the MPV_EVENT_CLIENT_MESSAGE event. I'm not sure if I've missed something, and I hope someone can point out any mistakes or provide other useful information.

#include <mpv/client.h>
#include <stdio.h>

MPV_EXPORT
int mpv_open_cplugin(mpv_handle *handle)
{
  printf("Hello world from C plugin '%s'!\n", mpv_client_name(handle));
  mpv_command(handle, (const char *[]){"define-section", "mpv_input_js",
                                       "a\n", "default", NULL});
  mpv_command(handle, (const char *[]){"enable-section", "mpv_input_js",
                                       "allow-hide-cursor+allow-vo-dragging", NULL});
  while (1)
  {
    mpv_event *event = mpv_wait_event(handle, -1);
    printf("Got event: %d\n", event->event_id);
    if (event->event_id == MPV_EVENT_SHUTDOWN)
      break;

    if (event->event_id == MPV_EVENT_CLIENT_MESSAGE)
    {
      printf("key event: %d\n", event->event_id);
    }
  }
  return 0;
}

ahaoboy avatar Feb 20 '24 08:02 ahaoboy

@avih Could you please help me or give some advice? I'm stuck with this problem for a long time, or is it not possible to get the user's input in the c plugin? If this problem can be solved, it would be possible to use deno to run js scripts, which would solve all the current problems with js scripts, e.g. performance, network requests, reading and writing to files, etc. https://github.com/mpv-player/mpv/issues/13608#issuecomment-2147452138

ahaoboy avatar Jun 06 '24 05:06 ahaoboy

I don't know how to help you, sorry. But here are some data points:

Your section looks empty. There won't be any notifications unless the section has some bindings in it (and the bindings need to explicitly target your client id).

You really shouldn't use deprecated functiuons in your code. I don't know what the alternative is, or even if there is an (good) alternative. Not everything you want to do is necessarily possible.

In this case, maybe the keybind command could help, but I don't know how to unbind it (other than rebinding the key to ignore), or how it behaves with regards to the different sections.

avih avatar Jun 06 '24 07:06 avih

Thank you, it seems that only sdl2 can be used to temporarily solve the problem

ahaoboy avatar Jun 06 '24 08:06 ahaoboy