问题描述
我正在尝试绑定 Ctrl + + 和 Ctrl + - 以放大/缩小操作.
这 question 解释了在某些键盘布局中,+ 和 - 存在于主层,而在其他键盘布局中,它们存在于次层(您必须按像 Shift 这样的修饰键才能输入)。另请参阅错误 4262044 和 6942481。
目前看来我必须分析我想要支持的所有布局,并根据当前布局配置不同的按键绑定,例如:
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;
public class KeyStrokeTest extends JFrame {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
// en_US
register(frame,"control shift EQUALS");
register(frame,"control MINUS");
// de_DE
// register(frame,"control PLUS");
// register(frame,"control MINUS");
// de_CH
// register(frame,"control shift 1");
// register(frame,"control MINUS");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.getRootPane().setFocusable(true);
frame.getRootPane().requestFocusInWindow();
}
private static void register(JFrame frame,String keyStrokeDefinition) {
KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeDefinition);
String key = keyStrokeDefinition;
frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke,key);
frame.getRootPane().getActionMap().put(key,new TestAction(keyStrokeDefinition));
}
private static class TestAction extends AbstractAction {
private String info;
public TestAction(String info) {
this.info = info;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Action performed: " + info);
}
}
}
有没有办法以编程方式为所有(或大多数)布局找出 +
和 -
的正确击键?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)