控制鼠标

 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使用简单说明的更多相关文章

  1. 用python写一个非常简单的QQ轰炸机

    闲的没事,就想写一个QQ轰炸机,按照我最初的想法,这程序要根据我输入的QQ号进行轰炸,网上搜了一下,发现网上的案列略复杂,就想着自己写一个算了.. 思路:所谓轰炸机,就是给某个人发很多信息,一直刷屏, ...

  2. python 监视和控制鼠标键盘的输入(使用pynput 而非pyhook)

    百度上搜到的文章大多基于pyhook, pip不能直接安装,托管在sourceForge上的代码仓库也找不到. google上发现可以使用pynput,貌似控制更为简单,而且可以直接使用pip安装 示 ...

  3. python监听、操作键盘鼠标库pynput详细教程

    § 0.0.0 前言 监听.操作鼠标.键盘是实现自动化的捷径,比如我实现自动化签到用到了模拟键盘操作. pynput是监听.操控鼠标和键盘的跨平台第三方python库. 你可以通过pip insnal ...

  4. 屏幕截图小工具的制作过程问题记录 python PIL pynput pyautogui pyscreeze

    最近想做一个脚本小工具,方便写一些操作说明文档,它的功能很简单,就是把脚本打开之后,鼠标进行操作点击时,会在点击后进行截图,并在图上标记出点击的位置,有点类似于录屏软件的图片版,这样的话,如果要想用文 ...

  5. 【造轮子】打造一个简单的万能Excel读写工具

    大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...

  6. Fabio 安装和简单使用

    Fabio(Go 语言):https://github.com/eBay/fabio Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的 ...

  7. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  8. 哪种缓存效果高?开源一个简单的缓存组件j2cache

    背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...

  9. 在Openfire上弄一个简单的推送系统

    推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...

随机推荐

  1. OLE、OCX和ActiveX控件之间的比较

      OLE(Object Linking and Embedding,对象连接与嵌入) 一.过去的OLE和今天的OLE 最初的OLE含义是指在程序之间链接和嵌入对象数据,它提供了建立混合文档的手段(资 ...

  2. python测试开发django-35.xadmin注册表信息

    前言 xadmin后台如果要对表的内容增删改查,跟之前的admin.py文件里面写注册表信息一样,需在admin.py同一级目录新建一个adminx.py的文件. 然后在adminx.py文件控制页面 ...

  3. 在MFC内竟然不可以解析“Mat”

    调试了好久,在MFC内竟然不可以解析“Mat” 总是显示"Mat"没有定义,改动include.lib.依赖库, 不断地改动: #include "opencv2/img ...

  4. Hyperledger 项目

    https://github.com/hyperledger/fabric.githttps://github.com/hyperledger/blockchain-explorer.githttps ...

  5. IDA修改游戏

    用GM找到机器码 基址   400000 偏移是401940-400000 = 0x1940 UE去到D40位置修改

  6. 用开源项目ExpandableTextView打造可以下拉扩展的TextView

    这次还是用开源项目来实现效果,我个人觉得上面的这个效果还是很赞的.于是就记录下如何实现这个效果,其实相当简单.这就是开源项目写的好的例子,整个开源项目的代码十分清晰,逻辑和代码结构都很棒,接入自己的工 ...

  7. Shape 属性解释

    本文来自:http://blog.csdn.net/brokge/article/details/9713041 简介: 作用:XML中定义的几何形状 位置:res/drawable/文件的名称.xm ...

  8. WinForm自定义控件–TextBox扩展

      一.简单回顾 在前两节中,对Panel和GroupBox控件进行了相关的扩展应用,主要都是设置控件的边框以及边框颜色等.本节,继续对WinForm现有的控件TextBox进行扩展,来满足实际开发中 ...

  9. linux rename命令批量修改文件名

    修改文件名可以用mv命令来实现 mv filename1 filename2 1 但如果批量修改还是使用rename命令更为方便 现在我们有a b c d 四个文件 增加后缀 rename 's/$/ ...

  10. 关于SqlBulkCopy SQL批量导入需要注意,列名是区分大小写的

    最近在做数据从Excel批量导入MSSQL时,传统的是使用Insert Into Table方法,不过这个方便比较慢 通过使用 SqlBulkCopy 可以批量导入到数据库. 默认批量导入数据库,需要 ...