Java:在 JTextPane 上注册 <ENTER> 按键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7439309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Register <ENTER> key press on JTextPane
提问by Mohammad Adib
I'm making an application with java that has a JTextPane. I want to be able to execute some code when the enterkey is pressed (or when the user goes to the next line). I've looked on the web and not found a solution. Would it be better to tackle this with C#? If not, how can i register the Enter key in the JTextPane's keyTyped() event? If C# is a good option, how would i do this in C#?
我正在使用具有 JTextPane 的 java 制作应用程序。我希望能够在enter按下键时(或当用户转到下一行时)执行一些代码。我在网上查看并没有找到解决方案。用 C# 解决这个问题会更好吗?如果没有,我如何在 JTextPane 的 keyTyped() 事件中注册 Enter 键?如果 C# 是一个不错的选择,我将如何在 C# 中执行此操作?
Here is a solution i thought would work...but did not
这是我认为可行的解决方案......但没有
//Event triggered when a key is typed
private void keyTyped(java.awt.event.KeyEvent evt) {
int key = evt.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
Toolkit.getDefaultToolkit().beep();
System.out.println("ENTER pressed");
}
}
Why the above example does not work is because no matter which key i press, i get a keyCode of 0. I would prefer a solution to this problem in Java but C# would work just as well, maybe better. Also, please try to answer the question with examples and not links(unless you really need to). Thanks!
为什么上面的例子不起作用是因为无论我按下哪个键,我得到的 keyCode 都是 0。我更喜欢用 Java 解决这个问题,但 C# 也能正常工作,也许更好。另外,请尝试用示例而不是链接来回答问题(除非您确实需要)。谢谢!
回答by Hovercraft Full Of Eels
One solution is to add a key binding on the textpane. e.g.,
一种解决方案是在文本窗格上添加键绑定。例如,
JTextPane textPane = new JTextPane();
int condition = JComponent.WHEN_FOCUSED;
InputMap iMap = textPane.getInputMap(condition);
ActionMap aMap = textPane.getActionMap();
String enter = "enter";
iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), enter);
aMap.put(enter, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("enter pressed");
}
});
回答by PaRaD0X
This answer is in case anyone ever views this thread I got the same things as Mr. Mohammad Adib. So instead of using (evt.getKeyCode()==evt.VK_ENTER) I use (evt.getKeyChar()=='\n')
这个答案是万一有人看过这个帖子,我得到了和 Mohammad Adib 先生一样的东西。因此,我使用 (evt.getKeyChar()=='\n') 而不是使用 (evt.getKeyCode()==evt.VK_ENTER)
and the solution worked.
回答by Louis Papaloizou
I am looking for ENTER key in the password text field, to launch the login method when ENTER was pressed. The code below will print in the console the keycode. After running the program and typing a few tihngs in the box I discovered for ENTER key it is code 13.
我正在密码文本字段中寻找 ENTER 键,以在按下 ENTER 时启动登录方法。下面的代码将在控制台中打印密钥代码。运行程序并在框中输入一些 tihngs 后,我发现 ENTER 键是代码 13。
txtPass = new Text(shlLogin, SWT.BORDER | SWT.PASSWORD);
txtPass.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.keyCode);
if (e.keyCode == 13) { /* ... Do your stuff ... */ }
}
});
If you are looking for a single key press, you can still be a little lazy and avoid learning new stuff about key bindings, by using this method. The fun begins when adding CTRL+[Letter] shortcuts - but this is for another discussion.
如果您正在寻找单个按键,您仍然可以有点懒惰,并通过使用此方法避免学习有关按键绑定的新知识。当添加 CTRL+[Letter] 快捷方式时,乐趣就开始了 - 但这是另一个讨论。