Equivalent function for C "_getch ()" in Java?

I use Google Wave, and I want to emulate the ability to send messages before you actually press the enter key.

Is there a Java equivalent to the C function _getch() ?

+5
java c getch
Dec 08 '09 at 1:42
source share
7 answers

I found code equivalent to the function for C "_getch ()

 public static void getCh() { final JFrame frame = new JFrame(); synchronized (frame) { frame.setUndecorated(true); frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); frame.addKeyListener(new KeyListener() { @Override public void keyPressed(KeyEvent e) { synchronized (frame) { frame.setVisible(false); frame.dispose(); frame.notify(); } } @Override public void keyReleased(KeyEvent e) { } @Override public void keyTyped(KeyEvent e) { } }); frame.setVisible(true); try { frame.wait(); } catch (InterruptedException e1) { } } } 
+4
Apr 12 2018-12-12T00:
source share

There is no getch equivalent in java. You can also create a GUI component and bind a keyEvent listener.

+1
Dec 08 '09 at 2:10
source share

You can use the JLine library method ConsoleReader.readVirtualKey (). See http://jline.sourceforge.net/apidocs/jline/ConsoleReader.html#readVirtualKey () .

If you do not want to use a third-party library, and if you are on Mac OS X or UNIX, you can simply use the same trick that JLine uses to read individual characters: just run the command "stty -icanon min 1" before starting your program and then System.in will no longer loop on the string, and you can get a single character using System.in.read (). Unfortunately, this trick does not work on Windows, so you will need to use your own library to help (or just use JLine).

+1
Dec 10 '09 at 8:56
source share

I originally thought about System.in.read (), but you need to get input without pressing Enter. This requires built-in interaction with the console (and in each system the console is different).

So, the answer is "no, there is no direct analogue."

0
Dec 08 '09 at 2:04
source share

Java custom method for getch () function for C







 import java.awt.event.KeyListener; import java.awt.event.KeyEvent; import javax.swing.*; class getch { public static void getCh() { final JFrame frame = new JFrame(); synchronized(frame) { frame.setUndecorated(true); frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); frame.addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) { synchronized(frame) { frame.setVisible(false); frame.dispose(); frame.notify(); } } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } }); frame.setVisible(true); try { frame.wait(); } catch(InterruptedException e1) { } } } } 
0
02 Oct. '13 at 21:07
source share

This will do the trick, but I only work from the command line. Not from IDE

  Console c =System.console(); Reader r = c.reader(); try { num= r.read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 
0
Nov 15 '17 at 5:10
source share

why don't you just create a Scanner variable that doesn't use it, anywhere.

pause0 = pause1.nextInt ();

: l it seems a lot easier ... Alternatively, you can send a message with the message "Click to continue."

-one
Mar 21 '17 at 2:43 on
source share



All Articles