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
原文地址:https://www.cnblogs.com/borter/p/9575412.html