Keyloggers with pyhook
To create a simple keylogger script to record keystroke activities on a computer and to store it in a text file, we could use the pyhook
module. This will provide callback for global mouse and keyboard events in Windows systems.
Import the required modules. Here, we are importing the pyhook
and pythoncom modules from ActivePython Package. The pythoncom
module is used in this script to pump all messages for the current thread:
import pyHook, pythoncom, sys, logging
Define the file where to save the logging data. (Windows filenames use a backslash as a separator. But, in Python, backslash is an escape character, so we have to put a double slash "\\
" in path. Otherwise, we can use rawstring to define the file name.):
file_log='C:\\log.txt'
Now we can define the function that handles each keyboard event. Here, we can make use of logging modules to log each character:
def OnKeyboardEvent(event): logging.basicConfig(filename*file_log, level=logging.DEBUG...