简体   繁体   English

如何拦截Java中的音乐控制键盘快捷键?

[英]How to intercept music control keyboard shortcuts in Java?

If your keyboard has buttons for play/pause/etc (music control shortcuts), and you press them, iTunes will open (at least on Mac). 如果键盘上有播放/暂停/等按钮(音乐控制快捷键),按下它们,iTunes将打开(至少在Mac上)。

If you recently opened another music player, like Spotify, it will actually intercept the shortcut keys, and iTunes won't do anything. 如果你最近打开了另一个音乐播放器,比如Spotify,它实际上会拦截快捷键,iTunes也不会做任何事情。

Well, I want to make a music player with Java, and I want to have the same behavior. 好吧,我想制作一个带有Java的音乐播放器,我想要有相同的行为。 I want my application to intercept such shortcuts, and other programs shouldn't be able to interfere. 我希望我的应用程序拦截这样的快捷方式,其他程序不应该干涉。

I am using JavaFX, although I don't think that really matters. 我正在使用JavaFX,虽然我认为这不重要。

How can I achieve this? 我怎样才能做到这一点?

I am already able to detect they keys the user presses using JNativeHook , but I do not know how to intercept the keys so that other applications won't do things with them. 我已经能够检测到用户使用JNativeHook按下的键,但我不知道如何拦截键,以便其他应用程序不会使用它们。

Once you detect the keys, you could send the pause key so that the song that is being played by itunes is paused, you could use a boolean variable to detect between the shortcuts being typed on the keyboard or being send by the program(in case if you need) 一旦你检测到键,你可以发送暂停键,以便暂停由iTunes播放的歌曲,你可以使用boolean变量来检测键盘上键入的快捷键或程序发送的快捷键(以防万一)如果你需要)

or 要么

You could use some c code(start the c program along with your java program) take a look at @Dave Delongs answer over here Modify NSEvent to send a different key than the one that was pressed You could have a different keyboard shortcut and modify the c program to send your shortcut keys while the Itunes Shortcut keys are pressed, if you need the key codes Where can I find a list of Mac virtual key codes? 你可以使用一些c代码(启动c程序和你的java程序)看看@Dave Delongs这里的答案修改NSEvent发送一个不同于按下的键的键你可以有一个不同的键盘快捷键并修改c程序在按下Itunes快捷键时发送快捷键,如果需要键码我在哪里可以找到Mac虚拟键码列表?

for example if your music program uses p to play songs and r to listen to the next song , and itunes uses spacebar to play songs and right arrow key to go to the next one, you could do modify @Dave Delongs answer here are the changes :- 例如,如果你的音乐节目用p to play songsr 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);
}

You may be able to use some of the code from iTunesPatch to accomplish what you're looking for, but it appears that a system daemon may need to be modified upon installation, and you will likely have to use Objective-C/Swift. 您可以使用iTunesPatch中的一些代码来完成您正在寻找的内容,但似乎系统守护程序可能需要在安装时进行修改,您可能不得不使用Objective-C / Swift。

There are further details about iTunesPatch in a blog post here . 大约有iTunesPatch在博客中进一步的细节在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM