Re: Play/Pause, rew/ff keys ...
Re: Play/Pause, rew/ff keys ...
- Subject: Re: Play/Pause, rew/ff keys ...
- From: Kevin Gessner <email@hidden>
- Date: Tue, 11 Nov 2008 19:21:37 -0500
The pertinent methods, from my app's delegate. It listens for both
regular and special key presses, so modify accordingly.
CGEventRef
myCGEventCallback(CGEventTapProxy proxy, CGEventType type,
CGEventRef event, void *refcon)
{
// Paranoid sanity check.
if ((type != kCGEventKeyDown) && (type != kCGEventKeyUp) && (type !=
NX_SYSDEFINED))
return event;
NSEvent *e = [NSEvent eventWithCGEvent:event];
// We're getting a special event
if( ([e type] == NSSystemDefined && [e subtype] == 8) ) {
// do whatever you do with special events
// return NULL to kill the event
// we're getting a normal key event
} else if([e type] == NSKeyDown || [e type] == NSKeyUp) {
// do whatever you do with regular events
// return NULL to kill the event
}
return event;
}
- (void)listenForKeyEvents
{
CFMachPortRef eventTap, eventTapTest;
CGEventMask eventMask;
CFRunLoopSourceRef runLoopSource;
eventMask = ((1 << kCGEventKeyDown) | (1 << kCGEventKeyUp));
// try creating an event tap just for keypresses. if it fails, we
need Universal Access.
eventTapTest = CGEventTapCreate(kCGSessionEventTap,
kCGHeadInsertEventTap, 0,
eventMask, myCGEventCallback, NULL);
if (!eventTapTest) {
NSLog(@"no tap");
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:@"Quit"];
[alert setMessageText:@"Could not create an event tap."];
[alert setInformativeText:@"Please enable \"access for assistive
devices\" in the Universal Access pane of System Preferences."];
[alert setAlertStyle:NSCriticalAlertStyle];
[alert runModal];
[NSApp terminate:self];
return;
}
// disable the test tap
// causes a crash otherwise (infinite loop with the replacement
events, probably)
CGEventTapEnable(eventTapTest, false);
// Create an event tap. We are interested in key presses and system
defined keys.
eventTap = CGEventTapCreate(kCGSessionEventTap,
kCGHeadInsertEventTap, 0,
CGEventMaskBit(NX_SYSDEFINED) | eventMask, myCGEventCallback,
NULL);
// Create a run loop source.
runLoopSource = CFMachPortCreateRunLoopSource(
kCFAllocatorDefault, eventTap, 0);
// Add to the current run loop.
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource,
kCFRunLoopCommonModes);
// Enable the event tap.
CGEventTapEnable(eventTap, true);
}
HTH,
-- Kevin
Kevin Gessner
http://www.kevingessner.com
email@hidden
On Nov 11, 2008, at 1:13 PM, I. Savant wrote:
On Nov 11, 2008, at 12:35 PM, Kevin Gessner wrote:
I can give y'all some code if you're needing.
Sure, the more the merrier! :-)
--
I.S.
_______________________________________________
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