控制鼠标

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

pynput使用简单说明相关推荐

  1. python监听键盘库_python监听、操作键盘鼠标库pynput详细教程|python基础教程|python入门|python教程...

    https://www.xin3721.com/eschool/pythonxin3721/ § 0.0.0 前言 监听.操作鼠标.键盘是实现自动化的捷径,比如我实现自动化签到用到了模拟键盘操作. p ...

  2. Pynput模块的学习与创新,基于python的操作录制脚本

    pynput模块是个好东西,它可以代理你完成一些重复且简单的操作.由于涉及到控制鼠键,我去年发布的博文始终没过审,本文会提供一些易使用的函数和我的学习过程. 时隔一年,我重新回到了pynput模块的学 ...

  3. 利用Python的pynput库解放双手之控制键盘

    在工作中,难免有鼠标.键盘点到手抽筋,腱鞘炎警告的时刻.某天,当有无数个数据等着我点击向下箭头一一确认时(如冰山一角的下图),我想到了可爱的python也许能救我,寻寻觅觅发现了pynput库. 利用 ...

  4. Python 在windows上跑图色脚本?简单又好玩,自己编写一个自动化脚本

    Python 在windows上跑图色脚本?简单又好玩,自己编写一个自动化脚本 大家好 我又来开新坑了,如图这次准备用python弄个简单脚本(根据图色判断进行键鼠操作) 1.老规矩 先安排运行环境 ...

  5. PYTHON鼠标记录器 一个简单的鼠标记录器 可以修改坐标和点击的时间

    PYTHON鼠标记录器 一个简单的鼠标记录器 可以修改坐标和点击的时间. 差一个定时器,如果加上定时器,估计就是解放了守在电脑边的你. 定时器版本忘了放在哪里了.先分享这个给对编程爱好的朋友,虽然我在 ...

  6. python实现一个简单的项目建议书范文_建议收藏,22个Python迷你项目(附源码)

    Python部落在使用Python的过程中,我最喜欢的就是Python的各种第三方库,能够完成很多操作. 下面就给大家介绍22个通过Python构建的项目,以此来学习Python编程. 大家也可根据项 ...

  7. python-pynput库用法 及 简单实现记录键盘

    pynput软件包说明文档 控制及监听鼠标 #测试pynput第三方库 #控制鼠标 import timefrom pynput.mouse import Button,Controller #导入控 ...

  8. python pynput库 自动按键 鼠标键盘 监听控制插件 可以制作按键精灵

    这是一个跨平台,使用简单的鼠标键盘监听控制库. 安装 环境 mac os + python 3.6 pip install pynput 监听,控制 import time from pynput i ...

  9. python键盘监听模块大全_python监听、操作键盘鼠标库pynput详细教程

    § 0.0.0 前言 监听.操作鼠标.键盘是实现自动化的捷径,比如我实现自动化签到用到了模拟键盘操作. pynput是监听.操控鼠标和键盘的跨平台第三方python库. 你可以通过pip insnal ...

最新文章

  1. 7 个日常实用的 Shell 拿来就用脚本实例!
  2. xv6/调度算法及并发程序设计
  3. zoj 1006 Do the Untwist 簡單字符串
  4. pyqt怎么button怎么链接_SEO内部链接怎么优化
  5. 使用Fsharp 探索 Dotnet 平台
  6. UVa1586 - Molar mass
  7. 学习EXT第八天:EXT的布局(Layout)Part 1
  8. python3生成exe文件_python3.7打包成exe就三步
  9. tf2多种方式对图像数据集进行预处理
  10. sql概念模型和逻辑模型
  11. 2021年韩国经济发展研究报告
  12. 修炼一名程序员的职业水准(林庆忠__署名原创)
  13. 游戏突发随机事件设计
  14. 习题6-5 UVA1600 巡逻机器人 Patrol Robot
  15. python实现自动按键_Python使用pyautogui模块实现自动化鼠标和键盘操作示例
  16. 史上最全后端架构师技术图谱,值得收藏
  17. 数据压缩读书笔记——线性代数的几何意义(五)
  18. 问题锦集 | 百度网盘播放视频显示:网络连接失败,请检查您的网络设置并稍后再试
  19. 文献综述十九:基于会员通用积分的客户关系管理系统设计与实现
  20. ACM/ICPC学习新人向导新手必读[转--算法学习推荐]

热门文章

  1. QC配置邮件服务器(Mail Dir)
  2. 在Mac和Linux之间用Rsync 拷贝文件
  3. “智慧血联网平台”亮相军民融合技术装备博览会
  4. Linux系统中的Page cache和Buffer cache
  5. http://www.cnblogs.com/youfan/articles/3216816.html
  6. 一个button导致的慘案
  7. 面向对象解决了全局变量问题?
  8. 解析Erlang日志组件lager的lager_transform模块
  9. MIT开放式课程“自然语言处理”介绍
  10. Calendar类点点滴滴积累