我对Swing的使用并不陌生,但我对它做了大量的工作,并且正在设计一个新的应用程序。这是一个绘图应用程序。它将允许用户在空白的“查看器”窗口中的特定位置点击,使用键盘输入字母和符号,从而编辑查看器窗口某处的文本。使用KeyBindings将所有字符映射到动作
因为我没有使用JComponent
来显示这个文本,所以我需要一个可靠的方式让我的应用程序接受输入。我选择了使用KeyBindings。但我的特殊查看器组件以空输入映射和空动作映射开始。
我的问题。什么是最简单的方式来映射所有的字母和符号,我可以使用我的键盘键入AbstractAction
的使用ActionMap和InputMap?该动作图需要使用WHEN_IN_FOCUSED_WINDOW
来捕捉接口的所有输入。
编辑: 我曾经使用的KeyListener为了做到这一点,但我没有在获得模块化的 方式,我想
我看不上的代码。彻底抛弃它。我相信这是在SSCCE:
import java.awt.event.KeyEvent;
import static java.awt.event.KeyEvent.*;
public class MyKeysHandler extends KeyListener {
//blah blah blah
//blah blah blah
public void keyPressed(KeyEvent ke)
{
// please excuse if the boolean logic of my use of masks is off
// their proper use doesn't come to me easily
// I hope you can get the jist
if((ke.getModifiersEx() & KeyEvent.SHIFT_DOWN_MASK) != 0) {
switch(ke.getKeyCode()) {
// handle capital letters
case VK_DELETE : editor.handleThisSpecialKey(ke.getKeyCode);
case VK_BACK_SLASH : // handle back slash
case VK_7 : // handle the ampersand
case VK_8 : // handle the asterisk character
default : // if just a normal letter...
editor.handleThisNormalKeyPlease(KeyEvent.getKeyText(ke.getKeyCode()).toUpperCase());
}
}
else {
switch(ke.getKeyCode()) {
case VK_DELETE : // handle the shift-delete command
case VK_BACK_SLASH : // handle the question mark
case VK_7 : // handle the 7
case VK_8 : // handle the 8
defualt :
if(ke.getKeyCode() == VK_C && ke.getModifiersEX() & KeyEvent.CTRL_DOWN_MASK) != 0) {
editor.handleTheCopyCommandPlease();
}
else
editor.handleThisKeyPlease(KeyEvent.getKeyText(ke.getKeyCode).toLowerCase());
}
}
}
}
但这真的很麻烦。每当你添加一个密钥时,你必须确保它不会与相同方法中的一些其他密钥代码发生冲突,并且对于你必须为任何应用程序制作的每一个keylistener,你都必须包含这个细致的“VK”开关代码。
我的应用程序也会有菜单,而且我也想为那些有时间的人安装新的键盘绑定或加速器(或任何他们称之为助记符?)。为了移动文本并删除它们,只需要一些特殊的组合键就可以了。至于你在上面看到的关键组合?你可以想象这对我来说有多大的噩梦。
我向你保证没有发布更多的代码,我试图遵循良好的可重用计算机编程实践模型。我有一个模型在下面运行,实际上处理编辑,以及上面运行并处理菜单按钮代码的视图。
不幸的是,我希望Java不必像上面显示的那样麻烦。键盘上的所有键除了大多数键都需要不同的响应,具体取决于您所按的键,并且KeyBindings中支持ctrl和shift之类的组合。
我的希望是有一个循环解决方案。然后,也许有一个解决方案使用WHEN-IN-ANCESTOR
,也可以工作。我正在使用的输入映射是组件根窗格,但我也打算尽可能远地查看组件本身(一个JPanel)并获得其实例,但我还没有这样做,因为我'实验。
我目前访问项目的方式是这样的:这适用于我。但它不处理符号。也许如果我增加了for循环中的字符跨度?或者手动添加几个?我不知道。
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
public void initKeyBindingsTheEasyWay() {
JRootPane rootPane = mainPane.getRootPane();
InputMap theWIMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap theAMap = rootPane.getActionMap();
for(char c = 'A'; c <= 'Z'; c++) {
// lower case
KeyStroke little = KeyStroke.getKeyStroke("pressed " + c);
theWIMap.put(little, ""+Character.toLowerCase(c));
theAMap.put(""+Character.toLowerCase(c), new LetterAction(Character.toLowerCase(c)));
}
}
public static class LetterAction extends AbstractAction
{
char theLetter;
public LetterAction(char letter)
{
theLetter = letter;
}
public void actionPerformed(ActionEvent ae)
{
System.out.print(theLetter);
}
}
这实际上并不是真正的关键绑定。如果你必须有一个gloabl级别的关键侦听器,你可以考虑使用AWTListener而不是 – MadProgrammer
@MadProgrammer看看我上面的编辑。 – JonAar
愚蠢的问题,你为什么要监视这么多键? – MadProgrammer