Java Swing Shift + F10辅助功能 [英] Java Swing Shift+F10 Accessibility

查看:203
本文介绍了Java Swing Shift + F10辅助功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据可访问性要求, Shift + F10 应该打开右键单击上下文菜单。

Per accessibility requirements, Shift+F10 is supposed to open right-click context menus.

在Swing中,一种方法是只为每个组件添加键绑定。但是,我已经尝试扩展EventQueue以处理所有 Shift + F10 按下。特别是,我已经重写了dispatchEvent(AWTEvent),将 Shift + F10 KeyEvents转换为右键单击mousePresses:

In Swing, one approach is to just add the key binding to every component you make. However, I've experimented with extending the EventQueue to handle all Shift+F10 presses. In particular, I've overridden dispatchEvent(AWTEvent) to convert Shift+F10 KeyEvents into right-click mousePresses:

protected void dispatchEvent(AWTEvent event) {
    if (event instanceof KeyEvent) {
        KeyEvent ev = (KeyEvent) event;
        if ((ev.getKeyCode() == KeyEvent.VK_F10) && 
                    (ev.getModifiersEx() & InputEvent.SHIFT_DOWN_MASK) > 0) {
            KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
            Component comp = kfm.getFocusOwner();
            Point mouse = MouseInfo.getPointerInfo().getLocation();
            SwingUtilities.convertPointFromScreen(mouse, comp);

            eventToDispatch = new MouseEvent(comp,
                            MouseEvent.MOUSE_RELEASED, ev.getWhen(), 0, mouse.x, mouse.y, 
                            1, true);
        }
   }
}

然而,这可以防止 Shift + F10 可以关闭任何启动的JPopupMenus。不知道这个解决方案是否可行,或者是否有更好的方法来满足这一要求?

However, this prevents Shift+F10 from being able to close any JPopupMenus that get launched. Any idea if this solution is workable, or are there better ways to accomplish meeting this requirement?

推荐答案

ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        try {
          int dotPosition = textField.getCaretPosition();
          Rectangle popupLocation = textField
              .modelToView(dotPosition);
          popup.show(textField, popupLocation.x, popupLocation.y);
        } catch (BadLocationException badLocationException) {
          System.out.println("Oops");
        }
      }
    };
    KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_F10,
        InputEvent.SHIFT_MASK);
    textField.registerKeyboardAction(actionListener, keystroke,
        JComponent.WHEN_FOCUSED);

这篇关于Java Swing Shift + F10辅助功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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