How to Control the Keyboard and Mouse with Python

Controlling mouse with Python

Tested on WinXP, Python 2.6 (3.x also tested) after installing pywin32 (pywin32-214.win32-py2.6.exe in my case):

import win32api, win32con
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(10,10)

How can I control the keyboard and mouse with Python?

for the mouse, I've found pymouse which seems to work (I haven't fully tried it, a small hack needed for the click, cf the issues)

for the keyboard, I'm not sure Xlib can do the job. I'm still looking on how to write something but you can catch key event as explained here or in C here using Xlib (but I don't know C).

here is an example working on gnome only (not good enough yet)

In pymouse, they have a nice way to make it work on the 3 different platform but needs to make 3 code...

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:

  1. 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.
  2. (On Windows) Control-Alt-Delete does allow Task Manager to be opened and then the program can be force-stopped from there.
  3. 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)

See a full version of the code here:

https://pastebin.com/WUygDqbG

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()

How to control the mouse in Mac using Python?

I dug through the source code of Synergy to find the call that generates mouse events:

#include <ApplicationServices/ApplicationServices.h>

int to(int x, int y)
{
CGPoint newloc;
CGEventRef eventRef;
newloc.x = x;
newloc.y = y;

eventRef = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, newloc,
kCGMouseButtonCenter);
//Apparently, a bug in xcode requires this next line
CGEventSetType(eventRef, kCGEventMouseMoved);
CGEventPost(kCGSessionEventTap, eventRef);
CFRelease(eventRef);

return 0;
}

Now to write Python bindings!

Is there a way for pyautogui to work without taking over mouse/keyboard control?

No, pyautogui only simulates user input which will take over the mouse/ keyboard.
From their github, "All keyboard presses done by PyAutoGUI are sent to the window that currently has focus, as if you had pressed the physical keyboard key." And similarly the mouse automation just sends commands to the OS as if you had physically moved/ clicked your mouse.

To write a program to control a game while still allowing you to use user control (mouse/ keyboard) you will have to directly send commands to the game by manipulating the game's memory. This is called making a "trainer". https://en.wikipedia.org/wiki/Trainer_(games)

Python script to control mouse clicks

I don't think with win32api you can listen to clicks you can just generate them (not sure though). However, try using pyHook, it's a simple api easy to use and can be found here http://sourceforge.net/apps/mediawiki/pyhook/index.php?title=Main_Page. With pyhook you can create a listener to listen to a mouse event and upon a mouse click you can do whatever you want, the example in the link shows you how. As for key press, you can use the same api for that too, also an example is provided, good luck!



Related Topics



Leave a reply



Submit