- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
如果您的键盘上有播放/暂停/等按钮(音乐控制快捷方式),当您按下这些按钮时,iTunes 将打开(至少在 Mac 上是这样)。
如果你最近打开了另一个音乐播放器,比如 Spotify,它实际上会拦截快捷键,而 iTunes 不会做任何事情。
好吧,我想用 Java 制作一个音乐播放器,并且我想要有相同的行为。我希望我的应用程序拦截此类快捷方式,而其他程序应该无法干扰。
我正在使用 JavaFX,尽管我认为这并不重要。
我怎样才能做到这一点?
我已经能够检测到用户使用 JNativeHook 按下的键,但我不知道如何拦截 key ,以便其他应用程序不会对它们执行任何操作。
最佳答案
一旦检测到按键,就可以发送暂停键,这样 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?,您可以使用不同的键盘快捷键并修改 c 程序以在按下 Itunes 快捷键时发送您的快捷键。
例如,如果您的音乐程序使用p 播放歌曲
,使用r 播放下一首歌曲
,而 iTunes 使用空格键
播放歌曲和右箭头键
去下一首,你可以做修改@Dave Delongs answer here are the changes :-
#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);
}
关于java - 如何拦截Java中的音乐控制键盘快捷键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36729554/
如何检测在 WPF 中按下了 Ctrl + O 等快捷键(独立于任何特定控件)? 我 try catch KeyDown但KeyEventArgs不会告诉我 Control 或 Alt 是否已关闭。
我想给 JMenuItem 添加一个热键。我学会了如何添加看起来像 CTRL+SOME_KEY 的键。但我需要的是三重热键。类似 CTRL+ALT+W 的东西。我想给 JMenuItem 添加一个热键
我被要求制作一个日文版的桌面应用程序,将来可能会是中文版。所有字符串都保存在资源文件中,其中一些包含用于显示访问/快捷键的下划线,例如“_Save”或“Save _As ...”。当按下 Alt 键时
命令 我们在前面HelloWord章节中已经提到了命令写法,这里再重温一下。 context.subscriptions.push(vscode.commands.registerComma
我目前正在 MSDN 中游泳(淹没),寻找此问题的答案: 像 OneNote 一样,我希望我的通用 Windows 应用程序能够监听快捷方式。然后运行一些东西,并显示结果。 所以我发现了这个关于 La
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我对 Qt 完全陌生。 如何在 Mac 上用代码定义 Cmd + 数字键 键序列? 对于 Windows,我有 QKeySequence(QString("Ctrl+") + QString::num
我为javascript函数制作了一个快捷键。我设置了 S 键来启动此功能,因此设置了 Z 键来清除间隔功能,但我对此感到厌倦,当按 Z 键时,间隔不会停止:( var isCtrl = false;
问题: 我有一个 WebBrowser 并公开了它的 ActiveX 方法。在我的 WebBrowser 所在的表单中,我有一个带有快捷方式的 MainMenuStrip。只有当我在我的表单上的文本框
我正在尝试实现编辑器的快捷方式,但到目前为止还没有取得任何成功。 我想覆盖一些默认的 QScintilla 快捷键。我读过这篇answer ,但我不确定这是否有助于解决我的问题。 我还阅读了 Scin
基本配置(萌新看,大佬请跳到下一节) 1、创建项目 点击Create New Project创建新的项目,点击Open打开已有的项目。 先选择左侧的项目类型,在右边选择项目的所在目录,强烈
当我想用快捷键 ctrl + / 注释或注释掉我的代码时,IntelliJ 将 // 放在第一列而不是电流。当前如何发表评论? 例子 System.out.println(set.co
我经常更改编辑器的字体大小。那么有没有人知道快捷键?到目前为止,我必须通过几次点击来使用菜单:首选项 -> 颜色和字体 -> 基本 -> 文本字体。可以使用 CTRL + MouseWheel 会很好
如何将 Xcode 4 快捷键重置为默认值? Xcode -> 性能 -> 键绑定(bind) 我发现我设置了错误,但找不到重置为原始的方法。 我该怎么办?重新安装 Xcode 4? 最佳答案 只需单
我试图从 TEdit 的 OnKeyPress 事件中检测 Ctrl+V,并且我已经请注意,当按 Ctrl+AnyKey 时,Key 参数会采用不寻常的值。 示例: procedure TForm1.
我定义了这些 requireJs 选项: require.config({ baseUrl: '/js/', paths: { jquery: '/components
最近,FoxitReader for Linux 正式推出,我很喜欢。 http://www.webupd8.org/2015/09/foxit-pdf-reader-sees-new-linux-r
如果您通过查找或 Shift + Cmd + O 在 Appcode 中打开文件,您看不到该文件在项目中的哪个文件夹中。在项目资源管理器中,左侧顶部栏上有一个按钮,如果您将鼠标悬停在它上面,它会显示文
Eclipse 中是否有快捷键可以用其 unicode 转义序列 (\uXXXX) 替换特殊字符(重音)? 最佳答案 作为替代方法,您可以使用 Java 编辑器模板(请参阅 Eclipse Prefe
在 Visual Studio 中,当您在按钮等文本中的字母前使用“&”来获取带下划线的字母快捷方式时,ALT + that letter 执行按钮等。 我想知道如何在 Java 中做到这一点。我知道
我是一名优秀的程序员,十分优秀!