Skip navigation.

mg12's Blog

paint my world ...

Java 中监听组合键

,

毕业设计中...
现在想模仿 Gajim 发送文本区的处理方法.
就是当发送文本区内有内容的时候, 只要键入 Enter 就能发送信息, 而 Ctrl+Enter 则是换行.
但是 Java 似乎不提供监听组合键的方法, 或许是我没找到.
所以用 "java ctrl enter" 到 Google 搜了一下, 问的人多, 答的人少.
没找着, 算了, 还得自己想. 想了个笨方法, 但还能用...

......
entryArea.addKeyListener( new EntryKeyListener() );
......

class EntryKeyListener implements KeyListener {
public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_CONTROL) {  
            ctrlIsPressed = true;  
            }
}

public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_CONTROL) {  
            ctrlIsPressed = false;  
            }
}

public void keyTyped(KeyEvent e) {
            if (e.getKeyChar() == KeyEvent.VK_ENTER) {
            if(ctrlIsPressed) { entryArea.append("\n"); }
            else { sendHandler(); }
            }
}
}

Happy Labor Days火箭难破第一轮魔咒

Comments

~sisie 4. May 2007, 09:26

高深哦...

火星兔 4. May 2007, 09:28

.。。代码白痴飘过

XieWei 4. May 2007, 14:43

javascript 应该支持~~

mg12 4. May 2007, 15:24

本来我在 Blogger 还开了个 Blog, 想写写学习心得, 却好些天上不了, 郁闷死了...

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies