disable or lock mouse and keyboard in Python?
I haven't tested (actually I've tested the mouse part, and it annoyingly works) but something like this using pyhook would do what you want:
import pythoncom, pyHook
def uMad(event):
return False
hm = pyHook.HookManager()
hm.MouseAll = uMad
hm.KeyAll = uMad
hm.HookMouse()
hm.HookKeyboard()
pythoncom.PumpMessages()
Python Block Keyboard / Mouse Input
You can use the keyboard module to block all keyboard inputs and the mouse module to constantly move the mouse, preventing the user from moving it.
See these links for more details:
https://github.com/boppreh/keyboard
https://github.com/boppreh/mouse
This blocks all the keys on the keyboard (the 150 is large enough to ensure all keys are blocked).
#### Blocking Keyboard ####
import keyboard
#blocks all keys of keyboard
for i in range(150):
keyboard.block_key(i)
This effectively blocks mouse-movement by constantly moving the mouse to position (1,0).#### Blocking Mouse-movement ####
import threading
import mouse
import time
global executing
executing = True
def move_mouse():
#until executing is False, move mouse to (1,0)
global executing
while executing:
mouse.move(1,0, absolute=True, duration=0)
def stop_infinite_mouse_control():
#stops infinite control of mouse after 10 seconds if program fails to execute
global executing
time.sleep(10)
executing = False
threading.Thread(target=move_mouse).start()
threading.Thread(target=stop_infinite_mouse_control).start()
#^failsafe^
And then your original code here (the if statement and try/catch block are no longer necessary).#### opening the video ####
import subprocess
import pyautogui
import time
subprocess.Popen(["C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",])
time.sleep(3)
pyautogui.write('www.youtube.com/watch?v=DLzxrzFCyOs', interval = 0.5)
pyautogui.hotkey('enter')
#### stops moving mouse to (1,0) after video has been opened
executing = False
Just a few notes:- The mouse-moving is hard to stop from outside of the program (it's basically impossible to close the program when it is executing, especially as the keyboard is also being blocked), that's why I put in the failsafe, which stops moving the mouse to (1,0) after 10 seconds.
- (On Windows) Control-Alt-Delete does allow Task Manager to be opened and then the program can be force-stopped from there.
- This doesn't stop the user from clicking the mouse, which can sometimes prevent the YouTube link from being typed in full (i.e. a new tab can be opened)
https://pastebin.com/WUygDqbG
Related Topics
Why Results of Map() and List Comprehension Are Different
Increment Numpy Array with Repeated Indices
How to Compare Times of the Day
Pycharm: Set Environment Variable for Run Manage.Py Task
Extracting Days from a Numpy.Timedelta64 Value
Python-Pandas: the Truth Value of a Series Is Ambiguous
Count the Number of Occurrences of a Certain Value in a Dictionary in Python
Python Insert Numpy Array into SQLite3 Database
Weighted Standard Deviation in Numpy
Change Tick Frequency on X (Time, Not Number) Frequency in Matplotlib
Typeerror: Expected a Character Buffer Object - While Trying to Save Integer to Textfile
Python - Legend Overlaps with the Pie Chart
What Is the Purpose of Flask's Context Stacks
Difference Between Two Lists with Duplicates in Python
How Does the Max() Function Work on List of Strings in Python