Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

Carbon mouse event handling

瀏覽次數:6 次
跳到第一則未讀訊息

nikkoara

未讀,
2006年11月16日 晚上11:24:182006/11/16
收件者:
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

未讀,
2006年11月17日 上午9:01:442006/11/17
收件者:
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

未讀,
2006年11月17日 上午9:46:242006/11/17
收件者:
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

未讀,
2006年11月17日 下午5:47:542006/11/17
收件者:
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

未讀,
2006年11月20日 凌晨3:45:042006/11/20
收件者:
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 則新訊息