Use of KeyListener to change string background and set it back when the key is release | Type Tutor program

后端 未结 2 1449
情歌与酒
情歌与酒 2021-01-29 03:56

I have been stock for a couple of hours now lol, not fun but this is why.. I have an app for typing which is to set the background of the button keyboard to a different colour l

2条回答
  •  甜味超标
    2021-01-29 04:15

    The main problem is, focus. The component that the KeyListener is registered to must be focusable and have focus before it can be notified of key events.

    Normally, I would suggest using the Key Bindings API, but that would require you to register a binding for each and every key you want to monitor, including it's shift state (almost doubling the key bindings).

    An alternative approach would be register a AWTEventListener with the Event Queue directly.

    This is very low level stuff, but basically means you will receive notification of just about every AWT based event that passes through the system. You can filter this to only include key events, so that makes your work slightly easier...

    Below is a VERY simple example. Know, you're going to receive two (basic) things. The virtual key code, which is used to make different key boards and platforms to a common known key value and the key character.

    Key characters are modified by the state of the shift key. So you're going to have to devise a method for looking up the key code/character and finding the button associated with it...

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
    
        @Override
        public void eventDispatched(AWTEvent event) {
            if (event instanceof KeyEvent) {
                KeyEvent ke = (KeyEvent) event;
                switch (ke.getID()) {
                    case KeyEvent.KEY_PRESSED:
                        // set the various state variables, like color for this state
                        break;
                    case KeyEvent.KEY_RELEASED:
                        // set the various state variables, like color for this state
                        break;
                }
                switch (ke.getKeyCode()) {
                    case KeyEvent.VK_SHIFT:
                        if (ke.getKeyLocation() == KeyEvent.KEY_LOCATION_LEFT) {
                            // left shift
                        } else if (ke.getKeyLocation() == KeyEvent.KEY_LOCATION_RIGHT) {
                            // right shift
                        }
                        break;
                    case KeyEvent.VK_CAPS_LOCK:
                        break;
                    case KeyEvent.VK_DELETE:
                        break;
                    case KeyEvent.VK_ENTER:
                        break;
                    case KeyEvent.VK_UP:
                        break;
                    case KeyEvent.VK_DOWN:
                        break;
                    case KeyEvent.VK_LEFT:
                        break;
                    case KeyEvent.VK_RIGHT:
                        break;
                    default:
                        // You could use the ke.getKeyChar() here, but remember,
                        // the caps-lock and shift will change the char returned
                        // the key code is safer, but that's a lot of work ;)
                        break;
                }
            }
        }
    }, AWTEvent.KEY_EVENT_MASK);
    

提交回复
热议问题