当前位置: 首页 > 编程日记 > 正文

pynput使用简单说明

控制鼠标

 1 from  pynput.mouse import Button, Controller
 2 import time 
 3 
 4 mouse = Controller()
 5 print(mouse.position)
 6 time.sleep(3)
 7 print('The current pointer position is {0}'.format(mouse.position))
 8 
 9 
10 #set pointer positon
11 mouse.position = (277, 645)
12 print('now we have moved it to {0}'.format(mouse.position))
13 
14 #鼠标移动(x,y)个距离
15 #param int x: The horizontal offset.
16 #param int dy: The vertical offset.
17 mouse.move(5, -5)
18 print(mouse.position)
19 
20 mouse.press(Button.left)
21 mouse.release(Button.left)
22 
23 mouse.press(Button.right)
24 mouse.release(Button.right)
25 
26 #Double click
27 #param int count: The number of clicks to send.
28 mouse.click(Button.left, 2)
29 
30 #scroll two     steps down
31 #param int dx: The horizontal scroll. 
32 #param int dy: The vertical scroll.
33 mouse.scroll(0, 500)

监听鼠标

 1 '''
 2 :param callable on_move: The callback to call when mouse move events occur.
 3 
 4         It will be called with the arguments ``(x, y)``, which is the new
 5         pointer position. If this callback raises :class:`StopException` or
 6         returns ``False``, the listener is stopped.
 7 
 8     :param callable on_click: The callback to call when a mouse button is
 9         clicked.
10 
11         It will be called with the arguments ``(x, y, button, pressed)``,
12         where ``(x, y)`` is the new pointer position, ``button`` is one of the
13         :class:`Button` values and ``pressed`` is whether the button was
14         pressed.
15 
16         If this callback raises :class:`StopException` or returns ``False``,
17         the listener is stopped.
18 
19     :param callable on_scroll: The callback to call when mouse scroll
20         events occur.
21 
22         It will be called with the arguments ``(x, y, dx, dy)``, where
23         ``(x, y)`` is the new pointer position, and ``(dx, dy)`` is the scroll
24         vector.
25 
26         If this callback raises :class:`StopException` or returns ``False``,
27         the listener is stopped.
28 
29     :param bool suppress: Whether to suppress events. Setting this to ``True``
30         will prevent the input events from being passed to the rest of the
31         system.
32 '''
33 
34 from pynput import mouse
35 from  pynput.mouse import Button
36 
37 def on_move(x, y):
38     print('Pointer moved to {o}'.format((x,y)))
39 
40 def on_click(x, y , button, pressed):
41     button_name = ''
42     #print(button)
43     if button == Button.left:
44         button_name = 'Left Button'
45     elif button == Button.middle:
46         button_name = 'Middle Button'
47     elif button == Button.right:
48         button_name = 'Right Button'
49     else:
50         button_name = 'Unknown'
51     if pressed:
52         print('{0} Pressed at {1} at {2}'.format(button_name, x, y))
53     else:
54         print('{0} Released at {1} at {2}'.format(button_name, x, y))
55     if not pressed:
56         return False
57 
58 def on_scroll(x, y ,dx, dy):
59     print('scrolled {0} at {1}'.format(
60         'down' if dy < 0 else 'up',
61         (x, y)))
62 
63 while True:
64     with mouse.Listener( no_move = on_move,on_click = on_click,on_scroll = on_scroll,suppress = False) as listener:
65         listener.join()

控制键盘

 1 '''
 2 ['alt', 'alt_l', 'alt_r', 'backspace', 'caps_lock', 'cmd', 'cmd_r', 'ctrl', 'ctrl_l', 'ctrl_r', 'delete', 
 3 'down', 'end', 'enter', 'esc', 'f1', 'f10', 'f11', 'f12', 'f13', 'f14', 'f15', 'f16', 'f17', 'f18', 'f19', 'f2', 'f20', 
 4 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'home', 'insert', 'left', 'menu', 'num_lock', 'page_down', 'page_up', 'pause',
 5 'print_screen', 'right', 'scroll_lock', 'shift', 'shift_r', 'space', 'tab', 'up']
 6 '''
 7 
 8 from pynput.keyboard import Key, Controller
 9 
