Java关闭辅助功能,Java Swing Shift + F10辅助功能

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

}

}

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值