https://blog.csdn.net/Jason_WangYing/article/details/114155511
基本控制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | # -*- encoding: utf-8 -*- import time import pyautogui as pag import keyboard # https://www.jianshu.com/p/8e508c6a05ce # https://blog.csdn.net/weixin_43430036/article/details/84650938 #保护措施,避免失控 pag.FAILSAFE = True #为所有的PyAutoGUI函数增加延迟。默认延迟时间是0.1秒。 #pag.PAUSE = 0.5 screenWidth, screenHeight = pag.size() # 获取屏幕的尺寸 def install_package(): "安装keyboard包" import os mirror = " -i https://pypi.douban.com/simple" os.system( "python -m pip install --upgrade pip" + mirror) # 更新 pip os.system( "pip install keyboard" + mirror) # 安装 keyboard os.system( "pip install pyautogui" + mirror) # 安装 pyautogui print ( "包安装成功! " ) #install_package() old_x = 0 old_y = 0 def test_a(): print ( 'aaa' ) def test(x): print (x) keyboard.add_hotkey( 'f1' , test_a) keyboard.add_hotkey( 'ctrl+alt' , test, args = ( 'ctrl+alt按下' ,)) #记录键盘事件,如果加上until参数,可以设置当按下某按键时结束监听,和wait方法有点像, #recorded = keyboard.record(until='esc') #当按下esc时结束按键监听,并输出所有按键事件 #绑定所有按键事件,当只要有按键按下/松开时就会触发的回调函数 def abc(x): a = keyboard.KeyboardEvent( 'down' , 28 , 'enter' ) #按键事件a为按下enter键,第二个参数如果不知道每个按键的值就随便写, #如果想知道按键的值可以用hook绑定所有事件后,输出x.scan_code即可 if x.event_type = = 'down' and x.name = = a.name: print ( "你按下了enter键" ) #当监听的事件为enter键,且是按下的时候 keyboard.hook(abc) #按下任何按键时,都会调用abc,其中一定会传一个值,就是键盘事件 #弹出对话框 提示信息 pag.alert(text = 'This is an alert box.' , title = 'Test' ) #选择框 pag.confirm( 'Enter option.' , buttons = [ 'A' , 'B' , 'C' ]) #password 密码框 隐藏输入内容 a = pag.password( 'Enter password (text will be hidden)' ) print (a) #消息框 a = pag.prompt( 'input message' ) print (a) try : while True : x, y = pag.position() # 返回鼠标的坐标 if x = = old_x and y = = old_y: pass else : old_x = x old_y = y print ( "Screen size: (%s %s), Position : (%s, %s)\n" % (screenWidth, screenHeight, x, y)) # 打印坐标 #pag.click(button='right') #点击鼠标右键 #pag.click(100, 100) #要在指定位置点击左键 #pag.moveRel(10, 10) # 相对于当前位置移动鼠标 # 用缓动/渐变函数让鼠标2秒后移动到(500,500)位置 # use tweening/easing function to move mouse over 2 seconds. #pag.moveTo(x=500, y=500, duration=2, tween=pag.easeInOutQuad) #鼠标绝对拖拽 按下左键移动鼠标 #pag.dragTo(x=427, y=535, duration=3,button='left') #鼠标相对拖拽 #pag.dragRel(xOffset=100,yOffset=100,duration=0,button='left',mouseDownUp=False) #pag.dragRel(100, 100) # 按下左键 , 相对于当前位置移动鼠标 #time.sleep(0.0001) # 每个1s中打印一次 , 并执行清屏 #鼠标移动到x=1796, y=778位置按下 #pag.mouseDown(x=1796, y=778, button='left') #鼠标移动到x=2745, y=778位置松开(与mouseDown组合使用选中) #pag.mouseUp(x=2745, y=778, button='left',duration=5) #鼠标当前位置滚轮滚动 #pag.scroll() #鼠标水平滚动(Linux) #pag.hscroll() #鼠标左右滚动(Linux) #pag.vscroll() #keyboard.wait('a') #在按下a之前后面的语句都不会执行,下面同理 except KeyboardInterrupt: print ( 'end' ) ''' moveTo(x, y) # 将鼠标移动到指定的 x y 坐标 . moveRel(xOffset, yOffset) # 相对于当前位置移动鼠标 . dragTo(x, y) # 按下左键移动鼠标 . dragRel(xOffset, yOffset) # 按下左键 , 相对于当前位置移动鼠标 . click(x, y, button) # 模拟点击 (默认是左键) . rightClick() # 模拟右键点击。 middleClick() # 模拟中键点击。 doubleClick() # 模拟左键双击。 mouseDown(x, y, button) # 模拟在 x、y 处按下指定鼠标按键。 mouseUp(x, y, button) # 模拟在 x、y 处释放指定键。 scroll(units) # 模拟滚动滚轮。正参数表示向上滚动, 负参数表示向下滚动。 typewrite(message) # 键入给定消息字符串中的字符。 typewrite([key1, key2, key3]) # 键入给定键字符串。 press(key) # 按下并释放给定键。 keyDown(key) # 模拟按下给定键。 keyUp(key) # 模拟释放给定键。 hotkey([key1, key2, key3]) # 模拟按顺序按下给定键字符串, 然后以相反的顺序释放。 screenshot() # 返回屏幕快照的 Image 对象 ''' |
监听鼠标
按键监听有问题
https://blog.csdn.net/Jason_WangYing/article/details/114155511
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import pynput, time def on_move(x, y): print ( 'Pointer moved to {0}' . format ((x, y))) def on_click(x, y, button, pressed): print (button, '{0} at {1}' . format ( 'Pressed' if pressed else 'Released' , (x, y))) if not pressed: # Stop listener #False return 1 def on_scroll(x, y, dx, dy): print ( 'Scrolled {0} at {1}' . format ( 'down' if dy < 0 else 'up' , (x, y))) # Collect events until released with pynput.mouse.Listener( on_move = on_move, on_click = on_click, on_scroll = on_scroll) as listener: listener.join() |
分类:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?