input - Java: read key from console without press enter key -
i want interrupt program when user press especific key. try code bellow, work enter key.
java.io.inputstreamreader reader = new java.io.inputstreamreader(system.in); boolean b = false; while(!b) { try{ if ( reader.ready()) { // read character , process system.out.println("key pressed"); b = true; } } catch (java.io.ioexception ioex) { system.out.println("io exception"); } // edit, lets not hog cpu time try { thread.sleep(50); system.out.println("nop yet"); } catch (interruptedexception ex) { // can't can we? ignoring system.out.println("interrupted exception"); } }
equivalent in c# (working):
consolekeyinfo k1 = new consolekeyinfo('t', consolekey.t, false, false, false); consolekeyinfo k = console.readkey(false); if (k== k1) { environment.exit(1); }
edit 1:
i tried threads , listners, not works too.
public void keytyped(keyevent e) public void keypressed(keyevent e) public void keyreleased(keyevent e)
Comments
Post a Comment