Reputation: 193
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class sample
{
public static void main(String args[]) throws AWTException {
Robot robot = new Robot();
WindowElement StopTime = handler.findElementByAutomationID(Timer, "2049");
handler.click(StopTime);
handler.setfocus(StopTime);
int StopTimeMinute = Minute + 8 ;
String val = "";
val = String.valueOf(StopTimeMinute);
sendkeys(val);
}
public static void sendkeys(String text)
{
try {
Robot robot = new Robot();
String val = text.toUpperCase();
for(int i=0;i<val.length();i++) {
robot.keyPress(Character.getNumericValue(val.charAt(i)));
}
} catch(java.awt.AWTException exc) {
System.out.println("error");
}
}
}
In the above code, I am getting source not found error if I try to send variable to robot key event method. => 89. Java robot is failing to press key. Can anyone tell me how I can pass variable to Robot.KeyPress(code)?
And whats wrong in the below code? Robot.KeyPress(VK_SHIFT) is working fine but Robot.KeyPress(code) is throwing the below Error.
WRobotPeer.keypress int(line) : not available [native method]
Source not found.
I even tried sending integer as an argument still same issue.
public static void StopMinute(int StopMinute) throws AWTException{
Robot robot = new Robot();
robot.delay(20);
robot.keyPress(StopMinute);
robot.keyRelease(StopMinute);
}
Can anyone suggest me with this. Robot.KeyPress(Code)
Upvotes: 0
Views: 769
Reputation: 1763
The method getNumericValue() on Character returns a Unicode code point for that character. Character.getNumericValue('A')
returns 10 for instance, whereas KeyEvent.VK_A
returns the ascii value of 65. The latter value is used for the AWT Robot, not the first.
Instead of itering over val.length()
, do val.toCharArray()
instead, and iterate over that. And then pass (int)charArray[i]
to robot keyPress.
Upvotes: 1