简体   繁体   English

Java:注册<ENTER> JTextPane 上的按键

[英]Java: Register <ENTER> key press on JTextPane

I'm making an application with java that has a JTextPane.我正在使用具有 JTextPane 的 java 制作应用程序。 I want to be able to execute some code when the enter key is pressed (or when the user goes to the next line).我希望能够在按下Enter键时(或当用户转到下一行时)执行一些代码。 I've looked on the web and not found a solution.我在网上查看并没有找到解决方案。 Would it be better to tackle this with C#?用 C# 解决这个问题会更好吗? If not, how can i register the Enter key in the JTextPane's keyTyped() event?如果没有,我如何在 JTextPane 的 keyTyped() 事件中注册 Enter 键? If C# is a good option, how would i do this in C#?如果 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.为什么上面的例子不起作用是因为无论我按下哪个键,我得到的 keyCode 都是 0。我更喜欢用 Java 解决这个问题,但 C# 也能正常工作,也许更好。 Also, please try to answer the question with examples and not links(unless you really need to).另外,请尝试用示例而不是链接来回答问题(除非您确实需要)。 Thanks!谢谢!

One solution is to add a key binding on the textpane.一种解决方案是在文本窗格上添加键绑定。 eg,例如,

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

This answer is in case anyone ever views this thread I got the same things as Mr. Mohammad Adib.这个答案是万一有人看过这个帖子,我得到了和 Mohammad Adib 先生一样的东西。

So instead of using ...所以,而不是使用...

(evt.getKeyCode()==evt.VK_ENTER)

I used ...我用了 ...

(evt.getKeyChar()=='\n')

and the solution worked.并且解决方案有效。

I am looking for ENTER key in the password text field, to launch the login method when ENTER was pressed.我正在密码文本字段中寻找 ENTER 键,以在按下 ENTER 时启动登录方法。 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.在运行程序并在框中输入一些 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] 快捷方式时,乐趣就开始了 - 但这是另一个讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM