所需环境:
IDE:Pycharm
第三方库:PyUserInput
通过 pip install PyUserInput 安装
win10可能会安装失败,解决方法参考 https://blog.csdn.net/zhusongziye/article/details/79241410
用法示例:
from pymouse import PyMouse
from pykeyboard import PyKeyboard
m = PyMouse()
k = PyKeyboard()
m.click(100,150)
k.type_string('123456')
k.tap_key(k.enter_key)
上述代码的含义即点击坐标(100,150),然后输入123456 (内容依次输入的),并按下回车键,可以简单模拟一个输入密码的过程
1.PyMouse的基本方法:
1)drag(self, x, y)
- Drag the mouse to a given x and y.
- A Drag is a Move where the mouse key is held down.
把鼠标移动到x,y坐标处,是一个拖拽动作,不只是移动
2) move(self, x, y)
- Move the mouse to a given x and y
把鼠标移动到x,y
3)position(self)
- Get the current mouse position in pixels.
- Returns a tuple of 2 integers
得到鼠标的当前位置,并返回坐标x,y
4)press(self, x, y, button=1)
- Press the mouse on a given x, y and button.
- Button is defined as 1 = left, 2 = right, 3 = middle.
鼠标按下不松开x,y的坐标点,button表示左右键
5)release(self, x, y, button=1)
- Release the mouse on a given x, y and button.
- Button is defined as 1 = left, 2 = right, 3 = middle.
松开指定位置的鼠标
6)scroll(self, vertical=None, horizontal=None, depth=None)
- Generates mouse scrolling events in up to three dimensions: vertical,horizontal, and depth (Mac-only). Values for these arguments may be positive or negative numbers (float or int). Refer to the following:
- Vertical: + Up, - Down
- Horizontal: + Right, - Left
- Depth: + Rise (out of display), - Dive (towards display)
滚动鼠标的滚轮,在三个方向上移动
7)click(self, x, y, button=1, n=1)
- Click a mouse button n times on a given x, y.
- Button is defined as 1 = left, 2 = right, 3 = middle.
鼠标点击
2.PyKeyboard的基本方法:
1)press_key(self, character='')
- Press a given character key.
2)release_key(self, character='')
- Release a given character key.
3) press_keys(self, characters=[])
- Press a given character key.
4)tap_key(self, character='', n=1, interval=0)
- Press and release a given character key n times.
#点击功能键F5
pyk.tap_key(pyk.function_keys[5])
#点击小键盘5,6次
pyk.tap_key(pyk.numpad_keys[5],6)
#点击回车键
pyk.tap_key(k.enter_key)
#联合按键模拟
#同时按alt+tab键盘
pyk.press_key(pyk.alt_key)#按住alt键
pyk.tap_key(pyk.tab_key)#点击tab键
pyk.release_key(pyk.alt_key)#松开alt键
5)type_string(self, char_string, interval=0)
- A convenience method for typing longer strings of characters. Generates as few Shift events as possible.