SavinaRoja / PyUserInput

A module for cross-platform control of the mouse and keyboard in python that is simple to install and use.
GNU General Public License v3.0
1.07k stars 244 forks source link

Crash when switching between windows #88

Open AlexanderPolenov opened 8 years ago

AlexanderPolenov commented 8 years ago

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.

pepijndevos commented 8 years ago

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.

AlexanderPolenov commented 8 years ago

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

moses-palmer commented 8 years ago

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.

pepijndevos commented 8 years ago

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.

ErikBjare commented 7 years ago

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.