View Full Version : Controlling The Mouse
Douglas O'Brien
2002.08.04, 10:05 PM
How do you set the mouse coordinates and simulate clicks in software?
What's your reason for doing this? Are you trying to automate another program or something else?
What's the target platform (9 or X)?
Douglas O'Brien
2002.08.05, 02:16 AM
I am trying to automate another program in OS 9.
You know that old puzzle that came with Mac OS 7 where a piece was missing and you slide other ones in that spot and you had to make a picture. I wrote a program that solves a scrambled on of those of any size and I want to have it physically unscramble the Apple version. That beats taking a screen reading and outputing the moves to a log file.
Actually, is there a C++ class called "Pane" which does this?
Nimrod
2002.08.05, 04:32 AM
Actually, I'd quite like to know this. For an editor I'm developing (on-and-off). When I drag an object, the cursor disappears, and as soon as you move the mouse it positions it back where it was (remembering the offset), so you can keep dragging to infinity. At least that's what happened on the PC, I don't know how to set the mouse positions in Carbon.
Nimrod: now that's more my kind of answer.
you want this (http://www.inio.org/~inio/glutWarpCursor.sit).
Should be fairly easy to hack it out of the glutWarpCursor wrapper it's in.
[edit]: you need to include CursorDevicesGlue.o in the project for that to work.
The OS9 side could be simplified a bit by using lowmem, but this does it the Right Way™.
I'm using it for the exact same purpose btw
Nimrod
2002.08.05, 06:35 AM
Wow, thanks Inio. A couple of things: is the OS9 code for Classic or Carbon? If it's Carbon, does it work on X as well? Just because I wouldn't mind having one app that runs on 9 & X, although it's nothing too major.
To anyone else reading this, this code is also useful for mouse-controlled games (e.g. FPS), so there is no limit on how far the player can turn.
Originally posted by Nimrod
Wow, thanks Inio. A couple of things: is the OS9 code for Classic or Carbon? If it's Carbon, does it work on X as well? Just because I wouldn't mind having one app that runs on 9 & X, although it's nothing too major.
The OS 9 code is for Carbon OS 9, and will not work under OS X as the Cursor Device Manager does not exist there. The OS X code is also Carbon but will not work under OS 9 as it relies on Core Graphics (unavailable in OS 9). If you're only running under Classic, all the code you need is in glutWarpCursor.c and will be switched over to automatically (look at the #ifs).
if you're wondering why the code is so nasty:
The OS9 code is nasty because you can't weak link to InterfaceLib from a Carbon app (it will refuse to launch in 9). The OSX code is nasty because CoreGraphics is a MachO library and therefore must be manually imported into a CFM app (there's no way to link against a framework from a CFM app).
Camacho: if you want to put this in the code snippets section, you have my permission.
Douglas O'Brien
2002.08.05, 01:57 PM
>When I drag an object, the cursor disappears, and as soon as you move the mouse it
>positions it back where it was (remembering the offset), so you can keep dragging
>to infinity.
There are also ways to poll input with input spockets or something I think, so that even if the mouse is in a corner, it will say the magnitude of movenment past the corner its trying to go.
>Nimrod: now that's more my kind of answer.
What?! And mine wasn't?
Originally posted by Douglas O'Brien
>Nimrod: now that's more my kind of answer.
What?! And mine wasn't?
Yours wasn't the kind I could easily answer :).
ylaporte
2002.08.05, 08:54 PM
I don't know how to simulate a click but I do know how to solve the infinite dragging problem under X (I wouldn't know on 9).
Strangely, the related functions are in CGDirectDisplay.h
One is
CGDisplayErr CGDisplayMoveCursorToPoint(CGDirectDisplayID display, CGPoint point);
Which moves the cursor to a specific position on the screen (starting top left)
The other one is
void CGGetLastMouseDelta( CGMouseDelta * deltaX, CGMouseDelta * deltaY );
which, according to the behavior I have seen so far, is not limited by the screen boundaries which is sort of logical since it is a MOUSE delta and not a CURSOR delta ;) )
As for simulating clicks, I'd guess there is a way to add events in the event queue. I other words, would it be possible to create and register a fake mouse up or down event?
In any case, programming that sort of puzzle in Cocoa is probably fun and easy (NSMatrix is your friend).
Originally posted by ylaporte
Strangely, the related functions are in CGDirectDisplay.hMakes sense to me...
But anyway, yeah, that's how I do it. If you're trying to make a 9/X app that's not as easy to do however. The code I posted takes care of setting the mouse location under both OS9 and OSX, and is a good basis for figuring out how to call other stuff in frameworks or InterfaceLib stuff missing from CarbonLib.
Originally posted by ylaporte
As for simulating clicks, I'd guess there is a way to add events in the event queue. I other words, would it be possible to create and register a fake mouse up or down event?
Under OS 9, use PostEvent()
Under OS X, use the calls in CGRemoteDisplay.h
ededed
2002.08.11, 09:06 AM
This may help with cocoa programmers if you want to stop the cursor from moving but still want to find the x and y deltas (how much the cursor has moved) then look under Game programming / Cursors in Cocoa.
I wanted to set the cursor position but locking the cursor instead was fine.
There is a download of source code too.
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.