[英]How to intercept music control keyboard shortcuts in Java?
如果鍵盤上有播放/暫停/等按鈕(音樂控制快捷鍵),按下它們,iTunes將打開(至少在Mac上)。
如果你最近打開了另一個音樂播放器,比如Spotify,它實際上會攔截快捷鍵,iTunes也不會做任何事情。
好吧,我想制作一個帶有Java的音樂播放器,我想要有相同的行為。 我希望我的應用程序攔截這樣的快捷方式,其他程序不應該干涉。
我正在使用JavaFX,雖然我認為這不重要。
我怎樣才能做到這一點?
我已經能夠檢測到用戶使用JNativeHook按下的鍵,但我不知道如何攔截鍵,以便其他應用程序不會使用它們。
一旦你檢測到鍵,你可以發送暫停鍵,以便暫停由iTunes播放的歌曲,你可以使用boolean
變量來檢測鍵盤上鍵入的快捷鍵或程序發送的快捷鍵(以防萬一)如果你需要)
要么
你可以使用一些c代碼(啟動c程序和你的java程序)看看@Dave Delongs這里的答案修改NSEvent發送一個不同於按下的鍵的鍵你可以有一個不同的鍵盤快捷鍵並修改c程序在按下Itunes快捷鍵時發送快捷鍵,如果需要鍵碼我在哪里可以找到Mac虛擬鍵碼列表?
例如,如果你的音樂節目用p to play songs
而r to listen to the next song
,而itunes用spacebar
播放歌曲,用right arrow key
轉到r to listen to the next song
,你可以修改@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);
}
您可以使用iTunesPatch中的一些代碼來完成您正在尋找的內容,但似乎系統守護程序可能需要在安裝時進行修改,您可能不得不使用Objective-C / Swift。
大約有iTunesPatch在博客中進一步的細節在這里 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.