如何为顶级特殊键定义键盘快捷键,例如德语变音键Ä?我找到了一种映射用于默认美国布局键的unicode字母的方法,请参阅here。但德国变音键Ä的关键事件是:
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='ä',keyLocation=KEY_LOCATION_STANDARD,rawCode=222,primaryLevelUnicode=228,scancode=40] on frame0
想法是注册键盘动作:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class KeyStrokeForGermanUmlaut {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(600, 400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
final JLabel label = new JLabel("Text shall change with shortcut");
panel.add(label);
panel.registerKeyboardAction(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent event) {
label.setText("It is working!!!");
}
}, KeyStroke.getKeyStroke("control typed Ä"), JComponent.WHEN_IN_FOCUSED_WINDOW);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
});
}
}
答案 0 :(得分:4)
你可以召唤JLabel,没有任何关键事件发生
应该是将焦点移动到JFrames ContentPane的起点(可以用作JPanel,但与普通JPanel相比具有BorderLayout - FlowLayout)
-
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class KeyStrokeForGermanUmlaut {
private JFrame frame = new JFrame();
private JLabel label = new JLabel("Text shall change with shortcut");
public KeyStrokeForGermanUmlaut() {
frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK), "CTRL + A");
frame.getRootPane().getActionMap().put("CTRL + A", updateCol());
frame.setPreferredSize(new Dimension(600, 100));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
private Action updateCol() {
return new AbstractAction("Hello World") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
label.setText(label.getText() + " presses");
}
};
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new KeyStrokeForGermanUmlaut();
}
});
}
}
EDIT
请参阅API getKeyStroke/ v.s. getKeyStrokeForEvent
那么结果可能是(在这两个字符中使用修饰符SHIFT和大写形式(ä和ú)时会有一点点丢失,也许有人会帮助我们使用那些KeyEvents)
这
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class KeyStrokeForGermanUmlaut {
private JFrame frame = new JFrame();
private JLabel label = new JLabel("Text shall change with shortcut");
public KeyStrokeForGermanUmlaut() {
frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke("typed ä"), "typed ä");
frame.getRootPane().getActionMap().put("typed ä", updateCol());
frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke("typed ú"), "typed ú");
frame.getRootPane().getActionMap().put("typed ú", updateCol1());
frame.setPreferredSize(new Dimension(600, 100));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
private Action updateCol() {
return new AbstractAction("Hello World") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
label.setText(label.getText() + " presses - ä");
}
};
}
private Action updateCol1() {
return new AbstractAction("Hello World") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
label.setText(label.getText() + " presses - ú");
}
};
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new KeyStrokeForGermanUmlaut();
}
});
}
}
答案 1 :(得分:2)
我担心在处理CTRL的修饰符时会出现一些问题。 即:在检查接收到的键修饰符= InputEvent.CTRL_MASK时,扩展修饰符= InputEvent.CTRL_DOWN_MASK。 API的javadoc有点可疑。
除此之外,当省略“控制”时,Ä不是特殊情况。
为了使它工作,我不得不添加一个脏 hack:注册一个关键监听器,它调用动作本身。我必须监督一些事情。
对于其他人,我按预期使用了InputMap / ActionMap。输入映射似乎不起作用,但根据我的理解,如果添加到JTextField或其他答案(对于Ä
),它不起作用。以下作品 - 以一种可怕的方式。
final JLabel label = new JLabel("Text shall change with shortcut");
final KeyStroke key = KeyStroke.getKeyStroke((Character)'k',
InputEvent.CTRL_DOWN_MASK, false);
final Object actionKey = "auml";
final Action action = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent event) {
System.out.println("aha");
label.setText("It is working!!!");
}
};
label.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(java.awt.event.KeyEvent e) {
if (e.isControlDown() && e.getKeyChar() == 'ä') {
System.out.println("Ctrl-ä");
label.getActionMap().get(actionKey).actionPerformed(null);
// return;
}
super.keyPressed(e);
}
});
label.getInputMap().put(key, actionKey);
label.getActionMap().put(actionKey, action);
答案 2 :(得分:-2)
Ä, \xC4, Ä, Ä, %C4, %C3%84
Ä
有代码。请尝试使用它,也许一个它们将为你工作。