Hotkeys ignored w Python libvlc bindings on Linux - python

I have problem controlling videos from Python 3.x using python-vlc bindings on Linux.
The video plays fine in a window, but hotkeys seem to be ignored. Does libvlc media player handle hotkeys?
My minimal code:
import vlc
from time import sleep
player = vlc.MediaPlayer("test.mp4")
player.video_set_key_input(True)
player.play()
while player.get_state()!=vlc.State.Ended:
sleep(1)

It is my belief that your contention, that video_set_key_input and video_set_mouse_input are the solution is a misunderstanding of those functions.
From vlc.py
def video_set_key_input(self, on):
'''Enable or disable key press events handling, according to the LibVLC hotkeys
configuration. By default and for historical reasons, keyboard events are
handled by the LibVLC video widget.
#note: On X11, there can be only one subscriber for key press and mouse
click events per window. If your application has subscribed to those events
for the X window ID of the video widget, then LibVLC will not be able to
handle key presses and mouse clicks in any case.
#warning: This function is only implemented for X11 and Win32 at the moment.
#param on: true to handle key press events, false to ignore them.
'''
return libvlc_video_set_key_input(self, on)
def video_set_mouse_input(self, on):
'''Enable or disable mouse click events handling. By default, those events are
handled. This is needed for DVD menus to work, as well as a few video
filters such as "puzzle".
See L{video_set_key_input}().
#warning: This function is only implemented for X11 and Win32 at the moment.
#param on: true to handle mouse click events, false to ignore them.
'''
return libvlc_video_set_mouse_input(self, on)
This suggests that setting those functions to False simply tells vlc that mouse and key input are to be ignored and passed to the users appliction.
For that to work, your application must be accepting and processing, mouse and key input coming out of the assigned X-window.
Not, that suddenly, pressing the space bar will pause the video, as when, vlc is performing the task of accepting and processing the mouse and key input.

Related

OpenCV: control video playback with both keyboard and mouse

I'd like to be able to play a video (that automatically stops at some fixed frames), and I want to be able to play/stop either using keyboard or mouse. However, it seems that opencv does not provide a function to wait both a keyboard event or a mouse event. I saw there exists a function setMouseCallback but I'm not sure how to use it to make sure that I don't have any conflicts between the keyboard and the mouse. Am I supposed to create a thread that receive messages from both the mouse and the keyboard, or is there something simpler? Otherwise, is there any more suited library to play/seek/stop a video precisely on a given frame?
EDIT
I tried to implement a solution based on threads, and I do action = self.queue.get() instead of cv2.waitKey(...), but then nothing is displayed on screen. Also, if I do cv2.waitKey(1) before, I get an empty frame, and the cv2.waitKey located in other threads does not detect any keypress...

tkinter binding keyRelease works just as keyPress

I am using Tkinter in python 3 to make a primitive game (I am aware of pyGame). My function that I have bound to KeyReleased is executed when any key is pressed. It seems to work just as KeyPress. Code down below
master.bind("KeyRelease",keyReleased) (with <> on the sides of KeyRelease
Most keyboards and OS's will send a steady stream of press/release events when you hold a key down. If you bind to <KeyRelease>, it absolutely will only fire on key release but your app may be betting multiple key release events, making it appear that they are happening on a press.

How to get python to constantly check for key combinations even if it is in the background?

I was thinking of coding a script that will run in the background of my computer that will detect key combinations to start specific tasks.
For example, the user presses Ctrl+Alt+F1 then the browser starts. However, I cannot seem to find a way to detect these combinations while the script is running in the background.
I thought of using a .pyw extension for my script, but that makes the script not interact-able.
If anyone needs to know, my OS is Windows 10.
I think the pyHook library is what you're looking for:
The pyHook package provides callbacks for global mouse and keyboard events in Windows. Python applications register event handlers for user input events such as left mouse down, left mouse up, key down, etc. and set the keyboard and/or mouse hook.

Python win32api mouse control losing focus

I'm using win32api in a Python script to control mouse movements. It's working fantastic, but as soon as I click (I also generate click events) outside my Python shell/IDE, all my mouse events immediately stop. If I click my shell/IDE again, control is restored.
It seems like mouse control is only working when my Python shell or IDE is the "active" window - is there any way to retain mouse control even after Python is sent to the background?
Turns out this was not a Python issue, but was an issue with the device I was using to generate mouse movements. A separate API call was required to allow this device to push events when its owner was out of focus.

pygame capture keyboard events when window not in focus

I wrote a simple python script that gives control over the cursor to a joystick. My way to find out how this works is documented here. Now that works flawlessly but, as soon as I start the script to use the joystick, the mouse is useless, because my python routine sets the value back to its original, whenever a new joystick event comes in.
Thus I want my joystick events to be ignored as long as a key of the keyboard is pressed. I came across the pygame.key.get_pressed() method but this seems to work only, if the pygame window is in focus. I want this script running in background. Should I start using non-pygame events to listen to the keyboard or are there ways to keep track of the keyboard events analogue to the joystick events, which are recognized in background, via pygame?
I expect pygame sets up its own "sandbox" so that it's hard to detect input from outside its window. Your previous question indicates that you are also using the win32api module. We can use that to detect global key presses.
The correct way to detect key presses at the global scope is to set up a keyboard hook using SetWindowsHookEx. Unfortunately, win32api does not expose that method, so we'll have to use a less efficient method.
The GetKeyState method can be used to determine whether a key is down or up. You can continuously check the state of a key to see if the user has pressed or released it lately.
import win32api
import time
def keyWasUnPressed():
print "enabling joystick..."
#enable joystick here
def keyWasPressed():
print "disabling joystick..."
#disable joystick here
def isKeyPressed(key):
#"if the high-order bit is 1, the key is down; otherwise, it is up."
return (win32api.GetKeyState(key) & (1 << 7)) != 0
key = ord('A')
wasKeyPressedTheLastTimeWeChecked = False
while True:
keyIsPressed = isKeyPressed(key)
if keyIsPressed and not wasKeyPressedTheLastTimeWeChecked:
keyWasPressed()
if not keyIsPressed and wasKeyPressedTheLastTimeWeChecked:
keyWasUnPressed()
wasKeyPressedTheLastTimeWeChecked = keyIsPressed
time.sleep(0.01)
Warning: as with any "while True sleep and then check" loop, this method may use more CPU cycles than the equivalent "set a callback and wait" method. You can extend the length of the sleep period to ameliorate this, but the key detection will take longer. For example, if you sleep for a full second, it may take up to one second between when you press a key and when the joystick is disabled.
when your window gains or looses focus you get an ACTIVEEVENT. It's gain and state attributes tell you which state you've gained or lost. The easisest solution would probably be to catch this events in your main event loop and use them to keep track weather you have focus or not. Then you can just ignore joystick events if you don't have the focus.

Categories

Resources