weixin_45536989
2019-08-19 06:18
采纳率: 40%
浏览 1.6k

python里怎么样监控鼠标按下的是左键还是右键

我写了一个监控鼠标事件的代码,我想监控鼠标的左右按键,当我按下左键或者右键时输出并且记录下来,
但是我的这段代码好像只能对右键反应,而且点击左键的输出一样,这是哪里出问题了,希望大佬帮我看看,谢谢
对了,我的需求很简单,就是实时监控鼠标左右键,按下键时记录下来保存为npy数据。不用监控鼠标坐标

from pynput import mouse
from pynput.mouse import Button, Controller
import numpy as np
right=[]
left=[]
no=[]
mouse_out=[]
def on_move(x, y):
   print('Pointer moved to {0}'.format((x, y)))

def on_click(x, y, button, pressed):
   right_data=[]
   left_data=[]
   no=[]

   if Button.right:
      right_data = [0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
      #print('{0} at {1}'.format(
         #'Pressed' if pressed else 'Released',(x, y)))
      #print("右键",right_data)
   elif button == Button.left:
      left_data = [0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
      print('{0} at {1}'.format('Pressed' if pressed else 'Released',(x, y)))
      print("左键",left_data)
   if not pressed:
      no=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
   return right_data,left_data,no
   # Stop listener

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 mouse.Listener(on_click=on_click) as listener:
   listener.join()
#print(right_data)
#np.save(r'C:\Users\Administrator\Desktop\ckpt_dir\mouse_data',right_data)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • weixin_45536989 2019-08-19 12:09
    已采纳

    我研究了几个小时,拿别人的一个源码改了一下,基本实现实时监控鼠标左右键的点击了,但是还是有问题,第一就是太占内存了,把鼠标坐标检测打开,CPU占用70,内存60.希望有大佬能优化一下,哈哈哈
    第二就是监控不知道是有错误,还是我的代码有错误,左键数值会变化,正常的是1,但是有时候会变成0,加了线程锁都没用。而且右键不是每次点击都会检测到,原因也不知道,发现自己啥也不知道。。

    import threading
    from pynput.mouse import Button, Listener, Controller
    from pynput import keyboard
    import time
    import numpy as np
    
    # 记录操作指令
    # 左键
    shoot = 0
    # 右键
    scope = 1
    # 
    status = 1
    mouse_out=[]
    
    # 鼠标控制器
    controller = Controller()
    lock=threading.Lock()
    
    """
    鼠标事件
    """
    def mouse_click(x, y, button, pressed):
        global shoot
        global scope
        if pressed:
            # 点击右键
            if button == Button.right:
                scope = 0
            # 点击左键
            if button == Button.left:
                shoot = 1
        # 
        if not pressed and button == Button.left:
            shoot = 0
    
    # 监控鼠标位置
    def mouse_move(x, y):
        print(x, y)
    
    """
    键盘事件
    """
    # def keyboard_release(key):
    #     global status
    #     if key == keyboard.Key.backspace:
    #         # 
    #         status = -status
    """
    监听事件方法
    """
    def mouseListener():
        with Listener(on_click=mouse_click) as listener:
            listener.join()
    
    # def keyboardListener():
    #     with keyboard.Listener(
    #             on_release=keyboard_release) as listener:
    #         listener.join()
    
    def main():
        threading._start_new_thread(mouseListener, ()) 
        #threading._start_new_thread(keyboardListener, 
        while 1:
        # 循环监听各状态并控制鼠标
            global shoot
            global scope
            global status
            #shoot=0
            scope = -1
            #status =1
            if shoot == 1:
                lock.acquire()
                time.sleep(0.2)
                controller.move(0, +15)
                mouse_out.append([shoot])
                np.save(r'C:\Users\Administrator\Desktop\ckpt_dir\mouse_out',mouse_out)
                print('左键',shoot)
                lock.release()
            elif scope == 0:
                time.sleep(0.2)
                controller.move(0, +25)
                mouse_out.append([scope])
                np.save(r'C:\Users\Administrator\Desktop\ckpt_dir\mouse_out',mouse_out)
                print('右键',scope)
    
    if __name__ == '__main__':
        main()
    
    点赞 打赏 评论
  • 苔原0902 2020-11-22 06:14

    你用了numpy能不卡吗?

    点赞 打赏 评论

相关推荐 更多相似问题