Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping through functions
#1
Hello, I have this code:

import pythoncom, pyHook
import msvcrt

def main():
    keyLogging(False) # call with False parameter means no old hooks to clean up
    pythoncom.PumpMessages()

def keyLogging(cleanup):
    hm = pyHook.HookManager()

    # if you called this function before, uninstall old hook before installing new one!
    if cleanup:
        hm.UnhookKeyboard()

    key = msvcrt.getwche() # ask user for new hook as you did originaly

    # in the if block, only select what hook you want, clean up redundant code!
    if key == 'z':
        hm.KeyDown = test
    elif key == 'v':
        hm.KeyDown = test2

    hm.HookKeyboard() # finally, install the new hook

def test(event):
    key = msvcrt.getwche()
    if key == 'x':
        print("Worked")

    keyLogging(True) # call with True parameter means old hook needs cleanup

def test2():
    key = msvcrt.getwche()
    if key == 'x':
        print("Worked again")

    keyLogging(True)


main()
I am trying to make it do, is to call functions without stopping. When I run it I want to press a key and call either test or test2 and after I press again a key it will return back to keylogging to wait for a new option, something like an infinite loop that every time I will choose which function to call. With this code I get this error:

Error:
zxWorked Traceback (most recent call last): File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pyHook\HookManager.py", line 351, in KeyboardSwitch return func(event) File "C:\Users\WantedStarling\source\repos\KeyLogger\KeyLogger\KeyLogger.py", line 30, in test keyLogging(True) # call with True parameter means old hook needs cleanup File "C:\Users\WantedStarling\source\repos\KeyLogger\KeyLogger\KeyLogger.py", line 13, in keyLogging hm.UnhookKeyboard() File "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\pyHook\HookManager.py", line 299, in UnhookKeyboard if self.keyboard_hook: AttributeError: 'HookManager' object has no attribute 'keyboard_hook'
It works for just one time but after going in the function it throws this error. As you can see I gave as an option z to call test function and then x to return back to keylogging but throwed this error.

As a solution I found this around the internet:

Quote:I found the solution. If you open HookManager.py and change all 'key_hook' words to 'keyboard_hook' no more error occurs

I tried it, it doesn't give me this error but after going back to keylogging program doesn't accept any more inputs but no error is shown. Any answer would be appreciated.

Thank you for your time
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020