根据可访问性要求, Shift + F10 应该打开右键单击上下文菜单。
在Swing中,一种方法是只将键绑定添加到您创建的每个组件。但是,我已经尝试扩展EventQueue以处理所有 Shift + F10 按下。特别是,我重写了dispatchEvent(AWTEvent),将 Shift + F10 KeyEvents转换为右键单击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。不知道这个解决方案是否可行,或者是否有更好的方法来满足这一要求?
答案 0 :(得分:0)
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);