Python控制键盘鼠标pynput的详细用法


Posted in Python onJanuary 28, 2019

pynput这个库让你可以控制和监控输入设备。

对于每一种输入设备,它包含一个子包来控制和监控该种输入设备:

  • pynput.mouse:包含控制和监控鼠标或者触摸板的类。
  • pynput.keyboard:包含控制和监控键盘的类。

地址:https://pypi.python.org/pypi/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)个距离
mouse.move(5, -5)
print(mouse.position)

mouse.press(Button.left)
mouse.release(Button.left)

#Double click
mouse.click(Button.left, 1)

#scroll two steps down
mouse.scroll(0, 500)

监控鼠标事件 :

from pynput import mouse

def on_move(x, y ):
 print('Pointer moved to {o}'.format(
  (x,y)))

def on_click(x, y , button, pressed):
 print('{0} at {1}'.format('Pressed' if pressed else 'Released', (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) as listener:
  listener.join()

键盘输入用法:

from pynput.keyboard import Key, Controller

keyboard = Controller()
# 按下空格和释放空格
#Press and release space
keyboard.press(Key.space)
keyboard.release(Key.space)
# 按下a键和释放a键
#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 
with keyboard .pressed(Key.shift):
 keyboard.press('a')
 keyboard.release('a')

#type 'hello world ' using the shortcut type method
keyboard.type('hello world')

键盘监听:

from pynput import keyboard

def on_press(key):
 try:
  print('alphanumeric key {0} pressed'.format(key.char))
 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) as listener:
  listener.join()

对于鼠标来说,api就上面几个。但是对于键盘来说还要别的,详细的查看:http://pythonhosted.org/pynput/index.html

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
讲解Python中if语句的嵌套用法
May 14 Python
在Python的while循环中使用else以及循环嵌套的用法
Oct 14 Python
Python多进程multiprocessing用法实例分析
Aug 18 Python
详谈Python高阶函数与函数装饰器(推荐)
Sep 30 Python
使用python实现ANN
Dec 20 Python
Python中实现变量赋值传递时的引用和拷贝方法
Apr 29 Python
详解python 注释、变量、类型
Aug 10 Python
Python爬虫常用库的安装及其环境配置
Sep 19 Python
python3 cvs将数据读取为字典的方法
Dec 22 Python
selenium+Chrome滑动验证码破解二(某某网站)
Dec 17 Python
Python如何用filter函数筛选数据
Mar 05 Python
Python监听剪切板实现方法代码实例
Nov 11 Python
用python 实现在不确定行数情况下多行输入方法
Jan 28 #Python
对python3中, print横向输出的方法详解
Jan 28 #Python
Python删除n行后的其他行方法
Jan 28 #Python
python 在指定范围内随机生成不重复的n个数实例
Jan 28 #Python
Python实现统计英文文章词频的方法分析
Jan 28 #Python
Python3实现统计单词表中每个字母出现频率的方法示例
Jan 28 #Python
Python判断变量名是否合法的方法示例
Jan 28 #Python
You might like
php 无限级数据JSON格式及JS解析
2010/07/17 PHP
在Laravel的Model层做数据缓存的实现
2019/09/26 PHP
激活 ActiveX 控件
2006/10/09 Javascript
JavaScript写的一个DIV 弹出网页对话框
2009/08/14 Javascript
你必须知道的JavaScript 中字符串连接的性能的一些问题
2013/05/07 Javascript
jQuery 三击事件实现代码
2013/09/11 Javascript
使用jquery实现的一个图片延迟加载插件(含图片延迟加载原理)
2014/06/05 Javascript
编写简单的jQuery提示插件
2014/12/21 Javascript
javascript操作ul中li的方法
2015/05/14 Javascript
Javascript 计算字符串在localStorage中所占字节数
2015/10/21 Javascript
js实现的光标位置工具函数示例
2016/10/03 Javascript
js基础之DOM中document对象的常用属性方法详解
2016/10/28 Javascript
基于JavaScript实现全选、不选和反选效果
2017/02/15 Javascript
js实现tab切换效果
2017/02/16 Javascript
Bootstrap实现下拉菜单多级联动
2017/11/23 Javascript
使用Angular 6创建各种动画效果的方法
2018/10/10 Javascript
[53:10]2018DOTA2亚洲邀请赛 4.6 淘汰赛 VP vs VG 第一场
2018/04/11 DOTA
详解Python中的多线程编程
2015/04/09 Python
PyCharm的设置方法和第一个Python程序的建立
2019/01/16 Python
Python如何获得百度统计API的数据并发送邮件示例代码
2019/01/27 Python
python求最大值,不使用内置函数的实现方法
2019/07/09 Python
图文详解Django使用Pycharm连接MySQL数据库
2019/08/09 Python
巴西男士个人护理产品商店:SHOP4MEN
2017/08/07 全球购物
New Balance法国官方网站:购买鞋子和服装
2019/09/01 全球购物
英国最大的在线快递公司之一:ParcelHero
2019/11/04 全球购物
美国精品地毯网站:Boutique Rugs
2020/03/04 全球购物
俄罗斯建筑和装饰材料在线商店:Stroilandia
2020/07/25 全球购物
英文求职信结束语大全
2013/10/26 职场文书
北体毕业生求职信
2014/02/28 职场文书
医学院毕业生自荐信范文
2014/03/06 职场文书
司法工作人员群众路线对照检查材料思想汇报
2014/09/30 职场文书
关于工作经历的证明书
2014/10/11 职场文书
销售内勤岗位职责
2015/02/10 职场文书
刑事附带民事诉讼答辩状
2015/05/22 职场文书
Python基础之pandas数据合并
2021/04/27 Python
使用ORM新增数据在Mysql中的操作步骤
2021/07/26 MySQL