Java robot cannot control the whole keyboard

2019-09-01 17:42发布

问题:

I tried to make a sort of keyboard controller class but the java robot seems to be unable to conrol the whole keyboard.

I tried

robot.keyPress(i);
Thread.currentThread().sleep(50);
robot.keyRelease(i);

from 0 to 255 and this key is never pressed (this key is present on all azerty keyboard).

Any idea why? Thanks.

Ps : you don't need to press CTRL or ALT in order to use this key, it produce this : "²"

回答1:

You could try creating a new window, and have it capture and print out the key codes for the keys pressed. Then run it and press the key in question. it should then print the KeyCode for it:

public static void main(String[] args) {
    JFrame frame= new JFrame();
    frame.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            System.out.println(e.getExtendedKeyCode());
        }            
    });
    frame.setBounds(0, 0, 100, 50);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

doing this with a virtual keyboard gave me 16777394

see http://docs.oracle.com/javase/7/docs/api/constant-values.html#java.awt.event.KeyEvent.CHAR_UNDEFINED for all the 'KeyCode' values