I have a code that takes a value from a button and then outputs something.
If I don’t use a break; and I press on the left button, it will output thousands of left. The same for enter and right.
I am no Java guru and I started just few weeks ago with Java programming.
I want my code never to stop reading the button value, but I don’t want my code to output thousands of left, right or enter when a button is pushed. How can I do this? This code is working but stops after I push one button. If I push button left, it will output left once, and then stops running. Without the break; it will output thousands of left.
for (int i = 0; ; i++) {
// Get the data from analog input 5
int sensorValue1 = phidget.getSensorValue(1);
int sensorValue2 = phidget.getSensorValue(2);
int sensorValue3 = phidget.getSensorValue(3);
if (sensorValue1 > 100 || sensorValue2 > 100 || sensorValue3 > 100){
// printing value
//System.out.println("sensorValue1 = " + sensorValue1 + ", sensorValue2 = " + sensorValue2 + ", sensorValue3 = " + sensorValue3 + ", Count = " + i);
if (sensorValue1 > 100){
System.out.println("RIGHT");
// simulates RIGHT key
try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_RIGHT);
} catch (AWTException e) {
e.printStackTrace();
}
break;
} else if (sensorValue2 > 100)
{
System.out.println("LEFT");
// simulates LEFT key
try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_LEFT);
} catch (AWTException e) {
e.printStackTrace();
}
break;
} else if (sensorValue3 > 100)
{
System.out.println("ENTER");
// simulates ENTER key
try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
} catch (AWTException e) {
e.printStackTrace();
}
break;
}
} else {}
}
You can track the last handled value of sensorValue and a click is considered to have occured when the new value of sensorValue1 is over 100 while the old value is under 100.