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参数和作用域的使用
Nov 01 Python
Python实现3行代码解简单的一元一次方程
Aug 18 Python
python学习之面向对象【入门初级篇】
Jan 21 Python
Python解析并读取PDF文件内容的方法
May 08 Python
完美解决python中ndarray 默认用科学计数法显示的问题
Jul 14 Python
Python 中Django安装和使用教程详解
Jul 03 Python
python之拟合的实现
Jul 19 Python
python列表每个元素同增同减和列表元素去空格的实例
Jul 20 Python
python自动化实现登录获取图片验证码功能
Nov 20 Python
利用Python实现Excel的文件间的数据匹配功能
Jun 16 Python
python实现学生信息管理系统源码
Feb 22 Python
一文读懂python Scrapy爬虫框架
Feb 24 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
检查url链接是否已经有参数的php代码 添加 ? 或 &amp;
2010/02/09 PHP
PHP仿博客园 个人博客(2) 数据库增添改删
2013/07/05 PHP
ThinkPHP惯例配置文件详解
2014/07/14 PHP
php插入含有特殊符号数据的处理方法
2016/11/24 PHP
PHP实现将上传图片自动缩放到指定分辨率,并保持清晰度封装类示例
2019/06/17 PHP
THINKPHP5.1 Config的配置与获取详解
2020/06/08 PHP
js实现GridView单选效果自动设置交替行、选中行、鼠标移动行背景色
2010/05/27 Javascript
根据对象的某一属性进行排序的js代码(如:name,age)
2010/08/10 Javascript
JavaScript 对任意元素,自定义右键菜单的实现方法
2013/05/08 Javascript
jQuery自动切换/点击切换选项卡效果的小例子
2013/08/12 Javascript
jquery validate在ie8下的bug解决方法
2013/11/13 Javascript
js写的方法实现上传图片之后查看大图
2014/03/05 Javascript
js css+html实现简单的日历
2016/07/14 Javascript
原生JS实现图片轮播与淡入效果的简单实例
2016/08/21 Javascript
JS实现购物车特效
2017/02/02 Javascript
[51:17]完美世界DOTA2联赛循环赛Inki vs DeMonsTer 第二场 10月30日
2020/10/31 DOTA
[36:33]完美世界DOTA2联赛PWL S2 LBZS vs Forest 第二场 11.29
2020/12/02 DOTA
python多线程编程方式分析示例详解
2013/12/06 Python
Python中使用不同编码读写txt文件详解
2015/05/28 Python
python下10个简单实例代码
2017/11/15 Python
matplotlib中legend位置调整解析
2017/12/19 Python
pandas把dataframe转成Series,改变列中值的类型方法
2018/04/10 Python
python使用MQTT给硬件传输图片的实现方法
2019/05/05 Python
Django框架文件上传与自定义图片上传路径、上传文件名操作分析
2019/05/10 Python
python实现随机漫步方法和原理
2019/06/10 Python
Windows10下Tensorflow2.0 安装及环境配置教程(图文)
2019/11/21 Python
三步解决python PermissionError: [WinError 5]拒绝访问的情况
2020/04/22 Python
基于python实现百度语音识别和图灵对话
2020/11/02 Python
免税水晶:Duty Free Crystal
2019/05/13 全球购物
党校培训自我鉴定范文
2014/04/10 职场文书
物业管理专业自荐信
2014/07/01 职场文书
教师竞聘上岗演讲稿
2014/09/03 职场文书
市场营销工作计划书
2014/09/15 职场文书
安全生产月标语
2014/10/07 职场文书
mysql事务对效率的影响分析总结
2021/10/24 MySQL