[python-win32] hook to obtain WM_GESTURE

Tim Roberts timr at probo.com
Thu May 9 18:46:49 CEST 2013


John Grant wrote:
>
> I would like to obtain multi-touch events in my Python code (running
> inside Blender's python environment). I have looked around the web for
> a python module that works with py 3.3 (others work with py 2.x), but
> I have not found one. So, I'm trying to build one.
>
> I believe I can use the 'ctypes' to call the function I need,
> GetGestureInfo.

That gets you gestures, but not multitouch.  If all you need is the
zoom, pan and rotate gestures and a two-finger tap, this will do it. 
For more complicated gestures, most solutions are custom right now.


> This function requires 2 parameters as input, the lParam from WndProc
> and a pointer to the GESTUREINFO structure.
>
> * I hope I can use 'ctypes' to declare the GESTUREINFO structure in my
> python code.*
> I see it is possible to pass structures as pointers using ctypes as well.

The structure doesn't have any pointers, so this should be straightforward.


> *** The problem seems to be obtaining the lParam from WndProc. ***
>
> My idea is to provide a callback function (again using ctypes to
> declare this callback) and use the SetWindowsHookEx function, passing
> my callback and the WH_CALLWNDPROC hook ID.
>
> Does this sound like it will work?

No, a Windows hook is the wrong answer.  That lets you intercept
messages from other processes (which is why hooks need to be in a DLL --
the DLL actually gets copied and injected into the processes being
hooked).  In your case, I assume you're trying to intercept gestures in
a window within your own process.  Is that right?  As long as you have
the window handle, all you need to do is subclass the window.  That
means you make yourself the wndproc for that window, so you get first
shot at all the messages.

Here's an example that shows how to do this in wxPython, but you can
eliminate the wxPython part of it.  The key point is using
win32gui.SetWindowLong to load your own function in
win32con.GWL_WNDPROC, and remembering the return value so you can call
the original function.
    http://wiki.wxpython.org/HookingTheWndProc

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list