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上弄一个简单的推送系统
推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...
随机推荐
- SharePoint JavaScript API 根据文件路径删除文件
最近,有这么个需求,然后写了几行代码,记录一下.有需要的可以参考一下. 有几个需要注意的地方,就是文件URL要传相对地址,使用网站对象之前要Load一下. 当然,如果你的网站不在根路径下,还可以用oW ...
- Lua游戏开发参考资料收集
table {border-collapse:collapse;} List of game engines that use lua: 2D Agen (Lua; Windows) Blitwiza ...
- 机器翻译评价指标之BLEU详细计算过程
原文连接 https://blog.csdn.net/guolindonggld/article/details/56966200 1. 简介 BLEU(Bilingual Evaluation Un ...
- org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.util.Date from String value '20Spring Jackson 反序列化Date时遇到的问题
Jackson对于date的反序列化只支持几种,如果不符合默认格式则会报一下错误 org.codehaus.jackson.map.JsonMappingException: Can not cons ...
- cocos2d-js中Chipmunk物理引擎相关(1)
近期看些cocos2d-js的东西.用到当中的Chipmunk的一些东西.由于相关的资料也不是非常具体,所以看到一些东西实用就记录下来. 1. chipmunk是cocos2d的一个一个物理引擎.用来 ...
- XlsToOra
ylbtech-Miscellaneos:XlsToOra 1. 返回顶部 在没有安装Oracle客户端而又需要将Excel数据导入Oracle表中时,XlsToOra可以方便快捷的达到你的目的. ...
- cull/clip distance example
http://www.gamedev.net/topic/578866-d3d10-how-to-increase-maxcount-of-sv_clipdistance/ The D3D#_CL ...
- go语言之进阶篇文件常用操作接口介绍和使用
一.文件常用操作接口介绍 1.创建文件 法1: 推荐用法 func Create(name string) (file *File, err Error) 根据提供的文件名创建新的文件,返回一个文件对 ...
- Permutations leetcode java
题目: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the f ...
- Dll 导出函数那些破事
经常使用VC6的Dependency查看DLL导出函数的名字,会发现有DLL导出函数的名字有时大不相同,导致不同的原因大多是和编译DLL时候指定DLL导出函数的界定符有关系. VC++支持两种语言:即 ...