Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8677951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:35:36+00:00 2026-06-12T20:35:36+00:00

I have a code that takes a value from a button and then outputs

  • 0

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 {}

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:35:37+00:00Added an answer on June 12, 2026 at 8:35 pm

    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.

    int oldSensorValue1 = 0;
    int oldSensorValue2 = 0;
    int oldSensorValue3 = 0;
    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 && oldSensorValue1 < 100){
    
                System.out.println("RIGHT");
    
                // simulates RIGHT key
                try { 
                Robot robot = new Robot(); 
                robot.keyPress(KeyEvent.VK_RIGHT); 
                } catch (AWTException e) { 
                e.printStackTrace(); 
                }
    
            } else if (sensorValue2 > 100 && oldSensorValue2 < 100)
            {
                System.out.println("LEFT");
    
                // simulates LEFT key
                try { 
                Robot robot = new Robot(); 
                robot.keyPress(KeyEvent.VK_LEFT); 
                } catch (AWTException e) { 
                e.printStackTrace(); 
                }
            } else if (sensorValue3 > 100 && oldSensorValue3 < 100)
            {
                System.out.println("ENTER");
    
                // simulates RIGHT key
                try { 
                Robot robot = new Robot(); 
                robot.keyPress(KeyEvent.VK_RIGHT); 
                } catch (AWTException e) { 
                e.printStackTrace(); 
                }
            }
            oldSensorValue1 = sensorValue1;
            oldSensorValue2 = sensorValue2;
            oldSensorValue3 = sensorValue3;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that takes a double value and converts it to
I have a piece of code that takes several rows from a database and
I have a code at the moment that takes a line from a .txt
I have the following piece of code from a function that takes the host
I have the following piece of code that takes in some words, stores them
I have this small piece of code that basically takes a list and runs
Does anyone have some code that will take a TimeZoneInfo field from .NET and
I have this jquery post that takes text from a textarea and inputs it
I need to configure a button with a code that takes the first two
I have code in my Page_load method that can take up to a few

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.