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
main
method is static, therefore, you cannot access non-static variables. If you don't wantstatic
on yourKey
object, then do the following:1) Make a constructor for your
ClickBot
class2) Initialize keyObject in it
2) In your
main
method create an instance ofClickBot
3) Make a new method in your
ClickBot
class (public void methodName()
)4) Put all your further code in it
5) Call
clickBotObject.methodName()
in yourmain
method.Otherwise, if
static
on yourKey
object is alright with you, then just addstatic
on it.Or, make it a local variable in your
main
method if that's alright with you.