首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Behavior Simulation of mouse and keyboard in Python

We can use Python to control our mouse and keyboard and let them finish some tasks automatically, such as pressing and dragging.

Preparation

We use Python packagepyautoguito achieve the goal. To install the package, if you use windows system, you just need to run:

if you useosxsystem, you need to install three packages instead of pip install it directly:

The packagetreatthe computer screen by pixel. We use

to get the size of our screen. Sometimes to delay the movement of mouse and keyboard, we need package time and function sleep.

Mouse

We mainly use three kinds of functions: click, move and drag.

Click

Click function simulates the mouse click.

We can control the position, left-button/right-button, times of click by parameters.

It simulates the behavior that we right-click the mouse twice at the location (100,100) on our screen.

Move

Move function simulates the mouse moving. There are mainly two kinds of function: moveTo and moveRel. MoveTo function let the mouse movetothe position (x,y).moveRellet the mouse move (x,y) pixelsrelativeto the current position ofmouse. Another parameter isduration, which controls the time of moving.

Drag

Drag function simulates the dragging ofmouse. Just like move function, there are two kinds:dragToanddragRel. One is to thepopsitionand the other is to drag relative to the current position.

Keyboard

We can simulate the pressing and typewriting onkeyboard.

Press

We use press function mainly to press the keys which are not letters and numbers(such as 'enter', 'shift'). We can also press several keys at the same time.

typewrite

We use typewrite function to press the letters and numbers. Also, we can use it to type long string.

Reference

[1] Introduction of PyAutoGUI : https://pyautogui.readthedocs.io/en/latest/introduction.html

[2] 使用 Python 实现鼠标键盘自动化 2 : https://python.freelycode.com/contribution/detail/579

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180210G022JA00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券