博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python控制键盘鼠标pynput
阅读量:3898 次
发布时间:2019-05-23

本文共 2104 字,大约阅读时间需要 7 分钟。

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

这篇文章主要介绍了Python控制键盘鼠标pynput的详细用法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

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

pynput.keyboard:包含控制和监控键盘的类。
地址:https://pypi.python.org/pypi/pynput

基本用法介绍:

from pynput.mouse import Button, Controllerimport time mouse = Controller()print(mouse.position)time.sleep(3)print('The current pointer position is {0}'.format(mouse.position))#set pointer positonmouse.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 clickmouse.click(Button.left, 1)#scroll two steps downmouse.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 spacekeyboard.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 Askeyboard.press('A')keyboard.release('A')# or with keyboard .pressed(Key.shift): keyboard.press('a') keyboard.release('a')#type 'hello world ' using the shortcut type methodkeyboard.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()

转载地址:http://neben.baihongyu.com/

你可能感兴趣的文章
TCP与UDP收发的时候TCP有缓冲区还是UDP有缓冲区,使用它们时该注意什么?
查看>>
C++中map、hash_map、unordered_map、unordered_set通俗辨析
查看>>
clone的fork与pthread_create创建线程有何不同&pthread多线程编程的学习小结
查看>>
运算符重载参数的顺序对运算是否有影响
查看>>
什么时候要用虚析构函数?
查看>>
序列化、反序列化与jsoncpp学习
查看>>
同步/异步与阻塞非阻塞的关系
查看>>
epoll模型讲解/源码分析
查看>>
java继承 long和float小记点
查看>>
记录几点在开发中遇到的问题 2015-7-28 (会更新)
查看>>
网银在线的异步操作代码示意图
查看>>
火狐Firefox浏览器安装Selenium_IDE的步骤以及其使用规则
查看>>
记录运行代码的时间长短
查看>>
关于yii2的一些知识的学习笔述
查看>>
用纯php实现MVC框架,文件目录模仿yii2
查看>>
新开发的体重管理项目----用纯php模仿yii2框架建立的
查看>>
JavaScript面向对象编程指南 的笔记
查看>>
在 2016 年做 PHP 开发是一种什么样的体验?(一)
查看>>
PHP获取客户端的IP
查看>>
从头开始学习yii2---1.搭建yii2开发环境
查看>>