Search code examples
javakeyboardremote-accessawtrobot

Java robot keyboard


I am trying to make a application in Java, where you can control someones keyboard from a distance, but I am facing a problem. I thought something like this would be possible:

            //the message is the key input
            String[] parts = message.split("-");
            String KeyPressed = parts[1];//This is the key that's pressed(for example K)
                try {
                    Robot robot = new Robot();
                    robot.keyPress(KeyEvent.VK_(KeyPressed));
                } catch (AWTException e1) {
            }

But this doesn't work. I know this one works, but you'll have to repeat it for every single key on your keyboard:

            //the message is the key input
            String[] parts = message.split("-");
            String KeyPressed = parts[1];
            if(KeyPressed.equals("H")){
                try {
                    Robot robot = new Robot();
                    robot.keyPress(KeyEvent.VK_H);
                } catch (AWTException e1) {
                }
            }

Now my question is, what is the code to do all the keys in a few lines instead of doing the code above for every key on your keyboard?


Solution

  • what you want to do is implement a KeyListener to receive all keys here is the event and some examples: KeyListener