سؤال

How can I basically get the opposite of a key event?

IE a simple program will print out "Whatever" until any key is pressed, then stop.

After the key is released, it will start printing out again?

public class Printer
{
   public static void main(String[] args)
   {
     "WHILE ANY KEYBOARD KEY NOT PRESSED" PRINT SOMETHING
     "WHEN PRESSED" STOP PRINTING SOMETHING
     " WHEN RELEASED" START PRINTING AGAIN
   }
}
هل كانت مفيدة؟

المحلول

I think you can print in the main thread while checking on a boolean (lets say "isKeyPressed" which will be initially false) in while loop whenever a key pressed in the key listener you will set the boolean flag to true which will prevent the main thread from printing and when key released is fired, you wil set the boolean flag to false again to resume Printing

in main method

while(!isKeyPressed){
your logic / printing
}

in your keypressed listener

 public void keyPressed(KeyEvent e) {
    isKeyPressed = true;
}

in your keyReleased listener

 public void keyReleased(KeyEvent e) {
    isKeyPressed = false;
}

Hope that will help you.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top