10 keyboard = Controller()
11 
12 #Press and release space
13 keyboard.press(Key.space)
14 keyboard.release(Key.space)
15 
16 #Type a lower case A ;this will work even if no key on the physical keyboard  is labelled 'A'
17 keyboard.press('a')
18 keyboard.release('a')
19 
20 #Type two  upper case As
21 keyboard.press('A')
22 keyboard.release('A')
23 # or 
24 #Executes a block with some keys pressed.    param keys: The keys to keep pressed.
25 with keyboard.pressed(Key.shift):    #组合按键
26     keyboard.press('a')
27     keyboard.release('a')
28 
29 #type 'hello world '  using the shortcut type  method
30 #This method will send all key presses and releases necessary to type all characters in the string.
31 #param str string: The string to type.
32 keyboard.type('hello world')
33 
34 keyboard.touch('&', True)
35 keyboard.touch('&', False)
36     
37 keyboard.press(Key.print_screen)
38 keyboard.release(Key.print_screen)
39 
40 with keyboard.pressed(Key.ctrl):    #组合按键
41     keyboard.press('s')
42     keyboard.release('s')

监听键盘

 1 from pynput import keyboard
 2 
 3 #alt_pressed、alt_gr_pressed、ctrl_pressed、shift_pressed
 4 
 5 
 6 def on_press(key):
 7     try:
 8         print('alphanumeric key     {0} pressed'.format(key.char))    #应该记录下之前有没有ctrl、alt、和shift按下
 9     except AttributeError:
10         print('special key {0} pressed'.format(key))
11 
12 def on_release(key):
13     print('{0} released'.format(key))
14     if key == keyboard.Key.esc:
15         return False
16 
17 while True:
18     with keyboard.Listener(
19         on_press = on_press,
20         on_release = on_release,
21         suppress = False) as listener:
22         listener.join()

相关文章:

linux qt5.7下打地鼠源程序,基于QT的打地鼠游戏

【实例简介】基于QT的一个打地鼠游戏&#xff0c;采用随机数的方法&#xff0c;是地鼠产生随机序列&#xff0c;有得分界面&#xff0c;动画效果也不错&#xff0c;用C进行编程【实例截图】【核心代码】打地鼠└── 打地鼠├── erwei│ ├── Makefile│ ├── Makefi…

事务隔离机制原理深入分析以及MySQL不同隔离级别分场景下实验对比

这是我总结的事务的四种隔离机制&#xff0c;比较好理解&#xff0c;主要是有些地方文字游戏说不清楚很容易混淆&#xff1a; Read Uncommitted&#xff08;读未提交&#xff09;A未完&#xff0c;B已更新&#xff0c;未提交&#xff0c;A读到B已更新的数据&#xff0c;由于未…

cogs 362. [CEOI2004]锯木厂选址

★★★ 输入文件&#xff1a;two.in 输出文件&#xff1a;two.out 简单对比 时间限制&#xff1a;0.1 s 内存限制&#xff1a;32 MB 从山顶上到山底下沿着一条直线种植了n棵老树。当地的政府决定把他们砍下来。为了不浪费任何一棵木…

中小企业低成本快速建站的秘诀——模板建站

从14年至今&#xff0c;小乔已经给很多行业的客户做了不少网站。在跟我咨询建站的这些人当中&#xff0c;其实不乏一些创业初期经济比较紧张的个人/公司。这些个人/公司需要一个网站对外宣传&#xff0c;但又希望可以节省开支&#xff0c;所以他们往往会选择成本低的建站服务&a…

MySQL常用性能分析方法-profile,explain,索引

1.查版本号 无论做什么都要确认版本号&#xff0c;不同的版本号下会有各种差异。 >Select version();2.执行状态分析 显示哪些线程正在运行 >show processlist;下面是完整的信息3.show profile show profile默认的是关闭的&#xff0c;但是会话级别可以开启这个功能&…

MathType在手,公式不求人!

