0

There are a lot of "key pressing in Java" questions already here so I did read some of them (Actually got a piece of the code from one). non answer the problem I've been having. I found this code that detects if a key is pressed and also if it's released but I can't get rid of this error

In this case I am testing if the 'W' is pressed

code:

private static boolean IsPressing(String string) {
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {

        @Override
        public boolean dispatchKeyEvent(KeyEvent ke) {
            synchronized (IsKeyPressed.class) {
                boolean wPressed;
                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;
                }
            }
        });
        return false;
    }
}

So basicly in the sixth line (synchronized (IsKeyPressed.class) {) it tels mee that "IsKeyPressed.class cannot be resolved to a type", then I get four eclipse options:

1.Create Class 2.Create Interface 3.Create Enum 4.Fix project

The first three I don't think that help me achieve my goal of detecting if a key is pressed, and the last one doesn't do anything

What am I doing wrong?

BRHSM
  • 854
  • 3
  • 13
  • 48
  • what are you trying to accomplish with `synchronized( IsKeyPressed.class )`? – user2570465 Jan 15 '15 at 16:06
  • Do you have the jars in your build path? – brso05 Jan 15 '15 at 16:06
  • You need to create the class I think I found the example you took the code from you will see he has the class created at the top...http://stackoverflow.com/a/18037609/4028085 – brso05 Jan 15 '15 at 16:09
  • Do you not understand the error? Eclipse cannot find the `IsKeyPressed` class. Eclipse is then saying "Hey! This doesn't exist, could you please make a class/interface/enum yourself so I know what the crap this is? Or perhaps give me a reference to this class you claim exists you crazy user" – chancea Jan 15 '15 at 16:10
  • This code snippet was probably contained within a class called `IsKeyPressed` originally. You can rename this to the class you currently have it in and it should work. However I wonder if you've thought out your approach on this - Is something supposed to happen 'w' is pressed / released? Or is something else independently checking whether 'w' is pressed during some other event? – ControlAltDel Jan 15 '15 at 16:10
  • thanks boys for clearing it up. It works now!! – BRHSM Jan 15 '15 at 16:13
  • @ControlAltDel Something has to happen when the key is pressed. Now I can check if the key is pressed then it returns the variable and I can call this function right? (Basically if w is pressed continue with the program) – BRHSM Jan 15 '15 at 16:37

1 Answers1

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

**********************************************You need this*********************************************

public class IsKeyPressed {
    private static boolean wPressed = false;
    public static boolean isWPressed() {
        synchronized (IsKeyPressed.class) {
            return wPressed;
        }
    }
    public static void main(String[] args) {
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {

        @Override
        public boolean dispatchKeyEvent(KeyEvent ke) {
            synchronized (Test.class) {
                switch (ke.getID()) {
                case KeyEvent.KEY_PRESSED:
                    if (ke.getKeyCode() == KeyEvent.VK_W) {
                        wPressed = true;
                        System.out.println("test");
                    }
                    break;

                case KeyEvent.KEY_RELEASED:
                    if (ke.getKeyCode() == KeyEvent.VK_W) {
                        wPressed = false;
                        System.out.println("test1");
                    }
                    break;
                }
                return false;
            }
        }
    });
   JFrame test = new JFrame();
   test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   test.setVisible(true);
  }
}

Then you can always use:

//////////////This code goes in method//////////////////////
if (IsKeyPressed.isWPressed()) {
    // do your thing.
}
/////////////////////////////////////////////////////////////////////

This code was taken from here

Community
  • 1
  • 1
brso05
  • 13,142
  • 2
  • 21
  • 40
  • Just a quick question: the `if (IsKeyPressed.isWPressed()) {` Can be used in all methodes or just the main – BRHSM Jan 16 '15 at 20:26
  • @CoderGuy you can use it in all methods because `isWPressed()` is a static method of class `IsKeyPressed` so in order to access it you just need to do `IsKeyPressed.isWPressed()`. – brso05 Jan 16 '15 at 20:32
  • BTW you mised one of these '}' in the IsKeyPressed class – BRHSM Jan 16 '15 at 20:33
  • I got the code from the above mentioned link but I think it is correct I just copied and pasted in eclipse and it seems to be right... – brso05 Jan 16 '15 at 20:36
  • Haha Might ask there than because it does not work with my new project? – BRHSM Jan 16 '15 at 20:39
  • @CoderGuy The if statement at the end shouldn't be there it needs to be in a method... – brso05 Jan 16 '15 at 20:40
  • i also added == true just for clearing things up in my head but that should not cause any errors right? – BRHSM Jan 16 '15 at 20:46
  • also should i put this in a timer loop using time sleep and an infinite while loop? – BRHSM Jan 16 '15 at 20:47
  • @CoderGuy I changed the code go ahead and copy/paste the new code. If you are using eclipse when you click w look at your console also when you release. If this helps you please mark this as correct again. – brso05 Jan 16 '15 at 21:47