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]))
How can i terminate this code after pressing Esc or Ctrl+C?I am using OSX.