1、PyUserInput 简介
PyUserInput是一个使用python的跨平台的操作鼠标和键盘的模块,非常方便使用。支持的平台及依赖如下:
- Linux - Xlib
- Mac - Quartz, AppKit
- Windows - pywin32, pyHook
支持python版本:我用的是3.6.7
2、安装
直接源码安装,python3加持:
git clone https://github.com/PyUserInput/PyUserInput.gitcd PyUserInputsudo python3 setup.py install
注: 推荐用python3
3、入门
实例化一个鼠标和键盘对象:
from pymouse import PyMousefrom pykeyboard import PyKeyboardm = PyMouse()k = PyKeyboard()
点击屏幕中间并输入"hello world":
x_dim, y_dim = m.screen_size()m.click(x_dim//2, y_dim//2, 1) #取整除 - 向下取接近除数的整数k.type_string('Hello, World!')
PyKeyboard支持多种输入方法:
# pressing a keyk.press_key('H')# which you then follow with a release of the keyk.release_key('H')# or you can 'tap' a key which does bothk.tap_key('e')# note that that tap_key does support a way of repeating keystrokes with a interval time between eachk.tap_key('l',n=2,interval=5)# and you can send a string if needed took.type_string('o World!')
并且支持各种特殊的按键输入:
#Create an Alt+Tab combok.press_key(k.alt_key)k.tap_key(k.tab_key)k.release_key(k.alt_key)k.tap_key(k.function_keys[5]) # Tap F5k.tap_key(k.numpad_keys['Home']) # Tap 'Home' on the numpadk.tap_key(k.numpad_keys[5], n=3) # Tap 5 on the numpad, thrice
4、PyMouse项目分析
PyUserInput开源项目维护的不太好,是将之前的PyMouse和PyKeyboard项目合到一起,而且竟然把人家之前的DEMO给删了!!!为了体验原汁原味的PyMouse我们还是单独分析吧 :
下面的一个例子展示了PyMouse的经典用法,主要用来操作鼠标 :
# import the modulefrom pymouse import PyMouse# instantiate an mouse objectm = PyMouse()# move the mouse to int x and int y (these are absolute positions)m.move(200, 200)# click works about the same, except for int button possible values are 1: left, 2: right, 3: middlem.click(500, 300, 1)# get the screen sizem.screen_size()# (1024, 768)# get the mouse positionm.position()# (500, 300)
我在之前写过一篇《》,就是用了PyMouse。
5、小结
PyMouse、PyKeyboard用python操作鼠标和键盘的库,使用起来比较简单,需要结合具体的应用需求才能玩出新花样。比如和openCV结合做、、....
: 完~
: 大家觉得不错,可以点推荐给更多人~LINKS
@beautifulzzzz智能硬件、物联网,热爱技术,关注产品博客:http://blog.beautifulzzzz.com园友交流群:414948975