没有JFrame,有没有办法获取键盘事件?

编程入门 行业动态 更新时间:2024-10-28 09:24:15
本文介绍了没有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?

推荐答案

这可能会做您想要的.请注意,此代码正在检查Ctr-F击键.我使用此代码从应用程序中的任何内容打开查找对话框.我很确定该应用程序必须具有重点.至少要尝试一下...

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...

AWTEventListener listener = new AWTEventListener() { @Override public void eventDispatched(AWTEvent event) { try { KeyEvent evt = (KeyEvent)event; if(evt.getID() == KeyEvent.KEY_PRESSED && evt.getModifiers() == KeyEvent.CTRL_MASK && evt.getKeyCode() == KeyEvent.VK_F) { } } catch(Exception e) { e.printStackTrace(); } } }; Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.KEY_EVENT_MASK);

我想我明白你想要什么.基本上是当应用程序没有焦点时.如果是这样,那么您可能必须使用本机API(JNI)介入操作系统事件,但这会迫使您进入特定的操作系统...

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...

更多推荐

没有JFrame,有没有办法获取键盘事件?

本文发布于:2023-10-31 01:25:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544581.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   键盘   事件   JFrame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!