Binding to Media keys on Apple keyboards under OSX 10.5 [closed]

喜你入骨 提交于 2019-11-29 22:30:42

问题


I am fairly new to OSX development, so this may be easy but google has not been helpful.

I am trying to bind an action to the media keys you find on newer Apple keyboards (Play, Pause , etc). It appears that you cannot bind to these keys using the regular hotkey API but it must be possible since iTunes clearly manages. Is there some tricky undocumented API that achieves this? Any help would be much appreciated.


回答1:


After more extensive googling, this http://www.rogueamoeba.com/utm/2007/09/29/ seems to address the issue. There is no easy solution but if you are developing a real Cocoa app this seems possible at least.




回答2:


From my investigations, the operating system is trapping those key events before they are available to other processes. I created a CGEventTap, like so:

int main(int argc, char *argv[]) {
CFMachPortRef      eventTap;
CFRunLoopSourceRef runLoopSource;

eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionListenOnly, kCGEventMaskForAllEvents, myCGEventCallback, NULL);

if (!eventTap) {
    NSLog(@"Couldn't create event tap!");
    exit(1);
}

runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);

CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);

CGEventTapEnable(eventTap, true);

CFRunLoopRun();

CFRelease(eventTap);
CFRelease(runLoopSource);

exit(0);

}

And then the actual event callback is this:

CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
    if (type == kCGEventKeyUp) {
        CGKeyCode keycode = CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);
        NSLog(@"%d", keycode);
    }

    return event;
}

What you see logged to the console is that you get normal log methods for function keys (if you're holding down the "fn" key as well), but when pressing the media keys, brightness, volume, or eject keys, nothing is getting logged.

So from this, it unfortunately appears that there's no way to capture a media key event. However, I would love to be proved wrong.

EDIT: I forgot to point out that for this to work it either needs to run as root, or you need to turn on access for assistive devices in the "Universal Access" pane of System Preferences.



来源:https://stackoverflow.com/questions/1053351/binding-to-media-keys-on-apple-keyboards-under-osx-10-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!