I have some problem with my code. I want to implement a keylistener. I have a keyHandler class which takes care about keyinput and a while loop in the main class to check if a certain key is pressed or not. I dont understand the behavior of my code. the strange thing is that every thing works when I put the System.out.println("hello") command in front of my if statement. but when i comment it out my programm doesnt realize that i press the key Im checkin in my if statement. I think i could find a workaround. but i would be very glad to understand this strange behavior. why is this happening. Sorry for my bad english. I hope you guys can help me.
public static void main(String[] args) {
    boolean running = true;
    JFrame window;
    KeyHandler k = new KeyHandler();
    
    window = new JFrame();
    window.setVisible(true);
    window.addKeyListener(k);
    
    while (running) {
        //System.out.println("hello");
        if (k.isKeyPressed(KeyEvent.VK_W)) {
            System.out.println("--------------------------------------------------------------------------");
        }
    }
}
//here is the KeyHandler class
public class KeyHandler implements KeyListener {
private boolean[] keysPressed = new boolean[128];
@Override
public void keyTyped(KeyEvent e) {
    
}
@Override
public void keyPressed(KeyEvent e) {
    keysPressed[e.getKeyCode()] = true;
    System.out.println(e.getKeyChar());
    System.out.println(keysPressed[e.getKeyCode()]);
    
}
@Override
public void keyReleased(KeyEvent e) {
    keysPressed[e.getKeyCode()] = false;
    System.out.println(e.getKeyChar());
    System.out.println(keysPressed[e.getKeyCode()]);
}
public boolean isKeyPressed(int keyCode) {
    return keysPressed[keyCode];
}
}
 
     
     
    