我一直在使用pynput庫來監視鼠標的點擊。我面對的唯一問題是終端不按Ctrl + C終止。我需要用鼠標監聽器來使用鍵盤監聽器。這是我的代碼:在Python中使用鼠標和鍵盤監聽器
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。
按「選項+ c」 –
我已經提到過我使用了選項+ C。它不工作。程序不終止。我想用鼠標監聽器添加鍵盤監聽器 –