title: Using Mouse and Keyboard Listeners Together in Python
body: <p>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:</p>
  
  <pre><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('&lt;mouse_new&gt;\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]))
  </code></pre>
  
  <p>How can i terminate this code after pressing Esc or Ctrl+C?I am using OSX.</p>