在Python中一起使用鼠标和键盘监听器

问题描述 投票:1回答:2

我一直在使用pynput库来监控鼠标的点击。我面临的唯一问题是终端没有按Ctrl + C终止。我需要使用带有鼠标监听器的键盘监听器。这是我的代码:

import os
import time
import re
from pynput import mouse
from pynput.keyboard import Key, Listener
f=open('maniac1.txt','a')

inc=1
f.write('<mouse_new>\n')

def on_click(x, y, button, pressed):
    f=open('maniac1.txt','a')
    if button == mouse.Button.left:
        print 'Left'
        f.write('left\n')

    if button == mouse.Button.right:
        print 'right'
        f.write('right\n')
    if button == mouse.Button.middle:
        print 'middle'
        f.write('middle\n')

with mouse.Listener(on_click=on_click,on_scroll=on_scroll) as listener:
    try:
        listener.join()
    except MyException as e:
        print('Done'.format(e.args[0]))

按Esc或Ctrl + C后如何终止此代码?我正在使用OSX。

python macos keylistener
2个回答
4
投票

创建一个没有“with”关键字的实例keyboard.Listener,以便您可以根据鼠标侦听器启动和停止侦听器。检查下面的代码,用鼠标右键单击后将停止听f8的按键。

import os
import time
import re
from pynput import mouse
from pynput.keyboard import Key, Listener
#f=open('maniac1.txt','a')

inc=1
#f.write('<mouse_new>\n')
from pynput import keyboard

def on_functionf8(key):
    if (key==keyboard.Key.f8):
        print('f8 is pressed')


key_listener = keyboard.Listener(on_release=on_functionf8)
key_listener.start()


def on_click(x, y, button, pressed):
    f=open('maniac1.txt','a')
    if button == mouse.Button.left:
        print ('Left')
        #f.write('left\n')

    if button == mouse.Button.right:
        key_listener.stop()
        print ('right')
        #f.write('right\n')
    if button == mouse.Button.middle:
        print ('middle')
        #f.write('middle\n')

with mouse.Listener(on_click=on_click) as listener:
    try:
        listener.join()
    except MyException as e:
        print('Done'.format(e.args[0]))

运行程序并按f8键,您将在终端上看到“f8被按下”。但是右键单击并按f8。当我们在鼠标右键单击时停止键盘监听器时,您不会看到任何打印内容。

对于mac:

def on_press(key):
    try:
        print('alphanumeric key {0} pressed'.format(
            key.char))
    except AttributeError:
        print('special key {0} pressed'.format(
            key))



key_listener = keyboard.Listener(on_release=on_press)

默认情况下,只有几个键如cmd,alt在mac上被监听。


-1
投票

此代码一起使用鼠标和键盘监听器。

from pynput.keyboard import Listener  as KeyboardListener
from pynput.mouse    import Listener  as MouseListener
from pynput.keyboard import Key
import logging

logging.basicConfig(filename=("log.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s')

def end_rec(key):
    logging.info(str(key))

def on_press(key):
    logging.info(str(key))

def on_move(x, y):
    logging.info("Mouse moved to ({0}, {1})".format(x, y))

def on_click(x, y, button, pressed):
    if pressed:
        logging.info('Mouse clicked at ({0}, {1}) with {2}'.format(x, y, button))

def on_scroll(x, y, dx, dy):
    logging.info('Mouse scrolled at ({0}, {1})({2}, {3})'.format(x, y, dx, dy))


with MouseListener(on_click=on_click, on_scroll=on_scroll) as listener:
    with KeyboardListener(on_press=on_press) as listener:
        listener.join()
© www.soinside.com 2019 - 2024. All rights reserved.