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

avatar
高深哦...

By sisie, # 4. May 2007, 09:26:59

avatar
.。。代码白痴飘过

By seabean, # 4. May 2007, 09:28:56

avatar
javascript 应该支持~~

By xwxw, # 4. May 2007, 14:43:34

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

By mg12, # 4. May 2007, 15:24:05

Write a comment

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

Please type this security code : e40ed4

Smilies