Java Swing Shift+;F10可访问性
根据可访问性要求,Shift+F10应该打开右键单击上下文菜单 在Swing中,一种方法是只将键绑定添加到您创建的每个组件。然而,我已经尝试过扩展EventQueue来处理所有Shift+F10按键。特别是,我已重写dispatchEvent(AWTEEvent)以将Shift+F10键事件转换为右键单击鼠标按下:Java Swing Shift+;F10可访问性,java,swing,Java,Swing,根据可访问性要求,Shift+F10应该打开右键单击上下文菜单 在Swing中,一种方法是只将键绑定添加到您创建的每个组件。然而,我已经尝试过扩展EventQueue来处理所有Shift+F10按键。特别是,我已重写dispatchEvent(AWTEEvent)以将Shift+F10键事件转换为右键单击鼠标按下: protected void dispatchEvent(AWTEvent event) { if (event instanceof KeyEvent) {
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。你知道这个解决方案是否可行,或者有更好的方法来满足这个要求吗 我已经知道我可以为每个组件注册键盘操作。但是,我们的应用程序很大。因此,有许多组件已经处理鼠标弹出触发器。这就是为什么我希望通过捕获EventQueue中的所有Shift+F10按键来实现这一点。
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);