r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
It doesn't work for some reason. When I click ENTER manually, it does work. How to fix this?
Are there certain dialogs when it's required to press it manually? Because a few dialogs before this issue occurs, clicking ENTER with the robot works fine. So, seems to be a special dialog?!
Are there "special dialogs"? If, how to automatically ENTER click then on these?
On Windows 7 and 10.
EDIT:
The whole code down below.
Robot r = new Robot();
r.keyPress(KeyEvent.VK_SHIFT);
r.keyPress(KeyEvent.VK_F10);
r.keyRelease(KeyEvent.VK_F10);
r.keyRelease(KeyEvent.VK_SHIFT);
r.keyPress(KeyEvent.VK_DOWN);
r.keyRelease(KeyEvent.VK_DOWN);
r.keyPress(KeyEvent.VK_DOWN);
r.keyRelease(KeyEvent.VK_DOWN);
r.keyPress(KeyEvent.VK_DOWN);
r.keyRelease(KeyEvent.VK_DOWN);
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
// ABOVE ENTER WORKS FINE! THEN A NEW WINDOW IS OPENED
// HERE IT STOPS WORKING!
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
r.keyPress(KeyEvent.VK_DOWN);
r.keyRelease(KeyEvent.VK_DOWN);
Where I put the command it stops working.
Right before that command, there's a new window opened. Now, maybe the next ENTERs aren't recognized anymore 'cause there's no focus?
But, I don't think so. Because when I click ENTER manually (without clicking anything else or moving the mouse), it does work. So apparently the focus is there.
From the Java Tutorial: "For a key press to affect a component, thecomponent must have the keyboard focus ".
Set the focus on the component you wish to detect keyPress events like this:
component.requestFocusInWindow();
In your case, you may want to detect the window opening event and then set focus.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.