You need a Listener indeed.
Then you can store the pressed keys in an array, and manage it like this.
bool keyPressed[256]; // Do not forget to initialize all to false if java does not do it; onKeyPressed(event e) { keyPressed[e.keyCode] = true; } onKeyReleased(event e) { keyPressed[e.keyCode] = false; } // in game loop you can then check like this if (keyPressed[keyCode("w")]) moveUp(); Note that all of this is pseudo-code.
EDIT :
The previous code is not really adapted for snake as the snake is supposed to keep moving on even if the key has been released.
Using booleans to keep track of the current direction as stated in OP's own answer is indeed the best solution I can think of.