>> Malte Skarupke schrieb:
>> > I'm trying to get the 3D Space Navigator
>> > (http://www.3dconnexion.com/) to work in Python. I quickly found a
>> > solution using comtypes on the 3DConnexion developer forum: 
>> > http://www.3dconnexion.com/forum/viewtopic.php?t=984&start=30
>> > 
>> > However that solution only works with comtypes version 0.2.1. Any
>> > newer version will result in the events not registering.
>> 
>> I've looked up the thread in the forum.  A user codenamed 'kitsu' wrote:
>> 
>>     Note that there is a error in the comtypes 0.3.2 GetEvents function which
>>     causes it to miss all events. I've reported it to the mailing list, but
>>     until it is fixed use comtypes 0.2.1.
>> 
>> I have not found the 'report' he mentions on the mailing list.
>> Dois the code snippet that he posted here
>>   http://www.3dconnexion.com/forum/viewtopic.php?t=984&start=33
>> work with comtypes 0.2.1, and not with later comtypes?
>> 
> 
> The code from the forum works with version 0.2.1, but not with 0.3.2
> or any newer version.

The biggest change from 0.2.1 to 0.3.2 in the events handling code is that
you must, in newer versions, keep the object returned from GetEvents() or
ShowEvents() alive.  When this object does away the connection is released.
In former versions the connection was attached to the 'source object' itself.

Here is a script that works with current comtypes:

<snip>
from comtypes.client import GetEvents, CreateObject

class SensorListener:
    """Catch and print sensor events."""
    def __init__(self, sensor):
        self.sensor = sensor
   
    def SensorInput(self, this, inval=None):
        print "Rot: ",
        print self.sensor.Rotation.X,
        print self.sensor.Rotation.Y,
        print self.sensor.Rotation.Z
        print "Trans: ",
        print self.sensor.Translation.X,
        print self.sensor.Translation.Y,
        print self.sensor.Translation.Z


def PumpMessages():
    from ctypes import windll, byref
    from ctypes.wintypes import MSG
    user32 = windll.user32
    msg = MSG()
    PM_REMOVE = 0x0001
    while user32.GetMessageA(byref(msg), 0, 0, 0):
        user32.TranslateMessage(byref(msg))
        user32.DispatchMessageA(byref(msg))

def main():
    # Get device
    device = CreateObject("TDxInput.Device")

    if device.Connect() == 0:
        print ": Connected!"
        con = GetEvents(device.sensor, SensorListener(device.sensor))
        try:
            PumpMessages()
        except KeyboardInterrupt:
            return


if __name__ == "__main__":
    main()

<snip/>
There is still one problem with this code, and I have no idea where it
comes from:  It /should/ be possible to replace the PumpMessages() call
in the above code by a call to 'comtypes.client.PumpEvents(timeout)'.
However, this only works when comtypes is initializing a multi threaded
apartment; this is done by executing 'import sys; sys.coinit_flags = 0'
*before* the initial import of comtypes.

-- 
Thanks,
Thomas

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
comtypes-users mailing list
comtypes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/comtypes-users

Reply via email to