Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中同时使用鼠标和键盘侦听器_Python_Macos_Keylistener - Fatal编程技术网

在Python中同时使用鼠标和键盘侦听器

在Python中同时使用鼠标和键盘侦听器,python,macos,keylistener,Python,Macos,Keylistener,我一直在使用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(

我一直在使用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]))
导入操作系统
导入时间
进口稀土
从pynput导入鼠标
从pynput.keyboard导入键,侦听器
f=打开('maniac1.txt','a')
inc=1
f、 写入('\n')
def on_单击(x,y,按钮,按下):
f=打开('maniac1.txt','a')
如果button==mouse.button.left:
打印“左”
f、 写入('左\n')
如果button==mouse.button.right:
打印“右”
f、 写入('right\n')
如果button==mouse.button.middle:
打印“中间”
f、 写入('middle\n')
使用鼠标.Listener(on_click=on_click,on_scroll=on_scroll)作为侦听器:
尝试:
listener.join()
除了我的例外情况e:
打印('Done'。格式(e.args[0]))

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

创建一个实例键盘。侦听器不带“with”关键字,以便您可以根据鼠标侦听器启动和停止侦听器。检查以下代码,在鼠标右键单击后,该代码将停止收听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]))

默认情况下,mac上只会侦听几个键,如cmd、alt。

此代码同时使用鼠标和键盘侦听器

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()

我几个小时前刚刚完成了同样的事情,这是我写的

首先添加另一个键盘侦听器:

# Collect events until released
with keyboard.Listener(on_release=on_release) as k_listener, \
        mouse.Listener(on_click=on_click) as m_listener:
    k_listener.join()
    m_listener.join()
然后在发布时添加
功能:

def on_release(key):
    if key == keyboard.Key.esc:
        # Stop listeners
        m_listener.stop()
        return False
然后,如果按Esc键,此代码将被终止


对于OSX,您需要使用
sudo
运行python,否则它将无法正常工作。

按“option+c”我已经提到我使用了option+c。它无法工作。程序不会终止。我想添加带有鼠标监听器的键盘监听器Nope..它不显示按了F8。我也尝试在右键单击后按F8。它只显示鼠标单击。即左/右/中。默认情况下,mac不受键盘记录程序的影响,除了cmd、alt等少数键外,不允许您按键盘。。这是完全不同的问题。如果要检查功能,请打印每个键,然后尝试按cmd或alt。将其添加到答案中。请检查。是的,你是对的。它只听特殊的键,如“Shift”、“cntrl”、“command”。但我希望操作系统也听其他字母数字键。我怎样才能做到这一点呢?考虑到苹果的限制,在Mac上做到这一点是一项艰巨的任务。这方面有许多悬而未决的问题。如果你认为我已经回答了你关于键盘和鼠标控制相结合的实际问题,请接受我的回答,解释你的答案是什么,以便其他用户能够理解主要思想是缩进的。我在代码中复制了这个概念,它工作得非常好。用户@0rk准确地回答了问题:“如何在Python中同时使用鼠标和键盘侦听器”。非常好!这是唯一可以正常工作的代码!怪不得在我的投票之前你没有得到任何支持!(这里不支持真正好的答案!)
def on_release(key):
    if key == keyboard.Key.esc:
        # Stop listeners
        m_listener.stop()
        return False