Python——控制鼠标键盘
一、安装包
pip install pynput
二、引用包
from pynput import mouse,keyboard
三、控制鼠标
from pynput.mouse import Button,Controller mouse = Controller() mouser.position =(10,20) mouser.move(5,-5) mouse.press(Button.left) mouse.release(Button.left) mouse.click(Button.left,2) mouse.scroll(0,2)
四、监控鼠标事件
from pynput import mouse def on_mov(x,y): pass def on_onclick(x,y,button,pressed): if not pressed: return False def on_scroll(x,y,dx,dy): pass with mouse.Listener(on_mov=on_mov, on_onclick=on_onclick, on_scroll=on_scroll) as listener: listener.join()
五、控制键盘
from pynput.keyboard import Key,Controller keyboard = Controller() keyboard.press(Key.space) keyboard.release(Key.space) keyboard.press('A') keyboard.release('A') with keyboard.pressed(Key.shift): keyboard.press('a') keyboard.release('a') keyboard.type('Hello World')
六、监控键盘事件
from pynput import keyboard def on_press(key): try: pass except AttributeErrorL: pass def on_release(key): if key == keyboard.Key.esc: return False with keyboard.Listener(on_press=on_press, on_release=on_release) as listener listener.join()