Java robot enter funktioniert nicht?


16.05.2021, 12:59

Hier der Quelltext:

import java.awt.AWTException;

import java.awt.Robot;

import javax.swing.JOptionPane;

public class DiscordXP {

   static Robot robot;

   public static void main(String[] args) {

      try {

         robot = new Robot();

      } catch (AWTException e) {

         // TODO Auto-generated catch block

         e.printStackTrace();

         return;

      }

      int wiederholung = Integer.parseInt(JOptionPane.showInputDialog("Wiederholung"));

      int sekunden = Integer.parseInt(JOptionPane.showInputDialog("Abstand in Sekunden"));

      sekunden = sekunden * 1000;

      try{

          Thread.sleep(10000);

      }catch(InterruptedException e){

      }

      for (int i = 0; i < wiederholung; i++) {

         pressAndRelease(75);

         pressAndRelease(13);

         try{

             Thread.sleep(sekunden);

         }catch(InterruptedException e){}

      }

   }

   public static void pressAndRelease(int buttons) {

      robot.keyPress(buttons);

      robot.keyRelease(buttons);

   }

}

1 Antwort

Vom Fragesteller als hilfreich ausgezeichnet

Ich kenne nicht alle KeyCodes, aber die Methoden Robot#keyPress und Robot#keyRelease nehmen int-Codes an, die in dem Enum KeyEvent gespeichert sind. Und deshalb verwende jeweils KeyEvent.VK_ENTER bzw. KeyEvent.VK_K als Parameter:

import java.awt.AWTException;


import java.awt.Robot;
import java.awt.event.KeyEvent;


import javax.swing.JOptionPane;


public class DiscordXP {


   static Robot robot;


   public static void main(String[] args) {


      try {
         robot = new Robot();
      } catch (AWTException e) {
         e.printStackTrace();
         return;
      }


      int wiederholung = Integer.parseInt(JOptionPane.showInputDialog("Wiederholung"));
      int sekunden = Integer.parseInt(JOptionPane.showInputDialog("Abstand in Sekunden"));
      sekunden = sekunden * 1000;
      try{
          Thread.sleep(10000);
      } catch (InterruptedException e){


      }


      for (int i = 0; i < wiederholung; i++) {


         pressAndRelease(KeyEvent.VK_K);


         pressAndRelease(KeyEvent.VK_ENTER);


         try{


             Thread.sleep(sekunden);


         }catch(InterruptedException e){}


      }


   }


   public static void pressAndRelease(int buttons) {


      robot.keyPress(buttons);


      robot.keyRelease(buttons);
   }
}

LG Thorax


BeamMichHoch241 
Fragesteller
 16.05.2021, 13:15

Danke hat funktioniert, ich dachte man müsste die KeyCodes nehmen

1