在 Java/Swing 中更改助记符修饰键 [英] Change mnemonic modifier key in Java/Swing

查看:17
本文介绍了在 Java/Swing 中更改助记符修饰键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Swing 中设置焦点热键非常简单:

Setting focus hot keys in Swing is very easy:

  tfldPlantsNeeded = new JTextField( FIELD_LEN_MED );
  lblPlantsNeeded = new JLabel( "Plants Needed" );
  lblPlantsNeeded.setDisplayedMnemonic( 'p' );
  lblPlantsNeeded.setLabelFor( tfldPlantsNeeded );

当用户按下 ALT+p 时,这会将焦点放在 tfldPlantsNeeded JTextField 上.它还突出显示/显示将触发焦点更改的字符.(在这种情况下,当按下 ALT 时,Plants"中的 'P' 带有下划线.)

This will give focus to the tfldPlantsNeeded JTextField when the user presses ALT+p. It also highlights/displays the character that will trigger the focus change. (In this case, when ALT is pressed, the 'P' in "Plants" is underlined.)

这太棒了……嗯,有点.在 Mac 上,当用户按下 ALT(这也是 Mac 键盘上的 Option)时,助记符被高亮显示,但当 时不会触发焦点变化p 也被按下了.但是,如果用户按下 Control + Option + p,那么它会按预期"工作并且焦点会改变.(顺便说一句,如果用户确实按下 Option + p,当前聚焦的文本字段将插入有趣的字符.)

This is great ... well, kinda. On a Mac, when the user presses ALT (which is also Option on the Mac keyboard) the mnemonic is highlited, but the focus change isn't triggered when p is pressed too. If, however, the user presses Control + Option + p, then it works as "expected" and focus is changed. (As an aside, if the user DOES press Option + p, the currently focused text field will get funny characters inserted.)

我知道我可以通过 getInputMapgetActionMap 指定自定义键绑定来自己完成此操作,但是有没有办法更改应用程序全局助记符修饰符 以便我们可以使用自动键绑定并触发字符突出显示?(就我而言,我想使用 CommandMeta 作为助记词修饰键.)

I know that I can do this myself by specifying custom keybindings via getInputMap and getActionMap, but is there a way to change the application global mnemonic modifier so that we can use the automatic keybindings and trigger character highlighting? (In my case, I would like to use Command or Meta as the mnemonic modifer key.)

推荐答案

显然这并不像您想象的那么简单,但有一个方法.

Apparently this isn't as straightforward as you might think, but there is a way.

首先,对于菜单 (JMenu),有一个由外观控制的属性,称为 Menu.shortcutKeys,您可以手动设置它.这为特定外观中的菜单设置助记符修饰符.如果您想了解更多相关信息,请随时询问.

First of all, for menus (JMenu) there is a property which is controlled by the look and feel called Menu.shortcutKeyswhich you can set manually. This sets the mnemonic modifier for menus in the specific look and feel. If you want more information about this feel free to ask.

为了为所有内容设置助记符修饰符,您需要覆盖默认工具包(Toolkit).首先,运行一个main方法,用下面几行来查找它是什么

In order to set the mnemonic modifier for everything, you need to override the default toolkit (Toolkit). First of all, run a main method to find what it is with the following lines

System.out.println(System.getProperty("java.awt.headless"));
System.out.println(System.getProperty("awt.toolkit"));

如果第一行是 falsenull(参见 java.awt.Toolkit getDefaultToolkit()),那么第二行会给你用作系统默认 Toolkit 的类名.我使用 Windows,第二行给出了输出 sun.awt.windows.WToolkit.现在创建一个类来覆盖默认工具包中的 getFocusAcceleratorKeyMask.对我来说它看起来像这样

If the first line is null of false (see java.awt.Toolkit getDefaultToolkit()) then the second line will give you the class name which is used as the default Toolkit for your system. I use Windows and the second line gives the output sun.awt.windows.WToolkit. Now create a class that overrides getFocusAcceleratorKeyMask in your default toolkit. For me it looks like this

public class MyToolkit extends WToolkit {

    @Override
    public int getFocusAcceleratorKeyMask() {
        return InputEvent.CTRL_DOWN_MASK;
    }
}

最后,我们必须告诉系统使用它.在您的应用程序中,放置一行

Finally, we have to tell the system to use it. In your application, put the line

System.setProperty("awt.toolkit", "packagename.MyToolkit");

您需要在哪里为您的类设置正确的包路径.确保在启动任何 GUI 相关代码之前放置这一行,最好在 main 的第一行.现在应该将 CONTROL 设置为全局助记符修饰符(或者使用 META_DOWN_MASK 如果这是你想要的.看看 java.awt.event.InputEvent用于掩码列表.)

where you need to set the correct package path to your class. Make sure this line is placed before starting any GUI related code, preferably in the first lines of main. This should now set CONTROL as the global mnemonic modifier (or use META_DOWN_MASK if that's what you want. Look at java.awt.event.InputEvent for MASK list.).

这篇关于在 Java/Swing 中更改助记符修饰键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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