Python控制键盘鼠标pynput的详细用法
作者:botoo 发布时间:2021-08-01 07:52:14
标签:Python,pynput,键盘,鼠标
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
来源:https://www.cnblogs.com/botoo/p/8302531.html
0
投稿
猜你喜欢
- 之前使用的python的smtplib、email模块发模块的一步步骤是:一、先导入smtplib模块 导入MIMEText库用
- 前言网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或
- Request.Cookies.Clear()这个方法并不是删除Cookie 删除 Cookie(即从用户的硬盘中物理移除 Cookie)是
- scikit-learn是python的第三方机器学习库,里面集成了大量机器学习的常用方法。例如:贝叶斯,svm,knn等。scikit-l
- Pycharm是当前进行python开发,尤其是Django开发最好的IDE。GitHub是程序员的圣地,几乎人人都在用。本文假设你对pyc
- 一、'建立register.asp 代码如下:<%@ language=vbscript %>&nb
- 一个网站程序,在添加新闻时出现错误以下是错误提示: Microsoft OLE DB Provide
- 引言:Python中的变量在使用中很流畅,可以不关注类型,任意赋值,对于开发来说效率得到了提升,但若不了解其中的机理,往往也会犯一些小错,让
- 前言大家都知道,英文的分词由于单词间是以空格进行分隔的,所以分词要相对的容易些,而中文就不同了,中文中一个句子的分隔就是以字为单位的了,而所
- 作用:用ASP程序将页面中的电话号码生成图片格式。 代码如下:<% Call Com_CreatValidCode
- 有时一些网页对源码进行了加密,我们很难找到类似像“onselectstart="return false"”这样的代码,
- 这篇文章主要介绍了python tkinter canvas使用实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习
- 导言到目前为止的讨论编辑DataList的教程里,没有包含任何验证用户的输入,即使是用户非法输入— 遗漏了product的name或者负的p
- 以前游戏2048火的时候,正好用其他的语言编写了一个,现在学习python,正好想起来,便决定用python写一个2048,由于没学过pyt
- 发送普通邮件发送文本和html普通邮件如下:from email.header import Headerfrom email.mime.t
- 终端输出彩色文字开发工具:Mac,Goland,Mac自带shell。这是基于Mac的测试结果,根据读者留言,在Windows上不生效,标识
- 经过一个阶段的asp学习,下面我们结合所学过的内容建立一个最简单的聊天室,虽然很简单,但是大家可以通过他来掌握一个聊天室建立的基本过程,并且
- 简单试用了一下IE8后,今天相对有时间点,对IE8、IE7、IE6、Firefox2.0.0.12做了简单的一些CSS HACK测
- 知识掌握 cv2.threshold()函数:设置固定级别的阈值应用于多通道矩阵,将灰度图像变换二值图像,或去除指定级别的噪声,或过滤掉过小
- 我就废话不多说了,大家还是直接看代码吧~#!/usr/bin/env python# encoding: utf-8''