Java 中 C 的“_getch()”的等效函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1864076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:22:51  来源:igfitidea点击:

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

javacgetch

提问by Pandemic21

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

我使用 Google Wave,我想模拟在您实际按下 Enter 键之前发送消息的能力。

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

是否有相当于 C 函数的 Java _getch()

回答by Vladimir Dyuzhev

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

最初我想到了 System.in.read(),但是您需要在不按 Enter 的情况下获取输入。这需要本地控制台交互(每个系统下的控制台都不同)。

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

所以答案是“不,没有直接的模拟”。

回答by tensaix2j

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

java中没有getch等价物。您不妨创建一个 GUI 组件并绑定 keyEvent 侦听器。

回答by marcprux

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

您可以使用 JLine 库的 ConsoleReader.readVirtualKey() 方法。请参阅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).

如果您不想使用第 3 方库,并且您使用的是 Mac OS X 或 UNIX,则可以利用 JLine 用来读取单个字符的相同技巧:只需执行命令“stty -icanon min 1" 在运行您的程序之前,然后 System.in 将不再被行缓冲,您可以使用 System.in.read() 获取单个字符。不幸的是,此技巧在 Windows 上不起作用,因此您需要使用本机库来提供帮助(或仅使用 JLine)。

回答by Nagaraju Badaeni

I found a code, Equivalent function to C's “_getch()

我找到了一个代码,等效于 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) {  
            }  
        }  
    }

回答by Souradeep Panda

Custom-made method in Java for getch() function of C

Java中为C的getch()函数定制的方法





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)
            {  
            }  
        }  
    }
}

回答by Jose Pablo

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

为什么不创建一个不使用它的变量 Scanner,该程序,在任何地方。

pause0 = pause1.nextInt();

pause0 = pause1.nextInt();

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

:l 看起来容易多了...另外你可以写一条消息说“按继续。”;

回答by harsha kumar Reddy

This will Do the trick but i works only from command line . Not from IDE

这可以解决问题,但我只能从命令行工作。不是来自 IDE

 Console c =System.console();
Reader r = c.reader();
try {
    num=    r.read();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}