开发者

Java Swing Shift+F10 Accessibility

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

In Swing, one approach is to just add the key binding to every component you make. However, I've experimented wit开发者_运维知识库h 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);
        }
   }
}

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