Python crash with pyHook.HookManager () in KeyDown [ctrl] + [c]

I create a python script to write the keys that I click on my system (keylogger) and call home with information about what are my typical habits. I am new to python and I use this app to find out about this. I am using python-3.x and windows 8

The full source code can be found here https://github.com/funvill/QSMonitor/blob/master/monitor.py

Using this snippet, I can record all clicks on my entire system. The problem is that when I [ctrl] + [c] copy something to another window, python code crashes.

Playback Steps

  • Run monitor.py script
  • In another window (notepad.exe), enter a few letters (abcd).
  • Highlight the letters in the notebook and hold [ctrl] and press [c]

Experienced:

A Windows error message will appear and tell me that python.exe is stopping and needs to be restarted. There was no python error message in the command window.

def OnKeyboardEvent(event): global keyDatabase global keyLoggerCount keyLoggerCount += 1 # http://code.activestate.com/recipes/553270-using-pyhook-to-block-windows-keys/ print ('MessageName:',event.MessageName ) print ('Message:',event.Message) print ('Time:',event.Time) print ('Window:',event.Window) print ('WindowName:',event.WindowName) print ('Ascii:', event.Ascii, chr(event.Ascii) ) print ('Key:', event.Key) print ('KeyID:', event.KeyID) print ('ScanCode:', event.ScanCode) print ('Extended:', event.Extended) print ('Injected:', event.Injected) print ('Alt', event.Alt) print ('Transition', event.Transition) print ('---') # check to see if this key has ever been pressed before # if it has not then add it and set its start value to zero. if event.Key not in keyDatabase: keyDatabase[ event.Key ] = 0 ; # Incurment the key value keyDatabase[ event.Key ] += 1 ; return True # When the user presses a key down anywhere on their system # the hook manager will call OnKeyboardEvent function. hm = pyHook.HookManager() hm.KeyDown = OnKeyboardEvent hm.HookKeyboard() while True : pythoncom.PumpWaitingMessages() 

My question is:

  • What can cause this crash when copying and the past?
0
source share
2 answers

In python, Ctrl + C throws a KeyboardInterrupt exception.

http://docs.python.org/2/library/exceptions.html#exceptions.KeyboardInterrupt

+1
source

If you want to catch KeyboardInterrupt exceptions, you can use a nested loop. Thus, when KeyboardInterrupt occurs, the program terminates only the inner loop.

 while True: try: while True: pythoncom.PumpWaitingMessages() except KeyboardInterrupt: pass 
+1
source

All Articles