e645. 处理键盘事件

You can get the key that was pressed either as a key character (which is a Unicode character) or as a key code (a special value representing a particular key on the keyboard).

    
component.addKeyListener(new MyKeyListener());
    
    public class MyKeyListener extends KeyAdapter {
        public void keyPressed(KeyEvent evt) {
            // Check for key characters.
            if (evt.getKeyChar() == 'a') {
                process(evt.getKeyChar());
            }
    
            // Check for key codes.
            if (evt.getKeyCode() == KeyEvent.VK_HOME) {
                process(evt.getKeyCode());
            }
        }
    }

 

Related Examples
posted @ 2018-09-02 21:28  borter  阅读(92)  评论(0编辑  收藏  举报