【发布时间】:2012-10-11 14:57:31
【问题描述】:
我有一个代码,它从button 中获取一个值,然后输出一些东西。
如果我不使用break;我按下left button,它会输出数千个。 enter 和 right 也是如此。
我不是 Java 专家,几周前我才开始使用 Java 编程。
我希望我的代码永远不会stop readingbutton value,但我不希望我的代码在button is pushed 时输出数千个左右或输入。我怎样才能做到这一点?此代码有效,但在我 push one button 之后停止。如果我向左按下按钮,它将向左输出一次,然后停止运行。没有休息;它将输出数千个。
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 {}
}
【问题讨论】:
-
好吧,我看不出你最外层的任何原因,如果构造..
-
我真的不明白你想要什么..
-
不能在代码中添加 eventHandling 吗?按下哪个按钮时做什么?