很多论文达人们的论文排版是相当漂亮的&#xff0c;页面也非常整齐美观&#xff0c;即使是理工类的论文&#xff0c;里面有很多的数学符号和公式&#xff0c;排版也是非常整洁&#xff0c;为什么达人们的公式论文能排版的这么完美&#xff0c;而自已却总是不得其门而入&#xf…

Linux系统mongdb还原数据库,linux下mongodb数据库备份与还原

MongoDb数据库备份还原数据库迁移,可视化工具NoSQLBooster for MongoDB 付费版才具有数据导入功能.代价过高,索性采起命令行web数据备份备份命令mongodbmongodump -h dbhost -d dbname -o dbdirectory-h&#xff1a;MongDB所在服务器地址&#xff0c;例如&#xff1a;127.0.0.1…

【逆序对】Ultra - Quicksort

POJ 2299 Ultra-QuickSort 只允许交换&#xff0c;比较相邻的元素&#xff0c; 求最少多少次交换可以使得序列有序 冒泡排序的次数——>数列中逆序对的个数减1——>最终为0 ——>答案为数列中逆序对的个数——> 归并排序求逆序对qwq 注意cnt开long long 不然会炸QA…

Android Touch事件传递机制 二:单纯的(伪生命周期) 这个清楚一点

转载于&#xff1a;http://blog.csdn.net/yuanzeyao/article/details/38025165 在前一篇文章中&#xff0c;我主要讲解了Android源码中的Touch事件的传递过程&#xff0c;现在我想使用一个demo以及一个实例来学习一下Andorid中的Touch事件处理过程。 在Android系统中&#xff0…

SpringBoot使用笔记

其实也是参考官方的&#xff1a;http://spring.io/guides/gs/rest-service/ &#xff0c;在官方代码基础上加入了很多实用的东西&#xff0c;比如运行环境启动命令等等。 官方文档&#xff1a;http://docs.spring.io/spring-boot/docs/current/reference/html/ SpringBoot并不…

linux卸载欧朋浏览器,如何在Centos下安装opera浏览器

如何在Centos下安装opera浏览器 &#xff0c;Opera目前是Linux平台上性能最优的浏览器&#xff0c;而且Opera中国团队本身即定位于Opera的研发中心&#xff0c;主要也是负责全球Linux平台项目的开发&#xff0c;这个版本初步解决了经年来Linux上Opera中文字体显示混乱的问题。我…

1-1 分配内存资源给容器和POD

这一小节讲解如何分配内存请求和对一个容器做内存限制。一个容器被保证拥有足够的内存可以处理请求&#xff0c;但是也不允许使用超过限制的内存。 开始之前 需要拥有一个k8s集群 需要安装好一个kubectl 工具&#xff0c;并且能够与集群通信。 如果没有准备好&#xff0c;你…

Java的SPI机制

Dubbo等框架使用到必须掌握。 java.sql.Driver 是 Spi&#xff0c;com.mysql.jdbc.Driver 是 Spi 实现&#xff0c;其它的都是 Api。package org.hadoop.java;public interface IService {public String sayHello(); public String getScheme(); }package org.hadoop.java…

你不知道的对称密钥与非对称密钥

&#xff08;一&#xff09;对称加密&#xff08;Symmetric Cryptography&#xff09; 对称密钥加密&#xff0c;又称私钥加密&#xff0c;即信息的发送方和接收方用一个密钥去加密和解密数据。它的最大优势是加/解密速度快&#xff0c;适合于对大数据量进行加密&#xff0c;对…

linux sntp 代码,C语言window(linux)平台的SNTP实现

C语言实现window(linux)平台的SNTP&#xff0c;本程序功能主要是实现电脑(或者设备)时间同步。摘录部分代码&#xff1a;unsigned char liVnMode; /* LeapSecond(2bits:0), VersionNumber(3bits: 3), Mode(3bits: Client3, Server4) */unsigned char stratum; /* 时间层级 (0-1…

在typescript中导入第三方类库import报错

问题 最近开始折腾typescript&#xff0c;在使用第三方类库&#xff0c;比如最常见的lodash&#xff0c;采用常规方法导入 import * as _ from lodashvscode中报错提示lodash不是module。 原因 因为第三方类库并没有ts的声明文件&#xff0c;查阅网上资料&#xff0c;有typings…

