Equivalent function to C's “_getch()” in Java?

2020-01-25 10:06发布

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()?

标签: java c getch
7条回答
倾城 Initia
2楼-- · 2020-01-25 10:45

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) {  
            }  
        }  
    }
查看更多
Lonely孤独者°
3楼-- · 2020-01-25 10:46

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

查看更多
我只想做你的唯一
4楼-- · 2020-01-25 10:54

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".

查看更多
放我归山
5楼-- · 2020-01-25 10:54

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.";

查看更多
女痞
6楼-- · 2020-01-25 11:05

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();
}
查看更多
对你真心纯属浪费
7楼-- · 2020-01-25 11:07

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)
            {  
            }  
        }  
    }
}
查看更多
登录 后发表回答