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

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

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

基本用法介绍:

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))#设置鼠标位置
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 mousedef 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 Falsedef 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, Controllerkeyboard = 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 keyboarddef 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 Falsewhile True:with keyboard.Listener(on_press = on_press,on_release = on_release) as listener:listener.join()

python使用笔记:pynput库控制键盘鼠标相关推荐

  1. python学习笔记--turtle库的使用

    turtle库的调用方法 python中其他库的调用方法也一样 1.import <库名> 使用方法:<库名>.<函数名>(函数参数) 使用这种方法的好处就是,当一 ...

  2. Lawliet|Python学习笔记——turtle库

    turtle库--turtle绘图体系的python实现 python的标准库之一 入门级的图形绘制函数库 1. 窗体设置 turtle.setup(width,height,startx,start ...

  3. Lawliet|Python学习笔记——numpy库

    1.numpy库认知 Numpy:表达N维数组的最基础库 库由c语言实现,Python来接口使用,计算速度优异 是Python数据分析及科学计算的基础库,支撑Pandas等 提供直接的矩阵运算.广播函 ...

  4. Lawliet|Python学习笔记——time库

    Time库--Python中处理时间的标准库 计算机时间的表达 提供获取系统时间并格式化输出功能 提供系统级精确计时功能,用于程序性能分析 import time time.<b>()#t ...

  5. Lawliet|Python学习笔记——random库

    Random库-- 是使用随机数的python标准库 伪随机数:采用梅森旋转算法生成的(伪)随机序列中元素 random库主要用于生成随机数 import random 1.基本随机数函数 随机数种子 ...

  6. Python学习笔记-Turtle库简单实例之日期时间显示

    Python语言包含有不少图形工具包,强如Tkinter,底层如python的内置图形库Graphics和Turtle.Turtle库就是通过控制屏幕上小乌龟的移动轨迹绘制图形,本文的一系列操作主要和 ...

  7. Python学习笔记之库使用-第三方库tabulate

    介绍:tabulate能够对二维数据进行表格输出,可以把列表进行表格格式化,对里面的参数进行调用 tabulate.tabulate(tabular_data, headers=(), tablefm ...

  8. python中的urllib库_Python2/3中的urllib库

    介绍urllib库在不同版本的Python中的变动,并以Python3.X讲解urllib库的相关用法. urllib库对照速查表 Python2.X Python3.X urllib urllib. ...

  9. Python学习笔记——照片换底色

    1.去除图片背景 每月免费次数50次 参考: Python学习笔记--removebg库之抠图 2.生成蓝色图片和拼接两张 参考: Python学习笔记--PIL库(Pillow库) 3.代码 注意: ...

最新文章

  1. Java项目:朴素风个人博客系统(前后端分离+java+vue+Springboot+ssm+mysql+maven+redis)
  2. python可以写接口吗_用python写api接口吗
  3. linux top命令VIRT,RES,SHR,DATA的含义
  4. kotlin 协程异常处理机制颠覆三观
  5. BackTrack 5 发布了 ---直接下载地址
  6. 程序员成熟的标志《程序员成长路线图:从入门到优秀》
  7. mysql自定义排序以及优化like模糊查询
  8. Linux基本命令(一)
  9. java web web.xml配置_web.xml配置详解
  10. 基于51单片机+SG90舵机
  11. 调试svo 中遇到的问题
  12. http Headers字段汇总
  13. 【渝粤教育】 国家开放大学2020年春季 1054流通概论 参考试题
  14. 玄幻:我!收徒就变强!(三)
  15. 关于Python列表解析式以及初始化指定size列表
  16. 调停者模式 java_《JAVA与模式》之调停者模式
  17. 2022-2028全球及中国健康资讯交换(HIE)行业研究及十四五规划分析报告
  18. ivms虚拟服务器,ivms监控服务器地址
  19. 2020中国项目经理视频直播大会成功举办
  20. unity 手机游戏优化(场景篇)

热门文章

  1. 怎么添加新项目到svn服务器,用eclipse+svn插件,上传新项目到svn服务器
  2. 水磨石地面分隔条设置示意图_水磨石抛光过程中什么时候用百洁垫?什么时候用百亮钢丝棉?...
  3. seaborn系列 (15) | 双变量关系图jointplot()
  4. ARZIO让AR应用制作变得更简单
  5. 阿里达摩院百万大奖评选开启!这次人人都能给青年科学家当伯乐
  6. 大凉山的美术课,怎么就跟英特尔扯上关系了
  7. 用美颜照当广告犯法!要么就标注“照骗”,挪威针对明星网红出手了
  8. 英伟达“神笔马良”GauGAN发布Windows应用程序,可导出PSD文件
  9. 今年央视春晚的创意担当,给了这个1岁的拓荒牛机器人
  10. QQ被曝自动读取浏览器记录,Chrome、Edge和360等无一幸免