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实现从字典中删除元素的方法
May 04 Python
Python3读取文件常用方法实例分析
May 22 Python
Python 26进制计算实现方法
May 28 Python
Python+matplotlib实现填充螺旋实例
Jan 15 Python
Python编程scoketServer实现多线程同步实例代码
Jan 29 Python
python 正确保留多位小数的实例
Jul 16 Python
python GUI实现小球满屏乱跑效果
May 09 Python
Python异常处理例题整理
Jul 07 Python
简单了解django索引的相关知识
Jul 17 Python
解决Djang2.0.1中的reverse导入失败的问题
Aug 16 Python
Python连接Hadoop数据中遇到的各种坑(汇总)
Apr 14 Python
Python填充任意颜色,不同算法时间差异分析说明
May 16 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执行速率优化技巧小结
2008/03/15 PHP
php 正则 过滤html 的超链接
2009/06/02 PHP
windows下的WAMP环境搭建图文教程(推荐)
2017/07/27 PHP
laravel实现上传图片的两种方式小结
2019/10/12 PHP
使javascript也能包含文件
2006/10/26 Javascript
把textarea中字符串里含有的回车换行替换成&amp;lt;br&amp;gt;的javascript代码
2007/04/20 Javascript
JQuery入门——事件切换之hover()方法应用介绍
2013/02/05 Javascript
javascript模拟枚举的简单实例
2014/03/06 Javascript
总结Javascript中数组各种去重的方法
2016/10/04 Javascript
详谈jQuery Ajax(load,post,get,ajax)的用法
2017/03/02 Javascript
微信小程序之滚动视图容器的实现方法
2017/09/26 Javascript
vue2.0安装style/css loader的方法
2018/03/14 Javascript
详解封装基础的angular4的request请求方法
2018/06/05 Javascript
AngularJs分页插件使用详解
2018/06/30 Javascript
JS根据json数组多个字段排序及json数组常用操作
2019/06/06 Javascript
使用 Vue 实现一个虚拟列表的方法
2019/08/20 Javascript
零基础写python爬虫之urllib2中的两个重要概念:Openers和Handlers
2014/11/05 Python
python实现定时播放mp3
2015/03/29 Python
python操作字典类型的常用方法(推荐)
2016/05/16 Python
python八大排序算法速度实例对比
2017/12/06 Python
浅谈python数据类型及类型转换
2017/12/18 Python
Python下使用Scrapy爬取网页内容的实例
2018/05/21 Python
解决Matplotlib图表不能在Pycharm中显示的问题
2018/05/24 Python
Python使用socketServer包搭建简易服务器过程详解
2020/06/12 Python
Python 绘制可视化折线图
2020/07/22 Python
推荐10个CSS3 制作的创意下拉菜单效果
2014/02/11 HTML / CSS
美国在线家居装饰店:Belle&June
2018/10/24 全球购物
SEPHORA丝芙兰德国官方购物网站:化妆品、护肤品和香水
2020/01/21 全球购物
.NET remoting的两种通道是什么
2016/05/31 面试题
光盘行动倡议书
2014/02/02 职场文书
翻译学院毕业生自荐书
2014/02/02 职场文书
酒店总经理助理职责
2014/02/12 职场文书
生产文员岗位职责
2014/04/05 职场文书
经贸日语专业个人求职信范文
2014/04/29 职场文书
师范生见习总结范文
2015/06/23 职场文书
MySQL系列之九 mysql查询缓存及索引
2021/07/02 MySQL