How can I avoid TypeError: MouseSwitch() missing 8 required positional arguments: 'msg', 'x', 'y', 'data', 'time', 'hwnd', and 'window_name'

前端 未结 2 948
遇见更好的自我
遇见更好的自我 2021-01-03 06:34

Trying to hook into mouse events but in my early tests the program stops responding after about 30 seconds[EDIT: See bottom of post] and gives this error

2条回答
  •  有刺的猬
    2021-01-03 07:19

    pyHook is more oriented to python 2. There are repositories in github to use it in python 3 as will as modifications and extensions and anymore, better use pynput in python 3 as follows:

    # -*- coding: utf-8 -*-
    
    from pynput.keyboard import Listener
    
    def key_recorder(key):    
        f=open('keylogger.txt','a')
        keyo=str(key)
    
        if keyo=="Key.enter":
            f.write('\n')
        elif keyo=="Key.space": 
            f.write(" ")
        elif keyo =="Key.backspace":       
            #f.write(keyo.replace(keyo,""))          
            size=f.tell()    # the size... 
            f.truncate(size-1)     
        elif keyo=="Key.alt_l" or keyo=="Key.tab":
            f.write('')  
        elif keyo=="Key.ctrl_l":
            f.write('')    
        elif keyo=="Key.alt_gr":
            f.write('')                                 
        else:
            print(keyo)
            f.write(keyo.replace("'",""))
    
    with Listener(on_press=key_recorder) as l :
        l.join()
    

提交回复
热议问题