Member Avatar for sravan953

Hey guys,

I want some of your help....

How do I make a keylogging application in Python without using pyHook? Any other module for that? I heard that learning about threading will help...is it true?

Thanks guys...

Threading won't help to make a keylogger. Threading is simply running multiple processes at the same time. There is probably another keylogging library out there but pyhook is better. You can also write one on your own if you know c++

commented: "Threading is simply running multiple processes at the same time." FAIL. -1
commented: you didnt deserve that +3

If you use the Windows OS:

# get the character of a key pressed (no return key needed)
# works only in the command window and with Windows OS
# msvcrt.dll is the MS C library containing most standard C functions

from msvcrt import getch

print "press a char key (escape key to exit)"

while True:
    z = getch()
    # escape key to exit
    if ord(z) == 27:
        break
    print z,
Member Avatar for sravan953

WOHOW sneekula! It worked! Can you please explain to me the innards of your prog? Awesome! I'm gonna do some research on getch()!

The contents of msvcrt.dll and its related dll's are used by Microsoft's C/C++ compilers for crt io. On Windows machines this dll is usually located in Windows/system32 and Python can access it.

If you are calling some of the msvcrt.dll C functions that pass arguments to and from Python you need to use module ctypes. Here is an example:

from ctypes import *

# declare the decorator with @ just before the function
# to make C functions in msvcrt.dll available
@cdecl(c_char_p, 'msvcrt', [c_char_p, c_int])
def strchr(string, c):
    """find a character in a string with C function strchr()"""
    return strchr._api_(string, c)

# ord() needed to converted from a single character Python string into a C char
print strchr('abcdefg', ord('d'))  # result --> defg

To get details on the C functions check this MS reference:
http://msdn.microsoft.com/en-us/library/b34ccac3(VS.80).aspx

sraven, this only works when the command prompt window is in focus. If you're trying to capture all input for other applications too, then you'll need to use pyHook or be able to expand on sneekula's getch() example, but I'm not sure how to make that grab key presses with the command prompt not being in focus....

If you have a Linux/Unix machine then you can use module termios ...

# get a character without pressing Enter
# see http://www.daniweb.com/forums/post898236-3.html

import termios  # linux/unix Python installations only

def getkey():
    fd = sys.stdin.fileno()
    old = termios.tcgetattr(fd)
    new = termios.tcgetattr(fd)
    new[3] = new[3] & ~termios.ICANON & ~termios.ECHO
    new[6][termios.VMIN] = 1
    new[6][termios.VTIME] = 0
    termios.tcsetattr(fd, termios.TCSANOW, new)
    key = None
    try:
        key = os.read(fd, 4)
    finally:
        termios.tcsetattr(fd, termios.TCSAFLUSH, old)
    return key
 
 
# use this list to find the key that we want
# this is hard coded for xterm
def chkkey(key):
    keylist = {'\x1b':'escape', '\x7f':'backspace', '\x1bOH':'HOME',
        '\x1bOP':'F1',  '\x1bOQ':'F2',   '\x1bOR':'F3',  
        '\x1bOS':'F4', '\x1b[15':'F5', '\x1b[17':'F6',  
        '\x1b[18':'F7', '\x1b[19':'F8', '\x1b[20':'F9', 
        '\x1b[21':'F10', '\x1b[23':'F11', '\x1b[24':'F12',
        '\x1b[A':'ARROW_UP', '\x1b[B':'ARROW_DN',
        '\x1b[C':'ARROW_RT', '\x1b[D':'ARROW_LT',
        '1':'1', '2':'2', '3':'3', '4':'4', '5':'5', '6':'6', 
        '7':'7', '8':'8', '9':'9', '0':'0' }
 
    if key in keylist:
        return keylist[key]
    else:
        return "Unknown"

If you use the Windows OS:

# get the character of a key pressed (no return key needed)
# works only in the command window and with Windows OS
# msvcrt.dll is the MS C library containing most standard C functions

from msvcrt import getch

print "press a char key (escape key to exit)"

while True:
    z = getch()
    # escape key to exit
    if ord(z) == 27:
        break
    print z,

That's not a real key logger and you know it. Fail to the OP for accepting it as a solution without the most obvious test. Goes to show just how lazy he is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.