Re: Notifying when a key is pressed and perform default tasks.
Re: Notifying when a key is pressed and perform default tasks.
- Subject: Re: Notifying when a key is pressed and perform default tasks.
- From: Boris Remizov <email@hidden>
- Date: Tue, 21 Aug 2007 10:37:14 +0400
Hi, as about your question, you may use AppleEvents to get
notifications about key pressed into yours windows and in the others.
Below placed the code I written/grab from other sample to play with
Carbon... It was many time ago...
//
// main.c
// CarbonNotification
//
// Created by Boris Remizov on 26/6/2006.
// Copyright Mercury Development, LLC 2006. All rights reserved.
//
#include <Carbon/Carbon.h>
static OSStatus AppEventHandler( EventHandlerCallRef inCaller,
EventRef inEvent, void* inRefcon );
static OSStatus HandleNew();
static OSStatus WindowEventHandler( EventHandlerCallRef
inCaller, EventRef inEvent, void* inRefcon );
static IBNibRef sNibRef;
//----------------------------------------------------------------------
----------------------
int main(int argc, char* argv[])
{
OSStatus err;
static const EventTypeSpec kAppEvents[] =
{
{ kEventClassCommand, kEventCommandProcess }
};
// Create a Nib reference, passing the name of the nib file
(without the .nib extension).
// CreateNibReference only searches into the application bundle.
err = CreateNibReference( CFSTR("main"), &sNibRef );
require_noerr( err, CantGetNibRef );
// Once the nib reference is created, set the menu bar.
"MainMenu" is the name of the menu bar
// object. This name is set in InterfaceBuilder when the nib is
created.
err = SetMenuBarFromNib( sNibRef, CFSTR("MenuBar") );
require_noerr( err, CantSetMenuBar );
// Install our handler for common commands on the application
target
InstallApplicationEventHandler (NewEventHandlerUPP
( AppEventHandler ), GetEventTypeCount( kAppEvents ), kAppEvents, 0,
NULL );
// Create a new window. A full-fledged application would do this
from an AppleEvent handler
// for kAEOpenApplication.
HandleNew();
// Run the event loop
RunApplicationEventLoop();
CantSetMenuBar:
CantGetNibRef:
return err;
}
//----------------------------------------------------------------------
----------------------
static OSStatus
AppEventHandler( EventHandlerCallRef inCaller, EventRef inEvent,
void* inRefcon )
{
OSStatus result = eventNotHandledErr;
switch ( GetEventClass( inEvent ) )
{
case kEventClassCommand:
{
HICommandExtended cmd;
verify_noerr( GetEventParameter( inEvent,
kEventParamDirectObject, typeHICommand, NULL, sizeof( cmd ), NULL,
&cmd ) );
switch ( GetEventKind( inEvent ) )
{
case kEventCommandProcess:
switch ( cmd.commandID )
{
case kHICommandNew:
result = HandleNew();
break;
// Add your own command-handling cases here
default:
break;
}
break;
}
break;
}
default:
break;
}
return result;
}
//----------------------------------------------------------------------
----------------------
DEFINE_ONE_SHOT_HANDLER_GETTER( WindowEventHandler )
//----------------------------------------------------------------------
----------------------
static OSStatus HandleNew()
{
static const EventTypeSpec kWindowEvents[] =
{
{ kEventClassCommand, kEventCommandProcess },
{ kEventClassKeyboard, kEventCommandProcess}
};
OSStatus err;
WindowRef window;
Rect newPlace;
Rect bounds;
ControlID controlId;
ControlRef controlRef;
Boolean trueValue = TRUE;
// Create a window. "MainWindow" is the name of the window
object. This name is set in
// InterfaceBuilder when the nib is created.
err = CreateWindowFromNib( sNibRef, CFSTR("MainWindow"), &window );
require_noerr( err, CantCreateWindow );
// Install a command handler on the window. We don't use this
handler yet, but nearly all
// Carbon apps will need to handle commands, so this saves
everyone a little typing.
InstallEventHandler (GetWindowEventTarget (window),
WindowEventHandler, 2, kWindowEvents, window, 0);
// Position new windows in a staggered arrangement on the main
screen
RepositionWindow (window, NULL, kWindowCascadeOnMainScreen);
// The window was created hidden, so show it
ShowWindow( window );
// Initialize the controls
controlId.id = 0;
controlId.signature = 'okbn';
GetControlByID(window, &controlId, &controlRef);
// SetControlData (controlRef, kControlEntireControl,
kControlPushButtonDefaultTag, sizeof (Boolean), &trueValue);
SetWindowDefaultButton (window, controlRef);
CantCreateWindow:
return err;
}
//----------------------------------------------------------------------
----------------------
static OSStatus WindowEventHandler (EventHandlerCallRef inCaller,
EventRef inEvent, void* inRefcon)
{
ControlButtonContentInfo iconInfo;
HICommand command;
OSStatus err = eventNotHandledErr;
UInt32 realSize;
ControlID controlId;
ControlRef controlRef;
CFStringRef text;
IconFamilyHandle iconFamily;
IconSuiteRef iconSuite;
FSRef iconFile;
Boolean yes;
Rect newRect;
switch (GetEventClass (inEvent))
{
case kEventClassKeyboard:
err = GetEventParameter (inEvent, kEventParamDirectObject,
typeHICommand, 0, sizeof (command), &realSize, &command);
if (err != noErr) break;
break;
case kEventClassCommand:
err = GetEventParameter (inEvent, kEventParamDirectObject,
typeHICommand, 0, sizeof (command), &realSize, &command);
if (err != noErr) break;
switch (command.commandID)
{
case 'ok ':
TransitionWindow ((WindowRef)inRefcon,
kWindowFadeTransitionEffect, kWindowHideTransitionAction, 0);
break;
case 'not!':
controlId.id = 0;
controlId.signature = 'text';
GetControlByID ((WindowRef)inRefcon, &controlId, &controlRef);
text = CFSTR ("Cancel button is not not for you, stupid.");
SetControlData (controlRef, kControlEntireControl,
kControlEditTextCFStringTag, sizeof (CFStringRef), &text);
CFRelease (text);
DrawOneControl (controlRef);
break;
case 'help':
controlId.id = 0;
controlId.signature = 'icon';
if (GetControlByID ((WindowRef)inRefcon, &controlId, &controlRef) !
= noErr) break;
FSPathMakeRef ("./CarbonNotification.app/Contents/Resources/DVD-
R.icns", &iconFile, &yes);
ReadIconFromFSRef (&iconFile, &iconFamily);
IconFamilyToIconSuite (iconFamily, kSelectorAllAvailableData,
&iconSuite);
iconInfo.contentType = kControlContentIconSuiteHandle;
iconInfo.u.iconSuite = iconSuite;
err = SetControlData (controlRef, kControlEntireControl,
kControlImageWellContentTag, sizeof (ControlButtonContentInfo),
&iconInfo);
DrawOneControl (controlRef);
newRect.top = 200;
newRect.left = 400;
newRect.right = 800;
newRect.bottom = 500;
TransitionWindow((WindowRef)inRefcon,
kWindowSlideTransitionEffect, kWindowMoveTransitionAction, &newRect);
break;
default:
err = eventNotHandledErr;
}
break;
}
return err;
}
On Aug 21, 2007, at 03:54 , Ricardo Diaz wrote:
No, how can I do that? Someone had said that to me before but I
couldn't do it.
_________________________________________________________________
With Windows Live Hotmail, you can personalize your inbox with your
favorite color.
www.windowslive-hotmail.com/learnmore/personalize.html?locale=en-
us&ocid=TXT_TAGLM_HMWL_reten_addcolor_0607
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden