前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >用python控制鼠标和键盘

用python控制鼠标和键盘

作者头像
用户6021899
发布于 2021-03-10 07:23:19
发布于 2021-03-10 07:23:19
1.6K00
代码可运行
举报
运行总次数:0
代码可运行

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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
代码运行次数:0
运行
AI代码解释
复制
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()

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-03-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
[QT]获取鼠标坐标以及按键响应
原文链接:https://blog.csdn.net/humanking7/article/details/80707591
祥知道
2020/03/10
4.7K0
Python: pyautogui模块之鼠标控制
文章背景:PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,利用它可以实现自动化任务。pyautogui模块中包含了一些函数,可以模拟鼠标移动、按键和滚动鼠标滚轮。本文对鼠标控制的相关函数进行介绍。
Exploring
2022/08/10
5.4K0
Python: pyautogui模块之鼠标控制
基于python3+pynput进行录制和回放
无意间在网上找到关于python3+pynput下进行监控、录制鼠标、键盘的操作,使得项目自动化测试更完善了。具体代码如下:
用户6367961
2021/08/13
2.2K1
软件测试|web自动化测试神器playwright教程(二十五)
鼠标为我们使用电脑提供了很多方便,我们看到的东西就可以将鼠标移动过去进行点击就可以打开或者访问内容,当页面内容过长时,我们也可以使用鼠标滚轮来实现对整个页面内容的查看,其实playwright也有鼠标操作的方法,本文我们就来介绍playwright的鼠标操作。
霍格沃兹测试开发Muller老师
2023/08/02
2970
【python自动化】Playwright基础教程(八)鼠标操作
playwright模拟鼠标操作,每个page对象都有自己的鼠标事件,可以通过page.mouse调用。
梦无矶小仔
2023/09/28
2.7K0
【python自动化】Playwright基础教程(八)鼠标操作
《最新出炉》系列入门篇-Python+Playwright自动化测试-45-鼠标操作-下篇
鼠标为我们使用电脑提供了很多方便,我们看到的东西就可以将鼠标移动过去进行点击就可以打开或者访问内容,当页面内容过长时,我们也可以使用鼠标滚轮来实现对整个页面内容的查看,其实playwright也有鼠标操作的方法。上一篇文章中已经讲解过鼠标的部分操作了,今天宏哥在这里将剩下的其他操作进行一个详细地介绍和讲解。
北京-宏哥
2024/05/06
2860
python自动化办公--pyautogui控制鼠标和键盘操作
在公司某些工作场景下,需要大量重复的工作,重复的工作完全可以通过python软件的自动化实现,省时省力。本文分享python自动化办公的利器之一--pyautogui,通过pyautogui可以轻松控制鼠标和键盘操作。
用户9925864
2022/07/27
2.2K0
python自动化办公--pyautogui控制鼠标和键盘操作
fvwm 3_三菱vvvf
Changes the menu style of menu to menustyle. You may specify more than one menu in each call of ChangeMenuStyle.
全栈程序员站长
2022/11/09
3320
一个简单的鼠标键盘监控工具
今天有位同事和我吐槽关于公司 XX 的问题,我告诉他不要在公司电脑上说这些,因为很可能会被狙击,这位同事刚开始还不信,直到我写了这边文章,他才恍然大悟。
Python研究所
2022/06/17
1.3K0
一个简单的鼠标键盘监控工具
python pyautogui 键盘鼠标自动化
1、安装模块: 在Windows 上,不需要安装其他模块。  在OS X 上,运行sudo pip3 install pyobjc-framework-Quartz,sudo pip3 install pyobjc-core,然后sudo pip3 install pyobjc。  在Linux 上,运行sudo pip3 install python3-xlib,sudo apt-get install scrot,sudo apt-get install python3-tk,以及sudo apt-get install python3-dev(Scrot 是 PyAutoGUI 使用的屏幕快照程序)。 在这些依赖安装后,运行pip install pyautogu(i 或在OS X和Linux上运行pip3), 安装pyautogui。 2、pyautogui执行时,如果鼠标移到屏幕左上角,将导致pyautogui产生pyautogui.FailSafeException异常。如果设置FAILSAEF=False将禁止这项功能。
用户5760343
2022/05/13
1.4K0
推荐一个比较好的操作鼠标键盘的python库
最近由于在家办公,很多东西在家没法访问。 于是我想自动操作,将daily build放到teams的公司共享盘里。这样就可以在家访问了。 结果遇到了一个难题。文件上传框是系统的。没法点。 先考虑autoit,感觉不是很理想。 然后用到了 pykeyboard.PyKeyboard() 和pymouse.PyMouse(),感觉也不是太理想 后面还尝试过pyautoit,也不理想。 最后找到了pyautogui
赵云龙龙
2020/12/15
8K0
推荐一个比较好的操作鼠标键盘的python库
AutoKey - 适用于Linux和X11的桌面自动化应用程序
在当今软件开发领域,质量保证是项目成功的关键因素之一。随着软件迭代速度不断加快,传统手动测试方法难以满足与日俱增的测试需求,因此,自动化测试工具的重要性愈发突出。
wangmcn
2024/11/14
2110
AutoKey - 适用于Linux和X11的桌面自动化应用程序
Gazebo機器人仿真學習探索筆記(一)安裝與使用
Gazebo提供了多平臺的安裝和使用支持,大部分主流的linux,Mac以及Windows,這裏結合ROS以Ubuntu爲例進行介紹。
zhangrelay
2019/01/23
6.3K0
跨平台 scrcpy显示/控制安卓手机方案
这里推荐一款开源免费, 跨平台支持 Win、Mac、Linux,可通过 USB 数据线 (或WiFi) 连接电脑,将手机画面投屏到电脑显示,并可使用键盘鼠标远程控制你的手机
acc8226
2022/05/17
1.4K0
vue中v-on支持的事件总结
checking downloading error noupdate obsolete updateready
samRsa
2025/02/24
750
python 捕捉和模拟鼠标键盘操作
使用的python版本为:3.6.1,使用anaconda配置的python环境 参考博文
py3study
2020/01/08
3.8K0
Godot进行2D游戏开发入门-按键事件
码客说
2023/08/08
4870
windows 桌面GUI自动化- 16.pywinauto 鼠标操作与滚动列表
上海-悠悠
2023/09/11
8640
windows 桌面GUI自动化- 16.pywinauto 鼠标操作与滚动列表
基于Guava API实现异步通知和事件回调
当小伙伴们在社区提问时,如果有设置指定用户回答,则对应的用户就会收到邮件通知,这就是观察者模式的一种应用场景。有些小伙伴可能会想到MQ、异步队列等,其实JDK本身就提供这样的API。我们用代码来还原这样一个应用场景,首先创建GPer类。
Tom弹架构
2021/11/17
7250
键盘和鼠标的隐形观察者:用Python的pynput库记录每一个动作
在数字时代,的每一次键盘敲击和鼠标点击都可能泄露信息。但如果能够控制这一过程,又将如何利用这些数据呢?Python的pynput库正是这样一个工具,它能够让捕捉并记录键盘和鼠标的动作。但在开始探索这个强大功能之前,让先讨论一下这是否触及了隐私的边界。
木头左
2024/06/23
5690
推荐阅读
相关推荐
[QT]获取鼠标坐标以及按键响应
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验