如何以编程方式确定不同键盘布局上“Ctrl + Plus”和“Ctrl + Minus”的正确击键?

问题描述

我正在尝试绑定 Ctrl + +Ctrl + - 以放大/缩小操作.

question 解释了在某些键盘布局中,+- 存在于主层,而在其他键盘布局中,它们存在于次层(您必须按像 Shift 这样的修饰键才能输入)。另请参阅错误 42620446942481

目前看来我必须分析我想要支持的所有布局,并根据当前布局配置不同的按键绑定,例如:

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

有没有办法以编程方式为所有(或大多数)布局找出 +- 的正确击键?

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)