0

Ok this is what i have now.

import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;

public class IsKeyPressed {
private static boolean wPressed = false;
public static boolean isWPressed() {
    synchronized (IsKeyPressed.class) {
        return wPressed;
    }
}

public static void main(String[] args) {
for (int i=1; i<2;){
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new                 KeyEventDispatcher(){

        @Override
        public boolean dispatchKeyEvent(KeyEvent ke) {
            synchronized (IsKeyPressed.class) {

                switch (ke.getID()) {
                case KeyEvent.KEY_PRESSED:
                    if (ke.getKeyCode() == KeyEvent.VK_W) {
                        wPressed = true;
                    }
                    break;

                case KeyEvent.KEY_RELEASED:
                    if (ke.getKeyCode() == KeyEvent.VK_W) {
                        wPressed = false;
                    }
                    break;
                }
                return false;
            }
            }   
    });
            if (IsKeyPressed.isWPressed()) {
        i=i+1;
    }
}
}

} }

What do i need to add or change to make this work.I have tried a while function.I dont know if puting a for statment would change it but i tried.

  • 1
    So what exactly is the problem? – coder hacker May 15 '14 at 03:30
  • 3
    You can't use Scanner class to do that. Probably you might want to read [this](http://stackoverflow.com/questions/18037576/how-do-i-check-if-the-user-is-pressing-a-key) – Sky May 15 '14 at 03:38
  • I tryed to use this but it just ends before i can enter anythaing. – Derrick Bush May 15 '14 at 12:50
  • It ends because it's a short example. Doesn't your program do something else than waiting for user inputs? If no add something that while last forever to prevent it from exiting. (well not forever, you can detect that the user pressed Q to exit for exmaple). – StephaneM May 22 '14 at 12:42
  • It will not update the input. I have tested my code and changed the false to true at the start then back and found out the input is not detected after the first loop. – Derrick Bush May 27 '14 at 12:44

0 Answers0