Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Carbon mouse event handling

6 views
Skip to first unread message

nikkoara

unread,
Nov 16, 2006, 11:24:18 PM11/16/06
to
The following program exhibits a curious behaviour. Mouse up event
only gets caught once (the first time), and mouse dragged never. Mouse
down and mouse moved are getting caught consistently. The intention is
to catch them and pass them further to the other handlers in the stack.

Any suggestion is welcome. If anyone knows an *in-depth* material which
discusses Carbon please let me know. The Carbon Event Programming Guide
is a great introductory text but it didn't give the needed information,
neither did two books I consulted on Safari books online.

$ cat t.cpp

#include <assert.h>
#include <iostream>
#include <Carbon/Carbon.h>

static pascal OSStatus
mouse_handler (EventHandlerCallRef next, EventRef event, void *data)
{
switch (GetEventKind (event)) {
case kEventMouseDown:
std::cout << "Mouse button down.\n";
break;

case kEventMouseUp:
std::cout << "Mouse button up.\n";
break;

case kEventMouseMoved:
std::cout << "Mouse moved.\n";
break;

case kEventMouseDragged:
std::cout << "Mouse dragged.\n";
break;

default:
std::cout << "Unknown event.\n";
break;
}

return CallNextEventHandler (next, event);
}

int main ()
{
WindowClass wc = kDocumentWindowClass;
WindowAttributes wa =
kWindowFullZoomAttribute |
kWindowCloseBoxAttribute |
kWindowCollapseBoxAttribute |
kWindowResizableAttribute |
kWindowCompositingAttribute |
kWindowStandardHandlerAttribute |
kWindowLiveResizeAttribute |
kWindowNoConstrainAttribute;

Rect r = { 0, 0, 300, 300 };

WindowRef wr;
assert (0 == CreateNewWindow (wc, wa, &r, &wr));

ShowWindow (wr);
RepositionWindow (wr, 0, kWindowCascadeOnMainScreen);

ProcessSerialNumber psn;
assert (0 == GetCurrentProcess (&psn));
TransformProcessType (&psn,
kProcessTransformToForegroundApplication);

static const EventTypeSpec app_events [] = {
{ kEventClassMouse, kEventMouseDown },
{ kEventClassMouse, kEventMouseUp },
{ kEventClassMouse, kEventMouseMoved },
{ kEventClassMouse, kEventMouseDragged }
};

EventTargetRef target = GetEventDispatcherTarget ();
InstallEventHandler (target, NewEventHandlerUPP (mouse_handler),
sizeof app_events / sizeof *app_events,
app_events, 0, 0);

RunApplicationEventLoop ();
DisposeWindow (wr);

return 0;
}

Build and run as:

$ g++ t.cpp -o t && ./t

Ben Artin

unread,
Nov 17, 2006, 9:01:44 AM11/17/06
to
In article <1163737458.5...@j44g2000cwa.googlegroups.com>,
"nikkoara" <nikk...@gmail.com> wrote:

> The following program exhibits a curious behaviour. Mouse up event
> only gets caught once (the first time), and mouse dragged never. Mouse
> down and mouse moved are getting caught consistently. The intention is
> to catch them and pass them further to the other handlers in the stack.
>
> Any suggestion is welcome. If anyone knows an *in-depth* material which
> discusses Carbon please let me know. The Carbon Event Programming Guide
> is a great introductory text but it didn't give the needed information,
> neither did two books I consulted on Safari books online.

kEventClassMouse has low-level events that you should not be using unless you
know exactly what you are doing.
<http://www.archivesat.com/Developer_discussion_of_programming_with_Carbon/thread
265515.htm> has a good summary of why using the standard Carbon event handler is
exclusive with using those low-level events, and my advice is the same as the
advice of that post: tell us what you are trying to do so we can suggest a good
approach.

Ben

--
If this message helped you, consider buying an item
from my wish list: <http://artins.org/ben/wishlist>

I changed my name: <http://periodic-kingdom.org/People/NameChange.php>

nikkoara

unread,
Nov 17, 2006, 9:46:24 AM11/17/06
to
Ben Artin wrote:
> In article <1163737458.5...@j44g2000cwa.googlegroups.com>,
> "nikkoara" <nikk...@gmail.com> wrote:
>
> > The following program exhibits a curious behaviour. Mouse up event
> > only gets caught once (the first time), and mouse dragged never. Mouse
> > down and mouse moved are getting caught consistently. The intention is
> > to catch them and pass them further to the other handlers in the stack.
> [...]

> exclusive with using those low-level events, and my advice is the same as the
> advice of that post: tell us what you are trying to do so we can suggest a good
> approach.

The intention is that in my original post: to learn Carbon by way of
testing and one such test case was attempting to intercept the mouse
events just for the sake of intercepting them. I am guilty of trying to
translate my X experience into Carbon...

Thanks for pointing me in the right direction.

Ben Artin

unread,
Nov 17, 2006, 5:47:54 PM11/17/06
to
In article <1163774784....@j44g2000cwa.googlegroups.com>,
"nikkoara" <nikk...@gmail.com> wrote:

As you realize by now, what you tried was not learn Carbon, but learn to use
Carbon to do things you know how to do in X11. What I would do instead would be
to focus on higher-level tasks, such as dragging or processing of menu commands.

hth

John C. Randolph

unread,
Nov 20, 2006, 3:45:04 AM11/20/06
to
On 2006-11-17 06:46:24 -0800, "nikkoara" <nikk...@gmail.com> said:

> I am guilty of trying to
> translate my X experience into Carbon...

Don't fight the framework. If you want to write apps the way you do
with X windows, then just install the X package on the machines you
use, and write an X11 app.

-jcr

0 new messages