How do I generate a keyPress? (maybe Robot)

edited May 2016 in How To...

Hello guys

I planned to create a special program only for me and just 4 fun Since a few weeks I had some ideas but today I really started aand then my first problem appeared So no I'm here and really hope you guys could help me

The title says a lot about my little problem I would like to generate an automatically keyPress with my arrows (UP, DOWN, LEFT and RIGHT) So i searched for an answer and then i founded these links about the Robot class:

http://wiki.processing.org/w/Robot_class http://www.elitepvpers.com/forum/coding-tutorials/358215-java-die-robot-class.html

But the first one only describe something with the mouse, and the second only in java

It would be great and I would really appreciate it when you could help me

Thanks for all

David

Tagged:

Answers

  • hey man of dreams and smileys. i do not totally understand your problem. sure that you need to generate auto keypress for what you want to achieve. what do you want to do? you could just call the code that should be executed when a key is pressed.

  • removed (no smiliey)

    I don't want to call a code that should be executed when a key is pressed. I would like to have a program which generates exactly command which would be called when i would press a key. Then my program replaces necessity to press this key manually.

  • edited April 2014

    There's Robot.keyPress() and Robot.keyRelease(). Say you wanted to press space every time that the mouse is pressed:

    import java.awt.AWTException;
    import java.awt.Robot;
    import java.awt.event.KeyEvent;
    
    Robot robot;
    
    void setup() {
      size(400, 400);
    
      //Let's get a Robot...
      try { 
        robot = new Robot();
      } catch (AWTException e) {
        e.printStackTrace();
        exit();
      }
    }
    
    void draw() {}
    
    void mousePressed() {
      //Press the space key
      robot.keyPress(KeyEvent.VK_SPACE);
      //If we want a delay here, that gets a little bit more complicated...
      robot.keyRelease(KeyEvent.VK_SPACE);
    }
    
    void keyPressed() {
      //Detect space key presses (to show that it works)
      if(key == ' ') {
        println("Space!");
      }
    }
    

    However, as @mschi has pointed out, it isn't very practical to emulate a key press and then detect it - it would make more sense to call the key press code directly. There may be useful applications of this API for key presses... but keep in mind that this may not be everything that you think it is. AFAIK, you cannot send key events to another application in this way - you can only send key events to the same place that you sent them from (this spoiled my dreams long ago, as well...).

  • my kind of humour does not translate well to text as it seems :) sorry for making you delete the smileys

  • @mschi no problem - when i saw it again i also thiught i would be too much ;)

    @calsign thanks a lot for this complete answer!

    So when it wont be possible to get the key event from one application to the other ... I think the only way i could solve my problem is that i get the other application into processing Does somebody know how i can run a website in processing? :) Or what other kind of applications do i can "import" in processing?

  • Answer ✓

    On second thought, it looks like Robot might be capable of a little bit more than I thought... it is possible to inject events into another application. I have made the example more Processing-friendly:

    import java.awt.Robot;
    import java.awt.event.KeyEvent;
    import java.io.IOException;
    
    int keyInput[] = {
      KeyEvent.VK_J, KeyEvent.VK_A, KeyEvent.VK_V, KeyEvent.VK_A, KeyEvent.VK_SPACE, 
      KeyEvent.VK_P, KeyEvent.VK_R, KeyEvent.VK_O, KeyEvent.VK_G, KeyEvent.VK_R, 
      KeyEvent.VK_A, KeyEvent.VK_M, KeyEvent.VK_M, KeyEvent.VK_I, KeyEvent.VK_N, 
      KeyEvent.VK_G, KeyEvent.VK_SPACE, KeyEvent.VK_F, KeyEvent.VK_O, KeyEvent.VK_R, 
      KeyEvent.VK_U, KeyEvent.VK_M, KeyEvent.VK_S, KeyEvent.VK_SPACE, KeyEvent.VK_PERIOD, 
      KeyEvent.VK_C, KeyEvent.VK_O, KeyEvent.VK_M
    };
    
    void setup() {
      try {
        //Launch NotePad
        Runtime.getRuntime().exec("notepad");
        //Get a Robot
        Robot robot = new Robot();
    
        for (int i = 0; i < keyInput.length; i++) {
          robot.keyPress(keyInput[i]);
          robot.delay(50);
          robot.keyRelease(keyInput[i]);
          robot.delay(50);
        }
      } catch (Exception e) {
        //Uh-oh...
        e.printStackTrace();
        exit();
      }
    }
    

    However, there are two potential issues:

    • You have to specify all of the key codes manually. This is kind of a pain... and unfortunately, there's no easy way to convert a String into an array of KeyEvents short of a really big switch statement.
    • There doesn't seem to be an easy way of automatically focusing the launched window. While the application is launched, the focus seems to stay on the PDE (or whatever else you had open before). You may have to resort to writing a small script in another language to satisfy this need.

    P.S.: If you're on a Mac, then you might need to change the example...

  • edited April 2014

    Thanks a lot - the result is a really amazing little program :)

    I changed it a little bit so now it runs on every computer with internet:P

    import java.awt.Robot;
    import java.awt.event.KeyEvent;
    import java.io.IOException;
    
      int keyInput[] = {
        KeyEvent.VK_H, KeyEvent.VK_E, KeyEvent.VK_L, KeyEvent.VK_L, KeyEvent.VK_O, 
        KeyEvent.VK_SPACE, KeyEvent.VK_A, KeyEvent.VK_N, KeyEvent.VK_D, KeyEvent.VK_SPACE, 
        KeyEvent.VK_T, KeyEvent.VK_H, KeyEvent.VK_A, KeyEvent.VK_N, KeyEvent.VK_K, KeyEvent.VK_S, 
        KeyEvent.VK_SPACE, KeyEvent.VK_A, KeyEvent.VK_SPACE, KeyEvent.VK_L, KeyEvent.VK_O, 
        KeyEvent.VK_T, KeyEvent.VK_SPACE, KeyEvent.VK_C, KeyEvent.VK_A, KeyEvent.VK_L, 
        KeyEvent.VK_S, KeyEvent.VK_I, KeyEvent.VK_G, KeyEvent.VK_N, KeyEvent.VK_SPACE, 
        KeyEvent.VK_ENTER
      };
    
    
      void setup() {
        try {
          open("google.com");
          Robot robot = new Robot();
          robot.delay(1000);
    
          for (int i = 0; i < keyInput.length; i++) {
            robot.delay(25);
            robot.keyPress(keyInput[i]);
            robot.delay(25);
            robot.keyRelease(keyInput[i]);
            robot.delay(25);
          }
        }
        catch (Exception e) {
          e.printStackTrace();
          exit();
        }
      } 
    

    Thanks again for all !

  • So I see the basic concept for KeyEvent.VK_, but I dont know how to find the codes for special keys, such as fn. Does anyone know where I could find a list of those?

Sign In or Register to comment.