1

I want to press Enter Key when a new window pop up for example for downloading a file. I have to press OK button.

So I tried the following code:

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

This is just ignoring. Do you have any idea. Thank you

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
user3610075
  • 73
  • 2
  • 4
  • 8
  • 3
    http://stackoverflow.com/questions/6630194/java-ignores-vk-enter-event-generated-by-robot – Anubian Noob May 22 '14 at 15:48
  • Do you want to simulate an 'Enter' Key press, or do you want to do something when the user physically presses 'Enter'? – Sam I am says Reinstate Monica May 22 '14 at 15:49
  • I want to simulate an "Enter" Key press. – user3610075 May 22 '14 at 15:54
  • https://stackoverflow.com/questions/3079524/how-do-i-manually-invoke-an-action-in-swing – Acewin Aug 15 '17 at 19:57
  • For which purpose? Depending on that the solution may vary. Do you want to send an `Enter`-key event to the OS such that the application which has currently focus can use it, for example to create a new line in the text editor that currently has focus? Or do you want to press some buttons in your **Java swing application** or stuff like that? Is the key-event supposed to leave your application (getting send to the OS)? – Zabuzard Aug 15 '17 at 19:57
  • I think anubian already answered in his comment. this seems to be a copied/duplicate question. And yes question is also incomplete. – Acewin Aug 15 '17 at 19:59
  • If you want to use Robot, you first need to (somehow) set the focus on the OK button before you actually press Enter. Didn't use robot for a while so I don't know how of the top of my head – Bentaye Nov 13 '19 at 09:17

1 Answers1

-1

This code simulates pressing the Enter Key:

    Scanner keyboard = new Scanner(System.in);
    keyboard.nextLine();
  • Depending on the application of OP this answer is not necessarily correct. Your code sends a *new-line* command to the channel connected to the Java application via `System.in`. That could be a **console** like Windows *cmd* or nobody in case of **javaw**. It does not emulate pressing the *Enter*-key on the keyboard. For example to create a new line in a text editor that currently has focus by the OS. – Zabuzard Aug 15 '17 at 19:53