Error pressing Alt+Tab using pyHook

Asked

Viewed 217 times

1

I am working on a python script that captures keystrokes typed by the user. For this, I used the pyHook module. However, using the program, I realized that every time the user presses Alt+Tab, an error occurs. The simplified version that synthesizes the part of my program in which the error occurs is as follows:

import pyHook,pythoncom
def teclado(event):
    print(event.Key)
    return True

hm=pyHook.HookManager()
hm.KeyDown=teclado
hm.HookKeyboard()
pythoncom.PumpMessages()

The code perfectly shows all keystrokes typed, but when pressing Alt+Tab, the following error occurs:

TypeError: KeyboardSwitch() missing 8 required positional arguments: 'msg', 'vk_code', 'scan_code', 'ascii', 'flags', 'time', 'hwnd', and 'win_name'

And the program ends. How can I avoid this error by preserving the Alt+Tab function in windows?

  • 1

    I don’t know Pyhook. But looking at Stackoverflow in English I found information that could be an error directly in the original code. The existing answer suggests a possible solution involving modifying and recompiling Pyhook: http://stackoverflow.com/questions/26156633/pythoncom-crashes-on-keydown-when-used-hooked-to-certain-applications

  • I read everything, but I don’t understand what the guy proposed. However, I visited related links and discovered this link: https://github.com/Answeror/pyhook_py3k. In it, there is a supposed fix to the problem of pyHook. Only I couldn’t install because I don’t know what the "path-to-swig.exe" part of the installation is. If you figure out how to install, you can add a response with this link teaching you to install. If it works, I accept. @

  • About Swig: http://www.swig.org/. I’m sorry, mate, but I’m not in a position to pursue that. Besides, this installation difficulty would be another question, if it was part of the scope of the site (software installation is not part of the scope here - try on [su]).

  • 1

    I didn’t know that Swig was software. As you told me, I downloaded it and managed to install pyHook fixed. I tested and the problem disappeared. Thank you for your time. @Uiz

  • Ah, for nothing. : ) In this case, create yourself an answer.

1 answer

0


This is an error in the pyHook script itself. A solution to this problem is to install a modified pyHook found here. To install, you need to download Swig here.

Installation:

  1. Open the cmd
  2. Give up your current pyHook with the command: Pip Uninstall pyHook
  3. By cmd, travel to the folder extracted from the corrected pyHook downloaded (pyhook_py3k-master)
  4. Type the command: python setup.py build_ext --swig=path-to-swig.exe
  5. After success, type: Pip install .

Note: path-to-swig.exe is the path to the swig.exe file found inside the folder downloaded by the Swig website.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.