PyUserInput icon indicating copy to clipboard operation
PyUserInput copied to clipboard

Crash when switching between windows

Open AlexanderPolenov opened this issue 8 years ago • 5 comments

OS: Windows 10. When I change active window, program crashes with error:

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

class DoubleClicker(PyMouseEvent):
    def __init__(self):
        PyMouseEvent.__init__(self)

    def click(self, x, y, button, press):
        if button == 1:
            if press:
                print(x,y)

I also get almost the same error when use PyKeyboardEvent.

AlexanderPolenov avatar Jun 04 '16 09:06 AlexanderPolenov

Is there a stacktrace? Likely a bug in PyHook, which we use for events on Windows.

http://stackoverflow.com/questions/27363268/how-can-i-avoid-typeerror-mouseswitch-missing-8-required-positional-arguments https://sourceforge.net/p/pyhook/bugs/1/

Unfortunately the project doesn't seem to be maintained. This leaves us in a tricky situation.

pepijndevos avatar Jun 04 '16 09:06 pepijndevos

No, there is no stacktrace. Only this error message.

AlexanderPolenov avatar Jun 04 '16 10:06 AlexanderPolenov

A possible workaround for the bug in PyHook is to use a simpler mechanism when hooking on Windows. That is what I use in pynput (see here: https://github.com/moses-palmer/pynput/blob/master/lib/pynput/_util/win32.py#L244).

The strange requirement for a DLL-function is not imposed for the hooks used by PyUserInput, so a simple ctypes based approach works, and the native code from PyHook is not strictly necessary.

Relying on ctypes brings the additional benefit of making the final projects easier to distribute.

moses-palmer avatar Jun 04 '16 10:06 moses-palmer

I'd be in favour of replacing PyHook with ctypes or cffi if anyone has the knowledge and time to pull it off. I certainly don't with exams coming up.

pepijndevos avatar Jun 04 '16 12:06 pepijndevos

I've done some research on this topic which you can find here: https://github.com/ActivityWatch/aw-watcher-afk/issues/12

I need this functionality for the project which the linked issue belongs to, might investigate ctypes or cffi but since the scope of the functionality I need is much smaller than the scope of PyUserInput I might end up not doing the full thing. I'll update the linked issue with details as it's resolved.

ErikBjare avatar Apr 12 '17 14:04 ErikBjare