jaseg / python-mpv

Python interface to the awesome mpv media player
https://git.jaseg.de/python-mpv.git
Other
531 stars 67 forks source link

trying to capture mouse clicks when running mpv #281

Closed najoshi closed 2 weeks ago

najoshi commented 3 weeks ago

Sorry, this isn't really an issue, but I don't see how else I can contact someone to ask about this. Is it possible to capture mouse clicks while mpv is running in full screen mode? I am writing a simple digital media frame with a touch screen for the Pi4. I already have the photos working using ImageTk and tkinter, and I capture the mouse clicks by binding the button to my canvas. But what I need is to be able to play the videos while still being able to capture the mouse clicks. When mpv plays a video, the canvas no longer gets the clicks. So is there a way to do that using python-mpv?

jaseg commented 2 weeks ago

If you use mpv's built-in window management, the easiest way to grab these events is by hooking into mpv's key binding system. Here's an example script:

import mpv
import time

player = mpv.MPV(loglevel='info', log_handler=lambda level, prefix, text: print(f'({level}) {prefix}: {text.rstrip()}'))
player.loop = 'inf'
player.fullscreen = True
player.play('tests/test.webm')

@player.on_key_press('MBTN_LEFT')
def click_handler():
    print(f'Left click at {player.mouse_pos}')

@player.on_key_press('MBTN_LEFT_DBL')
def click_handler():
    print(f'Left double click at {player.mouse_pos}')

@player.on_key_press('MBTN_RIGHT')
def click_handler():
    print(f'Right click at {player.mouse_pos}')

@player.on_key_press('MBTN_RIGHT_DBL')
def click_handler():
    print(f'Right double click at {player.mouse_pos}')

@player.on_key_press('MOUSE_MOVE')
def click_handler():
    print(f'Mouse moved to {player.mouse_pos}')

player.wait_for_playback()
najoshi commented 2 weeks ago

That's what I needed. Thank you!