Availability of Java Swing Shift + F10

For accessibility requirements, Shift+ is F10supposed to open the context menu of the context menu.

In Swing, one approach is simply to add key bindings to every component you make. However, I experimented with the EventQueue extension to handle all presses Shift+ F10. In particular, I redefined dispatchEvent (AWTEvent) to convert Shift+ F10KeyEvents to the right mouse button:

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 the ability to Shift+ F10close any JPopupMenus that starts. Any idea if this solution is workable, or are there any better ways to fulfill this requirement?

+3
1
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

Source: https://habr.com/ru/post/1776317/


All Articles