【322】python控制键盘鼠标:pynput
参考:python实战===python控制键盘鼠标:pynput
参考:[Python Study Notes]pynput实现对鼠标控制
参考:pynput doc
参考:pynput Package Documentation
Python控制键盘鼠标:pynput
地址:pynput - PyPI
这个库让你可以控制和监控输入设备。
对于每一种输入设备,它包含一个子包来控制和监控该种输入设备:
- pynput.mouse:包含控制和监控鼠标或者触摸板的类。
- pynput.keyboard:包含控制和监控键盘的类。
基本用法介绍:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | from pynput.mouse import Button, Controller import time # 获取鼠标位置 mouse = Controller() print (mouse.position) time.sleep( 3 ) print ( 'The current pointer position is {0}' . format (mouse.position)) # 设置鼠标位置 mouse.position = ( 277 , 645 ) print ( 'now we have moved it to {0}' . format (mouse.position)) # 鼠标移动(x,y)个距离 mouse.move( 5 , - 5 ) print (mouse.position) # 鼠标单击与释放 mouse.press(Button.left) mouse.release(Button.left) # 左键单击 mouse.click(Button.left, 1 ) # 右键单击 mouse.click(Button.right, 1 ) # 左键双击 mouse.click(Button.left, 2 ) # 鼠标滚动(x,y) x代表左右移动,y代表上下移动 # X:正值代表从右向左 Y:正值代表向上移动,负值代表向下移动 mouse.scroll( 0 , 100 ) |
监控鼠标事件 :略
键盘输入用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from pynput.keyboard import Key, Controller keyboard = Controller() #Press and release space keyboard.press(Key.space) keyboard.release(Key.space) #Type a lower case A ;this will work even if no key on the physical keyboard is labelled 'A' keyboard.press( 'a' ) keyboard.release( 'a' ) #Type two upper case As keyboard.press( 'A' ) keyboard.release( 'A' ) # or with keyboard.pressed(Key.shift): keyboard.press( 'a' ) keyboard.release( 'a' ) #type 'hello world ' using the shortcut type method keyboard. type ( 'hello world' ) |
综合使用:
1 2 3 4 5 6 | import pynput from pynput.mouse import Button from pynput.keyboard import Key mouse = pynput.mouse.Controller() keyboard = pynput.keyboard.Controller() ... |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)