在Python中一起使用鼠标和键盘侦听器 [英] Using Mouse and Keyboard Listeners Together in Python

查看:173
本文介绍了在Python中一起使用鼠标和键盘侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Pynput库来监视鼠标的点击.我面临的唯一问题是终端在按Ctrl + C时不会终止.我需要使用键盘侦听器和鼠标侦听器.这是我的代码:

I have been using pynput library for monitoring the clicks of the mouse.The only problem I am facing is that the terminal does not terminate on pressing Ctrl+C. I need to use keyboard listener with mouse listener. Here's my code:

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.

How can i terminate this code after pressing Esc or Ctrl+C?I am using OSX.

推荐答案

创建不带"with"关键字的实例keyboard.Listener,以便您可以基于鼠标侦听器启动和停止侦听器.检查下面的代码,用鼠标右键单击后将停止监听f8的按键.

Create an instance keyboard.Listener without "with" keyword so that you can start and stop the listener based on your mouse listener. Check the below code which will stop listening to key-press of f8 after right click by mouse.

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.当我们用鼠标右键单击停止键盘侦听器时,您将看不到任何打印内容.

run the program and press f8 and you will see 'f8 is pressed' on the terminal. But right click and press f8. You wont see anything printed as we stopped the keyboard listener on right click of mouse.

对于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)

默认情况下,在Mac上仅会监听cmd,alt之类的键.

only few keys like cmd, alt are listened on mac by default.

这篇关于在Python中一起使用鼠标和键盘侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