2
votes

I am making an application to control the mouse using an analog stick from a gamepad for Mac OS X (10.7.3). Currently it is working pretty well, I can control the cursor in desktop and most games. But in Team Fortress 2, I cant control the aim, but can control the cursor in the menu. Mouse wheel and clicks works everywhere. Another strange thing is that when I move the real mouse, the aim "jumps" the traveled distance from my "virtual mouse" before aiming normally, so it somewhat is receiving the events.

The game option "Raw mouse input" is disabled (I think it is not even supported in osx). And a similar application can controle the aim successfully.

I suspect the game is looking for "delta movement" or "relative movement" events or anything similar, but my code sets the position of the cursor using absolute positions. Not sute how to change this.

Here is the snippet of code used to send mouseMoved events:

EDIT: Crappy code removed!

. EDIT:

Also, because I did this way, I need to check the screen bounds manually to prevent the cursor going Crazy. So in multi-screen setups, and when the user change the resolution, it gets worst. Would be so much better if I can just send the amount of movement and let the OS take care of constraining the cursor.

.

The question is: I am doing the mouse move events the wrong way?

How can I fix this?

EDIT2: So, that was just a stupid bug, sorry =P

2

2 Answers

1
votes

I just forgot to call CGSetIntegerValueField(kCGEventMouseDeltaX, ...) (and Y) with the delta values... =P

0
votes

Finally got this working, after just a few short hours of Googling and experimenting :) Looks like Rodrigo was trying to say that you have to call CGEventSetIntegerValueField on kCGMouseEventDeltaX and kCGMouseEventDeltaY on the mouse event after constructing it.

My code, using PyObjC:

from Quartz.CoreGraphics import (
    CGEventCreate,
    CGEventCreateMouseEvent,
    CGEventGetLocation,
    CGEventPost,
    CGEventSetIntegerValueField,
    kCGEventMouseMoved,
    kCGHIDEventTap,
    kCGMouseEventDeltaX,
    kCGMouseEventDeltaY,
    )

def mouse_delta(delta_x, delta_y):
    """
    Send a MouseMoved event with the given deltaX and deltaY attributes,
    but without changing the cursor location.
    """
    # Might be a better way to get the current mouse location
    x, y = CGEventGetLocation(CGEventCreate(None))
    # The `mouseButton` parameter to CGEventCreateMouseEvent is ignored
    # unless `mouseType` is kCGEventOtherMouse{Down,Dragged,Up},
    # so let's just pass the value 0
    event = CGEventCreateMouseEvent(None, kCGEventMouseMoved, (x, y), 0)
    CGEventSetIntegerValueField(event, kCGMouseEventDeltaX, delta_x)
    CGEventSetIntegerValueField(event, kCGMouseEventDeltaY, delta_y)
    return CGEventPost(kCGHIDEventTap, event)