8

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

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

approxiblue
  • 6,982
  • 16
  • 51
  • 59
Pandemic21
  • 81
  • 2
  • 2
  • 4

7 Answers7

6

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

If you don't want to use a 3rd party library, and if you are on Mac OS X or UNIX, you can just take advantage of the same trick that JLine uses to be able to read individual characters: just execute the command "stty -icanon min 1" before running your program, and then System.in will no longer be line buffered and you can get an individual character using System.in.read(). Unfortunately, this trick doesn't work on Windows, so you would need to use a native library to help (or just use JLine).

marcprux
  • 9,845
  • 3
  • 55
  • 72
2

I found a code, Equivalent function to C's “_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) {  
            }  
        }  
    }
Nagaraju Badaeni
  • 890
  • 8
  • 14
  • Correct me if I'm wrong, but shouldn't `keyPressed`, `keyReleased`, and `keyTyped` have `@Override` in front of them? I don't think it'll work otherwise. Apart from that, excellent answer. +1 – aggregate1166877 Sep 11 '12 at 19:08
  • 5
    @Override is just an annotation, method overriding still works just fine without it. All it does it tell the compiler that the method *should* be overriding some other method. – lynks Nov 08 '12 at 16:43
  • 4
    How is this function equivalent to C's _getch() if it returns void? Wouldn't it need to return a character? Since it has override notation, do we need to write code that implements it in order to use it? – Joe Sep 15 '15 at 22:14
  • Could you drop the useless `synchronized`/`wait()` that clutter the code? A simple `System.out.println("Key pressed")` in the proper callback would be a lot better. – Matthieu Apr 05 '21 at 08:25
0

Initially I thought of System.in.read(), but you need to get input without pressing Enter. That requires native console interaction (and console is different under every system).

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

Vladimir Dyuzhev
  • 18,130
  • 10
  • 48
  • 62
0

There's no getch equivalent in java. You might as well create a GUI component and bind the keyEvent Listener.

tensaix2j
  • 462
  • 6
  • 18
0

Custom-made method in Java for getch() function of 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)
            {  
            }  
        }  
    }
}
  • This is a copy-paster of [another answer](https://stackoverflow.com/a/10118990/1098603) that is not even good, and more than a year later. – Matthieu Apr 05 '21 at 08:29
0

This will Do the trick but i works only from 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();
}
harsha kumar Reddy
  • 1,251
  • 1
  • 20
  • 32
-3

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

pause0 = pause1.nextInt();

:l it seems a lot more easy... Plus you can put a message saying "Press to continue.";