有没有办法在没有JFrame的情况下获取键盘事件?
我想让我的程序在用户按下某个快捷方式时取消隐藏主窗口。有没有办法获取全局关键事件,而不仅仅是焦点位于应用程序框架内时发生的事件?
I want to get my program to unhide main window when user presses some shortcut. Is there a way to get the global key events, not only the ones which happened when focus was inside application frame?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能会做你想做的事。请注意,此代码正在检查 Ctr-F 击键。我使用此代码从应用程序中的任何内容打开查找对话框。我很确定该应用程序必须具有焦点。至少要尝试一些东西......
编辑:我想我明白你想要什么。基本上当应用程序没有焦点时。如果是这样,那么您可能必须使用本机 API (JNI) 来连接操作系统事件,但这会迫使您使用特定的操作系统...
This might do what you want. Note that this code is checking for a Ctr-F keystroke. I use this code to open up a find dialog from anything in the application. I'm pretty sure that the app has to have focus though. Something to try at least...
EDIT: I think I understand what you want. Basically when the app does NOT have focus. If so then you'll probably have to hook into the OS events with a native API (JNI) but that forces you to a specific OS...
这可能会有用。我不确定是否有一个库适用于 Windows/Linux/Mac。对于 Windows,您将需要一些使用本机代码创建键盘挂钩的外部库。我不知道如何在其他操作系统上执行此操作。
This might be useful. I'm not sure if there is one library that will work for Windows/Linux/Mac. For Windows you will need some external library that uses native code to create a keyboard hook. I have no idea how to do it on the other OSes.
使用 JFrame 执行此操作的解决方案是将其不透明度设置为 0.0 并向其添加 Keylistener。但用户会在他的快捷栏中看到一个图标......
A solution to do this by using a JFrame is to set his opacity to 0.0 and to add the Keylistener to it. But the user will see an icon in his shortcut bar...