I want some of my class functions to be called when a some keyboard keys are pressed, no matter which window is active. how to do that in LINUX. What I was using until now is OpenCV and waitKey() function but for that I need to show a window and have it active when pressing a keyboard key. I do have a main loop always running where the pressed key can be checked but it would be nice to have a solution where no loop is needed.
Related
I want to make a program or when I click on a key the mouse clicks automatically (as long as I click on the key) if I do not click on the key it stops.
I don't want the clicks to happen only when I touch the key once, but as long as the key is held down (It can also be the left button of the mouse pressed that trigger clicks like razer synapse mouses)
Any Idea ?
EDIT 1 :
This one works but not when a key is held down (even when the click is held down it doesn't work anyway) it only detects a single click on the mouse and then it clicks by itself instead of clicking ONLY when the key is held down...
import pyautogui, time
from pynput import mouse
from pynput.mouse import Button,Controller
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry('500x400')
combo = ttk.Combobox(root,values=['ctrl','shift','alt'],width=5)
combo.set('Key...')
combo.pack()
def on_click(x, y, button, pressed):
if button == mouse.Button.left:
while pressed:
pyautogui.click()
pyautogui.PAUSE = 0.1
else:
return False
with mouse.Listener(
on_click=on_click
) as Listener:
Listener.join()
root.mainloop()
You can use the mouse module (pip install mouse) to setup mouse hooks (hotkeys) that will let you trigger the clicking globally. However, in order to manage the beginning and end of this clicking, you will need to use a new thread (here is a short intro to threading if you want to learn more about it). You will want to start a thread when you press down your hotkey. This thread will continuesly click until you trigger an event that stops it. You will trigger this event by releasing your hotkey. Thus, the thread (and with it the clicking) will begin when you press the hotkey down and end when you let it back up.
Here is a piece of code that does exactly that using the middle (scroll) mouse button as the hotkey:
import mouse # pip install mouse
import threading
import pyautogui
pyautogui.PAUSE = 0.1 # set the automatic delay between clicks, default is 0.1
def repeat_function(kill_event):
# as long as we don't receive singal to end, keep clicking
while not kill_event.is_set():
pyautogui.click()
while True:
# create the event that will kill our thread, don't trigget it yet
kill_event = threading.Event()
# create the thread that will execute our clicking function, don't start it yet
new_thread = threading.Thread(target=lambda: repeat_function(kill_event))
# set a hook that will start the thread when we press middle mouse button
mouse.on_button(new_thread.start, (), mouse.MIDDLE, mouse.DOWN)
# set a hook that will kill the thread when we release middle button
mouse.on_button(kill_event.set, (), mouse.MIDDLE, mouse.UP)
# wait for user to use the hotkey
mouse.wait(mouse.MIDDLE, mouse.UP)
# remove hooks that used the killed thread and start again with a new one
mouse.unhook_all()
If you want to use the right mouse button instead, replace mouse.MIDDLE with mouse.RIGHT. I would not recommend using the left mouse button as the hotkey, as pyautogui will simulate clicking this button and likely break the program. If you want to use a key on the keyboard as the hotkey, check out the keyboard module. The concept there is the exact same.
Note that as this code is implemented, it will not be able to do anything else while waiting for the hotkey and processing it. You will need to use it as a separate python program if you want to use it as-is. You could also implement this code to run in a separate thread during another program, but it would definitely be easier to just launch it as a stand-alone script.
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.
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 can I detect a key press outside of a python window(something like a tkinter window). So for example if I pressed the 'a' key while on Chrome, how would I detect I pressed that?
I am writing an application in Python using Tkinter to manage my GUI.
There is a text entry box on which I am trying to implement an autocompletion function which will bind to the Tab key.
I have bound the tab key to my entry box, but when I press tab, the program attempts to cycle between GUI elements.
How do I override this default behavior so that the GUI will only carry out my specified command on the key press?
Return 'break' at the end of your event handler. It interrupts event propagation.
def my_tab_handler(event):
... # handle tab event
return 'break' # interrupts event propagation to default handlers