Python Direct mouse Keyboard control

Source: Internet
Author: User
Tags python script

Python Direct mouse Keyboard control

Before because of the reason for the final period has been a long time without blogging, Bo today found a fun module Pyautogui, with it can use Python script directly control keyboard and mouse, feel can solve a lot of boring mechanical movement. Here's a record of learning.

Installation
$ sudo pip install pyautogui

There may be more dependent packages installed, just wait patiently

Basic use
=# 屏幕尺寸=# 返回当前鼠标位置,注意坐标系统中左上方是(0, 0)

In order to maintain the user can at any time interfere with the action of the mouse keyboard, the better way is to add a pause, or force the end, or you want to stop the mouse has been shaking will lose control, the following are the two corresponding settings

=1.5# 每个函数执行后停顿1.5秒=True# 鼠标移到左上角会触发FailSafeException,因此快速移动鼠标到左上角也可以停止
Mouse action Move
= pyautogui.size()pyautogui.moveTo(w/2, h/2# 基本移动pyautogui.moveTo(100200, duration=2# 移动过程持续2s完成pyautogui.moveTo(None500# X方向不变,Y方向移动到500pyautogui.moveRel(-40500# 相对位置移动
Drag
# 点击+向下拖动pyautogui.click(94134, button=‘left‘)pyautogui.dragRel(0100, button=‘left‘, duration=5)
Click
pyautogui.click(300400, button=‘right‘# 包含了move的点击,右键pyautogui.click(clicks=2, interval=0.25# 双击,间隔0.25s
Roller
pyautogui.scroll(-10)
Keyboard operation input Text

Click on the dialog box to enter text, but Chinese doesn't seem to work

pyautogui.click(1279374)pyautogui.typewrite(‘hello world!‘)
Precise keyboard control

A way to manually enter Chinese

pyautogui.press(‘shift‘# 切换输入法的中英文pyautogui.press([‘#‘‘ ‘# press 可以对单个字符或者列表进行操作pyautogui.press([‘x‘‘i‘‘a‘‘o‘])pyautogui.press([‘y‘‘u‘])pyautogui.press([‘w‘‘e‘‘i‘])pyautogui.press([‘l‘‘a‘‘n‘])pyautogui.press(‘ ‘)# 潇雨危栏

HotKey

pyautogui.hotkey(‘shift‘‘a‘# 可以使用组合键,本质上是‘‘‘pyautogui.keyDown(‘shift‘)pyautogui.keyDown(‘a‘)pyautogui.keyUp(‘shift‘)pyautogui.keyUp(‘a‘)‘‘‘
Summary

Pyautogui simulation of the mouse keyboard input, feel the machine seems to be manipulated by the script, the specific application has not thought well, feel as if can solve some crawler problems. The flexibility of this library lies in its application-agnostic nature , which is also the drawback.

Python Direct mouse Keyboard control

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.