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上弄一个简单的推送系统
推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...
随机推荐
- 递归搜寻NSString中重复的文本
递归搜寻NSString中重复的文本 效果 源码 https://github.com/YouXianMing/iOS-Project-Examples 中的 StringRange 项目 // // ...
- 疑犯追踪第一季/全集Person Of Interest迅雷下载
本季Person of Interest Season 1 第一季(2011)看点:如今,<疑犯追踪>正在纽约热拍,在11月1日的片场,刚刚完成了一场爆炸的戏.另外,<探索者传说第一 ...
- 危机边缘第五季/全集Fringe迅雷下载
本季Fringe Season 5 第五季(2012)看点:Walter 用琥珀将Olivia.Peter.Astrid和自己封存,以便在2036年的未来世界实现自己抵抗观察者的完美计划,“解冻”后的 ...
- 使用Aptana Studio 3开发让Extjs变的更简单
工欲善其事必先利器,做EXTJS开发先整好IDE,为后续的项目应用做准备. 下载地址 http://www.aptana.com/products/studio3/download# 下载汉化包 配置 ...
- Android日志监听工具logcat命令详解(转)
Android日志系统提供了记录和查看系统调试信息的功能.日志都是从各种软件和一些系统的缓冲区中记录下来的,缓冲区可以通过logcat命令来查看和使用. 在使用logcat之前,请确保手机的USB调试 ...
- java HttpServletRequest和HttpServletResponse詳解
這篇文章主要介紹瞭java HttpServletRequest和HttpServletResponse詳解的相關資料,需要的朋友可以參考下 java HttpServletRequest和HttpS ...
- mongodb分布式集群搭建手记
一.架构简介 目标单机搭建mongodb分布式集群(副本集 + 分片集群),演示mongodb分布式集群的安装部署.简单操作. 说明在同一个vm启动由两个分片组成的分布式集群,每个分片都是一个PSS( ...
- ASP.NET MVC:WebPageBase.cs
ylbtech-funcation-Utility: ASP.NET MVC:WebPageBase.cs 充当表示 ASP.NET Razor 页的类的基类. 1.A,WebPageBase 抽象类 ...
- 案例导入和导出Scott用户
ylbtech-Oracle:案例导入和导出Scott用户 导入和导出Scott用户 1. 导出Scott用户下的所有对象返回顶部 1.1, Microsoft Windows [版本 ] 版权所有 ...
- NVelocity语法常用指令
对变量的引用:$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]. 在NVelocity中,对变量的引用都是以$开头加上变量名称.当使用 ...