用python控制鼠标和键盘

2021-03-10 15:23:19 浏览数 (2)

可以通过第三方模块mouse来控制鼠标操作。下面是该模块各个属性和方法的英文说明。常用的我加了中文注释。

代码语言:javascript复制
mouse.ButtonEvent
ButtonEvent(event_type, button, time)
ButtonEvent.button
Alias for field number 1
ButtonEvent.count(self, value, /)
Return number of occurrences of value.
ButtonEvent.event_type
Alias for field number 0
ButtonEvent.index(self, value, start=0, stop=9223372036854775807, /)
Return first index of value.
Raises ValueError if the value is not present.
ButtonEvent.time
Alias for field number 2
mouse.DOUBLE
= 'double'
mouse.DOWN
= 'down'
mouse.LEFT
= 'left'
mouse.MIDDLE
= 'middle'
class mouse.MoveEvent
MoveEvent(x, y, time)
MoveEvent.count(self, value, /)
Return number of occurrences of value.
MoveEvent.index(self, value, start=0, stop=9223372036854775807, /)
Return first index of value.
Raises ValueError if the value is not present.
MoveEvent.time
Alias for field number 2
MoveEvent.x
Alias for field number 0
MoveEvent.y
Alias for field number 1
mouse.RIGHT
= 'right'
mouse.UP
= 'up'
class mouse.WheelEvent
WheelEvent(delta, time)
WheelEvent.count(self, value, /)
Return number of occurrences of value.
WheelEvent.delta
Alias for field number 0
WheelEvent.index(self, value, start=0, stop=9223372036854775807, /)
Return first index of value.
Raises ValueError if the value is not present.
WheelEvent.time
Alias for field number 1
mouse.X
= 'x'
mouse.X2
= 'x2'
mouse.version
= '0.7.1'
mouse.is_pressed(button='left')
Returns True if the given button is currently pressed.
mouse.press(button='left') #按压
Presses the given button (but doesn't release).
mouse.release(button='left') #释放
Releases the given button.
mouse.click(button='left') #单击
Sends a click with the given button.
mouse.double_click(button='left') #双击
Sends a double click with the given button.
mouse.right_click() #右击
Sends a right click with the given button.
mouse.wheel(delta=1) #中间滚动delta步。delta正负号代表滚动方向
Scrolls the wheel delta clicks. Sign indicates direction.
mouse.move(x, y, absolute=True, duration=0, steps_per_second=120.0)#移动鼠标,可相对之前位置移动
Moves the mouse. If absolute, to position (x, y), otherwise move relative to the current position. If duration is non-zero, animates the movement. The fps of the animation is determined by 'steps_per_second', default is 120.
mouse.drag(start_x, start_y, end_x, end_y, absolute=True, duration=0)#拖动鼠标
Holds the left mouse button, moving from start to end position, then releases. absolute and duration are parameters regarding the mouse movement.
mouse.on_button(callback, args=(), buttons=('left', 'middle', 'right', 'x', 'x2'), types=('up', 'down', 'double'))
Invokes callback with args when the specified event happens.
mouse.on_click(callback, args=())
Invokes callback with args when the left button is clicked.
mouse.on_double_click(callback, args=())
Invokes callback with args when the left button is double clicked.
mouse.on_right_click(callback, args=())
Invokes callback with args when the right button is clicked.
mouse.on_middle_click(callback, args=())
Invokes callback with args when the middle button is clicked.
mouse.wait(button='left', target_types=('up', 'down', 'double'))
Blocks program execution until the given button performs an event.
mouse.get_position() #获取鼠标当前位置
Returns the (x, y) mouse position.
mouse.hook(callback)
Installs a global listener on all available mouses, invoking callback each time it is moved, a key status changes or the wheel is spun. A mouse event is passed as argument, with type either mouse.ButtonEvent, mouse.WheelEvent or mouse.MoveEvent.
Returns the given callback for easier development.
mouse.unhook(callback)
Removes a previously installed hook.
mouse.unhook_all()
Removes all hooks registered by this application. Note this may include hooks installed by high level functions, such as record.
mouse.record(button='right', target_types=('down',)) #记录所有鼠标事件用户按下了指定的键。
Records all mouse events until the user presses the given button. Then returns the list of events recorded. Pairs well with play(events).
Note: this is a blocking function. Note: for more details on the mouse hook and events see hook.
mouse.play(events, speed_factor=1.0, include_clicks=True, include_moves=True, include_wheel=True) #播放鼠标事件
Plays a sequence of recorded events, maintaining the relative time intervals. If speed_factor is <= 0 then the actions are replayed as fast as the OS allows. Pairs well with record().
The parameters include_* define if events of that type should be inluded in the replay or ignored.

可以通过第三方模块keyboard来控制键盘。例子如下:

代码语言:javascript复制
import keyboard

keyboard.press_and_release('shift s, space')
keyboard.send("down") #按下DOWN
keyboard.send("enter") #按回车
keyboard.send("tab") #按 Tab
#键盘输入字符串
keyboard.write('The quick brown fox jumps over the lazy dog.')

keyboard.add_hotkey('ctrl shift a', print, args=('triggered', 'hotkey'))

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey('page up, page down', lambda: keyboard.write('foobar'))

# Blocks until you press esc.
keyboard.wait('esc')

# 记录键盘事件直到ESC被按下
recorded = keyboard.record(until='esc')
# 播放键盘事件
keyboard.play(recorded, speed_factor=3)

#别名的用法
# T输入 @@ 然后按下空格可以代替后面的完整字符串.
keyboard.add_abbreviation('@@', 'my.long.email@example.com')

# 永久阻塞, like `while True`.
keyboard.wait()

通过这两个模块可以完成一些键盘鼠标的自动化操作,这里不再举例。

0 人点赞