JavaAgent 实现字节码注入

新建MyAgent项目 pom文件 <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://maven.apach…

php打印中文乱码

php文档的文本格式都设置成 utf-8 格式 在代码中添加 header("content-type:text/html; charsetutf-8"); 转载于:https://www.cnblogs.com/negro-guoguo/p/5421355.html

linux孤立cpu,Linux 抛弃旧款 CPU,一下子少 50 万行代码

IT 之家4 月 3 日消息 Linux 内核维护者已经决定在即将发布的新版本中抛弃对旧款 CPU 架构的支持&#xff0c;因此 Linux 4.17 内核将减少大约 500000 行代码&#xff0c;根据 Linux 统计器&#xff0c;目前它包含大约 2030 万行代码。IT 之家报道&#xff0c;将被弃用的体系架…

CSS3 从头捋

1.border-radius 边界半径 作用&#xff1a;该属性用来实现圆角 示例1实现圆角 div {border:2px solid red;width:300px;border-radius:25px; } 示例2实现圆 div {border: 1px solid red;height: 100px;width: 100px;border-radius: 50%; } 示例3 不规则圆 div {border: 1px s…

算法:详解布隆过滤器的原理、使用场景和注意事项@知乎.Young Chen

算法&#xff1a;详解布隆过滤器的原理、使用场景和注意事项知乎.Young Chen 什么是布隆过滤器 本质上布隆过滤器是一种数据结构&#xff0c;比较巧妙的概率型数据结构&#xff08;probabilistic data structure&#xff09;&#xff0c;特点是高效地插入和查询&#xff0c;可…

linux shell显示下载进度,shell脚本测试下载速度

在linux下用shell来测试下载速度&#xff0c;很实用的shell代码。代码&#xff1a;复制代码 代码示例:#!/bin/bash#date:20140210# edit: www.jquerycn.cn#used for test server download speedr_host"188.18.28.19"r_dir"/home/test0208/tmp"r_file"…

openStack调试

openStack调试 posted on 2016-04-23 22:07 秦瑞It行程实录 阅读(...) 评论(...) 编辑 收藏 转载于:https://www.cnblogs.com/ruiy/p/5425823.html

快应用开发常见问题以及解决方案【持续更新】

接触快应用也有一段时间了&#xff0c;踩过了大大小小的坑&#xff0c;让我活到了今天。准备在此立贴持续更新&#xff0c;记录遇到的问题以及解决方案&#xff0c;造福大众。css 方面 1、文字竖排不支持 目前官方还不支持writing-mode&#xff0c;除了等待官方支持这个api以外…

Java字节码研究

关于怎么查看字节码的五种方法参考本人另一篇文章《Java以及IDEA下查看字节码的五种方法》 1.String和常连池 先上代码&#xff1a; public class TestApp {public static void main(String[] args) {String s1 "abc";String s2 new String("abc");St…

在c语言中逗号的作用,关于c语言中的逗号运算符???

等下。。答错了。。还需要理解一下神马是逗号表达式。。我前面说的和uuyyhhjj与delta_charlie的意思一样&#xff0c;但其实我们都搞错了。你可以自己把我们的例子都运行一下&#xff0c;看看是不是这样。下面我感觉应该是我正确的理解。逗号表达式是所有运算符中优先级最低的&…

2018-2019-1 20165206 《信息安全系统设计基础》第4周学习总结

- 2018-2019-1 20165206 《信息安全系统设计基础》第4周学习总结 - 教材学习内容总结 程序员可见的状态&#xff1a;Y86-64程序中的每条指令都会读取或修改处理器状态的某些部分&#xff0c;这称为程序员可见状态。包括&#xff1a;程序寄存器、条件码、程序状态、程序计数器和…

PHP——图片上传

图片上传 Index.php文件代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Document</title> </head> <body><form action"upload2.php" method"p…

IDEA实用插件和技巧

《解决lambda expressions are not supported at this language level的问题》 《Intellij Idea 代码格式化/保存时自动格式化》 一、安装google-java-format preferences -> plugins -> Browse repositories… 搜索google-java-format 还有阿里的代码规范插件也不…