简体   繁体   English

Java中的按键绑定与按键监听器

[英]Key bindings vs. key listeners in Java

I note that in Java / Swing there seem to be at least two different ways of handling key events: 我注意到,在Java / Swing中,似乎至少有两种不同的方法来处理关键事件:

What are the advantages / disadvantages of each, and when should you prefer one rather than the other? 两者的优点/缺点是什么?何时应该选择一个而不是另一个?

when should you prefer one rather than the other? 什么时候应该选择一个而不是另一个?

Prefer Key Bindings since they were introduced. 自引入键绑定以来,首选键绑定。 A KeyListener is a lower level connection with events. KeyListener是具有事件的较低级别的连接。

That page for the key bindings covers a lot of the reasons I would tend to use them rather than a KeyListener . 关于键绑定的页面涵盖了很多我倾向于使用它们而不是使用KeyListener It lists many things which are simply 'not available' to a KeyListener . 它列出了很多对于KeyListener “不可用”的东西。 EG choices of: EG的选择:

  • WHEN_FOCUSED
  • WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
  • WHEN_IN_FOCUSED_WINDOW

The more I read the linked document, the less I can understand the need to ask the question. 我阅读链接文档的次数越多,对所提问题的理解就越少。 EG: 例如:

An alternative to key bindings is using key listeners. 键绑定的替代方法是使用键侦听器。 Key listeners have their place as a low-level interface to keyboard input, but for responding to individual keys key bindings are more appropriate and tend to result in more easily maintained code. 按键侦听器是键盘输入的低级接口,但是对于响应单个按键,按键绑定更为合适,并且往往会导致代码维护更加容易。 Key listeners are also difficult if the key binding is to be active when the component doesn't have focus. 如果要在组件没有焦点时激活键绑定,则键侦听器也很困难。 Some of the advantages of key bindings are they're somewhat self documenting, take the containment hierarchy into account, encourage reusable chunks of code ( Action objects), and allow actions to be easily removed, customized, or shared. 按键绑定的一些优点是它们可以自我记录,可以考虑容纳层次结构,鼓励可重用的代码块( Action对象),并允许轻松地删除,自定义或共享操作。 Also, they make it easy to change the key to which an action is bound. 而且,它们使更改动作绑定的键变得容易。 Another advantage of Actions is that they have an enabled state which provides an easy way to disable the action without having to track which component it is attached to. Actions另一个优点是它们具有启用状态,该状态提供了一种轻松的方式来禁用该动作而不必跟踪其附加到哪个组件。

Text components 文字成分

As noted by @Robin, text components also have DocumentListener & DocumentFilter which can be added for functionality more suited to text documents. 如@Robin所述,文本组件还具有DocumentListenerDocumentFilter ,可以添加它们以提供更适合文本文档的功能。 See Text Component Features for more information on document listeners & filters . 有关文档侦听 过滤器的更多信息,请参见文本组件功能

  1. KeyBindings (high abstraction) 按键绑定(高度抽象)

advantages 好处

  • settable, shareable, 可设置,可共享,

  • designated for simple shortcuts, without un_wanted side effects (most of those events are quite simple and settable) 指定用于简单的快捷方式,没有不必要的副作用(大多数事件非常简单且可设置)

  • confortly solving any issue with focus in window (settable too, sure in Java the window must has focus on the screen) 轻松解决窗口焦点问题(也可以设置,请确保在Java中窗口必须焦点在屏幕上)

  • Swing internally to use KeyBindings, built_in shortcuts, actions, more in Key Bindings by @camickr (in Swing are implemented interesting shortcuts & actions) 在内部摆动以使用@camickr的按键绑定中的键绑定 ,内置快捷键,操作 (在Swing中实现了有趣的快捷键和操作)

  • output should be to the Swing Action (the same high possible abstraction in Swing) 输出应该是Swing Action(Swing中相同的可能的高抽象度)

disadvantages 缺点

  • not possible to override all keys from keyboards 无法覆盖键盘上的所有键

  • not possible to override three or more keys are pressed in the same time 无法覆盖同时按下三个或更多键

  • code looks like as very complicated (not true at all, code is sorter in most cases in compare with the same code from KeyListener) 代码看起来非常复杂(一点都不是真的,与来自KeyListener的相同代码相比,大多数情况下代码是排序器)

  • removed for more details to see commnent by @camickr (required Swing Timer for repeated actions) 已删除以获取更多详细信息,以查看@camickr的常用命令 (重复操作需要Swing计时器)

  • not possible to consume() with one method implemented in API 使用API​​中实现的一种方法无法使用Consumption()

.

KeyListeners (low level listener) KeyListeners(低级侦听器)


advantages 好处

  • very easy to use, intuitive 非常易于使用,直观

  • code is very short for one two key events 一两个关键事件的代码很短

  • not required any knowledge about Swing, Java 不需要任何有关Swing,Java的知识

  • is possible to override tree or more keys are pressed (eg), for very complicated keyshortcuts, then there doesn't matter which one can firing any, separate key events 可能会覆盖树或按下更多键(例如),对于非常复杂的快捷键,那么哪个人可以触发任何单独的键事件都没有关系

  • is possible to programatically to event.consume() 可以通过编程方式到event.consume()

  • is possible to listening for non_finalized, internal events from Compound JComponents (JComboBox, JSpinner ...) 可以监听来自复合JComponents(JComboBox,JSpinner ...)的未完成的内部事件

disadvantages 缺点

  • not accesible for part of containers and JComponents 不适用于部分容器和JComponents

  • (J)Component should be focus owner and must be focusable (J)组件应该是焦点所有者,并且必须是可焦点的

  • not designated for Swing JComponents 没有为Swing JComponents指定

.

AWTEventListener AWTEventListener


  • to combine all Key and Mouse events, low level listener as is possible in Java 组合所有Key和Mouse事件,以及Java中可能的低层侦听器

  • basically there isn't reason to use this listener for most of (even are very complicated) GUI based on Swing 基本上没有理由针对大多数基于Swing的GUI(甚至非常复杂)使用此侦听器

  • I see this listener implemented in custom Componets based on AWT required peers came from native OS 我看到此侦听器在基于AWT的自定义组件中实现,所需对等体来自本机操作系统

  • but there are excelent implementations for AWTEventListener Application Inactivity and Global Event Listeners by @camickr 但是@camickr提供了AWTEventListener 应用程序不活动和全局事件监听器的出色实现

.

notice: ordering of Key events is different platform by platform 注意:关键事件的顺序因平台而异


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

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