一旦检测到按键,您可以发送暂停键,以便暂停正在播放的 iTunes 歌曲,您可以使用boolean 变量来检测在键盘上键入或发送的快捷键之间程序(以备不时之需)
或
您可以使用一些 c 代码(将 c 程序与您的 java 程序一起启动)看看@Dave Delongs 在这里的回答 Modify NSEvent to send a different key than the one that was pressed
如果您需要键码Where can I find a list of Mac virtual key codes?
例如,如果您的音乐程序使用p to play songs 和r to listen to the next song,iTunes 使用spacebar 播放歌曲并使用right arrow key 转到下一首,您可以修改@Dave Delongs 答案这里是更改:-
#import <Cocoa/Cocoa.h>
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
//0x31 is the virtual keycode for "Spacebar"
//0x23 is the virtual keycode for "p"
if (CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode) == 0x31) {
CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, 0x23);
}
//0x7C is the virtual keycode for "Right arrow"
//0x0F is the virtual keycode for "R"
if (CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode) == 0x7C) {
CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, 0x0F);
}
return event;
}
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CFRunLoopSourceRef runLoopSource;
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, 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);
[pool release];
exit(0);
}