-3

So I tried to look a bit in forums and StackOverflow but nothing worked for me I need when enter is pressed to stop my code this is my code `

JFrame f;

    JTextField I;

    // JButton
    JToggleButton b;
 
    // label to display text
    JLabel l;

    f = new JFrame("AutoClicker");

    i = new JTextField("100");

    // create a label to display text
    l = new JLabel("clicks/seconds");

    // create a new buttons
    b = new JToggleButton("Start");

    // create a panel to add buttons
    JPanel p = new JPanel();

    // add buttons and textfield to panel
    p.add(b);
    p.add(i);
    p.add(l);


    // setbackground of panel
    p.setBackground(Color.red);

    // add panel to frame
    f.add(p);

    // set the size of frame
    f.setSize(280, 80);

    f.setVisible(true);

    b.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int jml = Integer.parseInt(i.getText());
            if(jml < 50)
            {
                jml = 50;
            }
            AutoClicker(jml);
        }
    });
}

static void AutoClicker(int jml)
{
    while(true)
    {
        try{
            Robot r = new Robot();
            int button = InputEvent.BUTTON1_DOWN_MASK;
            r.mousePress(button);
            Thread.sleep(jml);
            r.mouseRelease(button);
            Thread.sleep(jml);
        }
        catch(Exception e){
            e.printStackTrace();
            System.out.println("not good");
        }
    }

}

}`

I tried to add a KeyListener but it did not work. I don't understand why it doesn't work so if you can just help me know why it doesn't work it would be much apreciated.

1 Answers1

-1

KeyListener isn't going to solve the problem of the fact that you are simply not handling the potential of Integer.parseInt to throw an exception - I mean, how can it convert "" or "This is not a number" to an int. In those cases it throws an exception

The JavaDocs clearly state

Throws:
NumberFormatException - if the string does not contain a parsable integer.

Go back and have a look at the original error you were getting from your previous question on this exact topic

java.lang.NumberFormatException: For input string: "Enter"

It's telling you exactly what the problem is - the text "Enter" can not be converted to an int value.

YOU have to handle this possibility. No offence, but this is honestly basic Java 101. See Catching and Handling Exceptions

Another option which "might" help is to use a formatted text field

You also don't seem to have a firm grip on the concept of what a "event driven environment" is or what the potential risk of what doing something like while (true) will do if executed within the context of the Event Dispatching Thread. This makes me think you've got yourself in over all head.

You're going to want to learn about Concurrency in Swing as AutoClicker should never be called within the context of the Event Dispatching Thread, which is going to lead you another area of complexity, concurrency and all the joys that brings.

Updated

Wow, you changed the title. Maybe a better description of the problem you're trying to solve would have a gotten a better answer. In short, you can't, not natively in Java anyway. The only way you can detect keyboard input out side of the app is using native integration, JNA/JNI. Plenty of examples about, for example

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366