pynput使用简单说明

news/2024/7/3 0:24:02

控制鼠标

 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()

 


http://lihuaxi.xjx100.cn/news/245810.html

相关文章

git revert和git reset的区别

git revert 是撤销某次操作&#xff0c;此次操作之前的commit都会被保留git reset 是撤销某次提交&#xff0c;但是此次之后的修改都会被退回到暂存区具体一个例子&#xff0c;假设有三个commit&#xff0c; git st:commit3: add test3.ccommit2: add test2.ccommit1: add test…

ActiveMQ 消息服务(一)

2019独角兽企业重金招聘Python工程师标准>>> 1、百度百科对ActiveMQ的解释&#xff1a; ActiveMQ 是Apache出品&#xff0c;最流行的&#xff0c;能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现&#xff0c;尽管JMS规范出…

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

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

Mysql隐藏命令_mysql常用命令整理

说明&#xff1a;下面用到的语法是参考官方mysql5.7 en manual文档&#xff0c;本机的运行环境为centos7和mysql5.7.26修改数据库语法&#xff1a;ALTER {DATABASE | SCHEMA} [db_name]alter_specification ...ALTER {DATABASE | SCHEMA} db_nameUPGRADE DATA DIRECTORY NAMEal…

linux:关于Linux系统中 CPU Memory IO Network的性能监测

我们知道&#xff1a;系统优化是一项复杂、繁琐、长期的工作.通常监测的子系统有以下这些&#xff1a;CPUMemoryIONetwork下面是常用的监测工具Linux 系统包括很多子系统&#xff08;包括刚刚介绍的CPU&#xff0c;Memory&#xff0c;IO&#xff0c;Network&#xff0c;等&…

javaweb项目开发日志的原理,方式

2019独角兽企业重金招聘Python工程师标准>>> 日志框架可以做什么?日志框架可以帮助各类项目人员,,记录各种项目运行时信息;以便其,通过日志信息,了解和监控项目的状态,解决各种运行时项目出现的问题;日志框架如何使用呢? 那log4j和slf4j为例: ----pom.xml …

【逆序对】Ultra - Quicksort

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

学生的新增mySQL文档_MySQL增删改查

连接命令&#xff1a;mysql -h[主机地址] -u[用户名] -p[用户密码]创建数据库&#xff1a;create database [库名]显示所有数据库: show databases;打开数据库:use [库名]当前选择的库状态:SELECT DATABASE();创建数据表:CREATE TABLE [表名]([字段名] [字段类型]([字段要求]) …