pynput使用简单说明
控制鼠标
from pynput.mouse import Button, Controller
import time mouse = Controller()
print(mouse.position)
time.sleep(3)
print('The current pointer position is {0}'.format(mouse.position)) #set pointer positon
mouse.position = (277, 645)
print('now we have moved it to {0}'.format(mouse.position)) #鼠标移动(x,y)个距离
#param int x: The horizontal offset.
#param int dy: The vertical offset.
mouse.move(5, -5)
print(mouse.position) mouse.press(Button.left)
mouse.release(Button.left) mouse.press(Button.right)
mouse.release(Button.right) #Double click
#param int count: The number of clicks to send.
mouse.click(Button.left, 2) #scroll two steps down
#param int dx: The horizontal scroll.
#param int dy: The vertical scroll.
mouse.scroll(0, 500)
监听鼠标
'''
:param callable on_move: The callback to call when mouse move events occur. It will be called with the arguments ``(x, y)``, which is the new
pointer position. If this callback raises :class:`StopException` or
returns ``False``, the listener is stopped. :param callable on_click: The callback to call when a mouse button is
clicked. It will be called with the arguments ``(x, y, button, pressed)``,
where ``(x, y)`` is the new pointer position, ``button`` is one of the
:class:`Button` values and ``pressed`` is whether the button was
pressed. If this callback raises :class:`StopException` or returns ``False``,
the listener is stopped. :param callable on_scroll: The callback to call when mouse scroll
events occur. It will be called with the arguments ``(x, y, dx, dy)``, where
``(x, y)`` is the new pointer position, and ``(dx, dy)`` is the scroll
vector. If this callback raises :class:`StopException` or returns ``False``,
the listener is stopped. :param bool suppress: Whether to suppress events. Setting this to ``True``
will prevent the input events from being passed to the rest of the
system.
''' from pynput import mouse
from pynput.mouse import Button def on_move(x, y):
print('Pointer moved to {o}'.format((x,y))) def on_click(x, y , button, pressed):
button_name = ''
#print(button)
if button == Button.left:
button_name = 'Left Button'
elif button == Button.middle:
button_name = 'Middle Button'
elif button == Button.right:
button_name = 'Right Button'
else:
button_name = 'Unknown'
if pressed:
print('{0} Pressed at {1} at {2}'.format(button_name, x, y))
else:
print('{0} Released at {1} at {2}'.format(button_name, x, y))
if not pressed:
return False def on_scroll(x, y ,dx, dy):
print('scrolled {0} at {1}'.format(
'down' if dy < 0 else 'up',
(x, y))) while True:
with mouse.Listener( no_move = on_move,on_click = on_click,on_scroll = on_scroll,suppress = False) as listener:
listener.join()
控制键盘
'''
['alt', 'alt_l', 'alt_r', 'backspace', 'caps_lock', 'cmd', 'cmd_r', 'ctrl', 'ctrl_l', 'ctrl_r', 'delete',
'down', 'end', 'enter', 'esc', 'f1', 'f10', 'f11', 'f12', 'f13', 'f14', 'f15', 'f16', 'f17', 'f18', 'f19', 'f2', 'f20',
'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'home', 'insert', 'left', 'menu', 'num_lock', 'page_down', 'page_up', 'pause',
'print_screen', 'right', 'scroll_lock', 'shift', 'shift_r', 'space', 'tab', 'up']
''' from pynput.keyboard import Key, Controller keyboard = Controller() #Press and release space
keyboard.press(Key.space)
keyboard.release(Key.space) #Type a lower case A ;this will work even if no key on the physical keyboard is labelled 'A'
keyboard.press('a')
keyboard.release('a') #Type two upper case As
keyboard.press('A')
keyboard.release('A')
# or
#Executes a block with some keys pressed. param keys: The keys to keep pressed.
with keyboard.pressed(Key.shift): #组合按键
keyboard.press('a')
keyboard.release('a') #type 'hello world ' using the shortcut type method
#This method will send all key presses and releases necessary to type all characters in the string.
#param str string: The string to type.
keyboard.type('hello world') keyboard.touch('&', True)
keyboard.touch('&', False) keyboard.press(Key.print_screen)
keyboard.release(Key.print_screen) with keyboard.pressed(Key.ctrl): #组合按键
keyboard.press('s')
keyboard.release('s')
监听键盘
from pynput import keyboard #alt_pressed、alt_gr_pressed、ctrl_pressed、shift_pressed def on_press(key):
try:
print('alphanumeric key {0} pressed'.format(key.char)) #应该记录下之前有没有ctrl、alt、和shift按下
except AttributeError:
print('special key {0} pressed'.format(key)) def on_release(key):
print('{0} released'.format(key))
if key == keyboard.Key.esc:
return False while True:
with keyboard.Listener(
on_press = on_press,
on_release = on_release,
suppress = False) as listener:
listener.join()
pynput使用简单说明的更多相关文章
- 用python写一个非常简单的QQ轰炸机
闲的没事,就想写一个QQ轰炸机,按照我最初的想法,这程序要根据我输入的QQ号进行轰炸,网上搜了一下,发现网上的案列略复杂,就想着自己写一个算了.. 思路:所谓轰炸机,就是给某个人发很多信息,一直刷屏, ...
- python 监视和控制鼠标键盘的输入(使用pynput 而非pyhook)
百度上搜到的文章大多基于pyhook, pip不能直接安装,托管在sourceForge上的代码仓库也找不到. google上发现可以使用pynput,貌似控制更为简单,而且可以直接使用pip安装 示 ...
- python监听、操作键盘鼠标库pynput详细教程
§ 0.0.0 前言 监听.操作鼠标.键盘是实现自动化的捷径,比如我实现自动化签到用到了模拟键盘操作. pynput是监听.操控鼠标和键盘的跨平台第三方python库. 你可以通过pip insnal ...
- 屏幕截图小工具的制作过程问题记录 python PIL pynput pyautogui pyscreeze
最近想做一个脚本小工具,方便写一些操作说明文档,它的功能很简单,就是把脚本打开之后,鼠标进行操作点击时,会在点击后进行截图,并在图上标记出点击的位置,有点类似于录屏软件的图片版,这样的话,如果要想用文 ...
- 【造轮子】打造一个简单的万能Excel读写工具
大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...
- Fabio 安装和简单使用
Fabio(Go 语言):https://github.com/eBay/fabio Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的 ...
- node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理
一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...
- 哪种缓存效果高?开源一个简单的缓存组件j2cache
背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...
- 在Openfire上弄一个简单的推送系统
推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...
随机推荐
- Java实现用汉明距离进行图片相似度检测的
Google.Baidu 等搜索引擎相继推出了以图搜图的功能,测试了下效果还不错~ 那这种技术的原理是什么呢?计算机怎么知道两张图片相似呢? 根据Neal Krawetz博士的解释,原理非常简单易懂. ...
- IOS应用提交所需的ICON
如果提交的ipa包中,未包含必要的Icon就会收到类似的通知,为什么偏偏是Icon-76呢? 因为我们开发的游戏,默认是支持iphone以及ipad的,根据官方提供的参考 Icon-76.png是必须 ...
- 用SimpleAdapter来设置ListView的内容
Mainactivit.java package com.kale.listview; import java.util.ArrayList; import java.util.HashMap; im ...
- C#零基础入门07:打老鼠之面向对象重构
一:前言 有了上面两节的知识,尤其是第六节之后,现在我们回过头看我们的打老鼠游戏,我们是不是会发现:这个程序也太不面向对象了.我们所有的代码逻辑都分布在Code-Hide中(UI的后台代码,称之为Co ...
- android R.layout 中找不到已存在的布局文件
在R.layout.test文件时,总是找不到您想要的文件,可是它明明就在layout文件下面,而且在R.Java中也已经生成了,那么找不到的原因就是你导入了Android.R的包,这样你永远找不到你 ...
- CImg、libjpeg--介绍、配置(操作JPEG)
关于处理图片,之前写了两篇博客关于ImageMagick的: <ImageMagick–介绍> <ImageMagick–VS2015环境配置.开发(registrykeylooku ...
- 解析eml文件
之前使用lumisoft解析eml,总是会出现很奇怪的问题,所以改使用微软自家的com库,确实厉害兼容性更好,代码 string file = emailPath; CDO.Message oMsg ...
- [leetcode]Palindrome Partitioning @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s suc ...
- ScaleIO 1.32现在可以免费下载安装使用了(除生产环境之外)
EMC World 2015会上宣布, ScaleIO 1.32可以免费下载使用在非生产环境上了. 这个可以免费下载的版本叫做Free and Drictionless (F&F) downl ...
- 领扣-120 三角形最小路径和 Triangle MD
三角形最小路径和 Triangle 数组 动态规划 问题 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [2], [3,4], [6,5,7], ...