user6573158
user6573158

Reputation: 1

How to close a dialog box using Java, that pops up during runtime?

I am trying to make a project that performs OCR (image to text conversion). I am using the AspriseOCR jar file and i set up my project accordingly. It works fine.

Whenever i run the project, when the call is made to aspriseOCR API, a dialog box pops up in windows asking if i want to go to their website. I need to automate the whole process. So i dont want this dialog box to appear.

Is there any way by which i can close this dialog box in my code itself, in case it appears? Is there a way i can press the Enter button (it will close accordingly). I am using Java, Eclipse.

Note: This is not on web browsers. So I cannot use Selenium related commands.

Edit:- I tried to use this :-

Robot r = new Robot(); r.keyPress(KeyEvent.VK_ENTER); r.keyRelease(KeyEvent.VK_ENTER);

But it wouldn't solve my problem as I need to add this in the API source code, which I do not have access to. Is there any other approach or method to close the dialog box?

So a part of the code is :-

`{
String s = ocr.recognize( new File[] {new File("C:\\work\\"+file)}, 
Ocr.RECOGNIZE_TYPE_ALL, Ocr.OUTPUT_FORMAT_PLAINTEXT); 
Robot robot; 
robot = new Robot(); 
robot.keyPress(KeyEvent.VK_ENTER); 
robot.keyRelease(KeyEvent.VK_ENTER); 
System.out.println("\n\n pressed enter\n\n"); 
System.out.println("Result:\n\n"+s); 
ocr.stopEngine();
}`

So when the control goes to the function ocr.recognize(...) the dialog box comes up and I do not have access to that source code which is present in that jar file.

Is there any way that I can parallelly run another thread which waits for the opening of this dialog box and closes it ( this thread parallelly running with ocr.recognize). Kindly let me know how to identify if a windows dialog box has been opened using java code.

Upvotes: 0

Views: 624

Answers (1)

Kushal Bhalaik
Kushal Bhalaik

Reputation: 3384

You can use Robot class for this purpose as below:

    //if you want to press alt+f4; to close window or
    Robot robot = new Robot();

    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_F4);
    robot.keyRelease(KeyEvent.VK_ALT);
    robot.keyRelease(KeyEvent.VK_F4);


    // to press enter

    Robot robot = new Robot();

    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

Upvotes: 1

Related Questions