首页 > 编程语言 >python监听鼠标键盘

python监听鼠标键盘

时间:2022-11-07 16:55:25浏览次数:36  
标签:鼠标 python button pag 按下 keyboard print 监听

https://blog.csdn.net/Jason_WangYing/article/details/114155511

 

# -*- 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 对象
'''

  

标签:鼠标,python,button,pag,按下,keyboard,print,监听
From: https://www.cnblogs.com/gooutlook/p/16866522.html

相关文章