在Java中捕获全局按键 [英] capturing global keypresses in Java

查看:123
本文介绍了在Java中捕获全局按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,每当用户在我的Swing应用程序中的任何位置按空格键时,我想触发一个事件(暂停/取消暂停某些媒体)。

So I want to trigger an event (pausing/unpausing some media) whenever the user presses spacebar anywhere in the my Swing app.

由于控件太多和可能有焦点的面板,实际上不可能向他们添加关键事件(更不用说粗略)。

Since there are so many controls and panels that could have focus, its not really possible to add keyevents to them all(not to mention gross).

所以我找到了

KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher()

非常棒,您可以注册全局按键预处理程序。但是有一个主要的问题 - 在输入字段,表格单元格等中会一直输入空格,我显然不想触发暂停事件!

which is awesome, you can register global keypress pre-handlers. There's a major problem though - spaces will be typed all the time in input fields, table cells, etc, and I obviously dont want to trigger the pause event then!

所以有任何想法吗?
也许有办法全局检测光标是否专注于允许文本输入的内容,而无需检查所有可编辑控件的列表(呕吐!)?

So any ideas? Perhaps there is way to detect globally whether the cursor is focused on something that allows text input, without having to check through a list of all the editable controls(vomit!)?

推荐答案

我想你自己回答了 - 是的,我认为你可以找到有焦点的当前元素,如果它是某个字段类的实例,您为了暂停事件而忽略该空间。如果它接缝很重,请不要担心,对于JVM来说,instanceof是非常快的(并且在任何情况下你都在谈论对处理器来说是人类规模的事件)。

I think you answered that yourself - yes I think you can find out the current element that has focus, and if it is an instanceof a certain field class, you ignore the space for the purpose of pause event. If it seams heavy handed, don't worry, instanceof is VERY fast for the JVM (and in any cause you are talking human scale events which are an eon to a processor).

这篇关于在Java中捕获全局按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