My eclipse tells me that i need to use an static modifier but when i do so the hole thing becomes wrong. Here is my code I hope that you can help me and tell me what i did messed up(i got the hint for the inner class from StealthyHunter7):
public class ClickBot
{
private class Key
implements KeyListener
{
private boolean spacebarPressed = false;
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_SPACE)
{
spacebarPressed = true;
}
}
@Override
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_SPACE)
{
spacebarPressed = false;
}
}
public boolean isSpacebarPressed()
{
return spacebarPressed;
}
}
Key keyObject = new Key();
public static void main(String[] args) throws IOException, AWTException
{
JFrame.addKeyListener(keyObject);
final Robot robot = new Robot();
robot.delay(2000);
while(keyObject.spacebarPressed())
{
{
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(30);
}
}
}
}
Your
mainmethod is static, therefore, you cannot access non-static variables. If you don't wantstaticon yourKeyobject, then do the following:1) Make a constructor for your
ClickBotclass2) Initialize keyObject in it
2) In your
mainmethod create an instance ofClickBot3) Make a new method in your
ClickBotclass (public void methodName())4) Put all your further code in it
5) Call
clickBotObject.methodName()in yourmainmethod.Otherwise, if
staticon yourKeyobject is alright with you, then just addstaticon it.Or, make it a local variable in your
mainmethod if that's alright with you.