javamemoryinputdialoguser-input

Automatic insertion of a value in the program


I need to periodically record a value in the program using a dialog box. The program is written in Borland Delphi 7, Windows API. The recorded value is stored in RAM.

I have a Java program that generates these values.

How do I modify the Java program so that the dialog box is called automatically and the desired value is copied there (At least tell me roughly in which direction to move)?

P.S. i want to inject a value into a different program's dialog box.


Solution

  • Well, here is my two cents. Let's assume you're using AWT/Swing to build the UI. If you want to simulate user input into a dialog box from an external Java program, you can use the Java Robot class to imitate key presses and mouse actions.

    Here's a basic example:

    
    import java.awt.Robot;
    import java.awt.event.KeyEvent;
    
    public class AutoInput {
    
        public static void main(String[] args) throws Exception {
            Robot robot = new Robot();
    
            // Optional: wait for the dialog to appear
            Thread.sleep(2000);
    
            // Type the number "42"
            robot.keyPress(KeyEvent.VK_4);
            robot.keyRelease(KeyEvent.VK_4);
    
            robot.keyPress(KeyEvent.VK_2);
            robot.keyRelease(KeyEvent.VK_2);
    
            // Press ENTER to confirm
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
        }
    }
    

    If you want to change your code and automatically show a dialog and auto-fill a value and auto-confirm it (simulate the "OK" button press), use something like:

    import javax.swing.*;
    import java.awt.event.*;
    
    public class AutoSubmitDialog {
        public static void main(String[] args) {
            JTextField field = new JTextField("42");
    
            JDialog dialog = new JDialog();
            dialog.setTitle("Set Parameter");
    
            JButton okButton = new JButton("OK");
            okButton.addActionListener(e -> {
                String value = field.getText();
                System.out.println("Submitted: " + value);
                dialog.dispose();
            });
    
            JPanel panel = new JPanel();
            panel.add(new JLabel("Enter value:"));
            panel.add(field);
            panel.add(okButton);
    
            dialog.add(panel);
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
    
            // Automatically click OK after 2 seconds
            Timer timer = new Timer(2000, e -> okButton.doClick());
            timer.setRepeats(false);
            timer.start();
        }
    }
    

    This opens a dialog with a pre-filled value ("42") and simulates clicking "OK" after 2 seconds using Timer.

    Maybe I misunderstood your exact goal, but I hope this points you in the right direction as requested :)

    There is a caveat though that Java - being platform agnostic - has to rely on the platform here. There may be restrictions in place that may limit the robot's functionality. Read more about this in the documentation.

    Further reading: