gpt4 book ai didi

java - 从键盘(控制台)读取字符,而无需按Enter键

转载 作者:搜寻专家 更新时间:2023-10-31 08:04:47 24 4
gpt4 key购买 nike

这个问题已经有了答案:
How to read a single char from the console in Java (as the user types it)?
5个答案
如何在不按回车键的情况下从键盘(控制台)读取字符,请帮助我。是否可以在控制台应用程序中使用keyevent?

最佳答案

查看此链接可能会得到您的答案:
http://code.runnable.com/UqaE-KocrFtRAAKL/reading-characters-from-console-in-java
或者试试这个

import java.io.*;
public class ConsoleReadingDemo {
public static void main(String[] args) {
java.io.InputStreamReader reader = new java.io.InputStreamReader(System.in);
boolean b = false;
while(!b)
{
try
{
int key = System.in.read();
// read a character and process it
System.out.println("key pressed");
b = true;
} catch (java.io.IOException ioex) {
System.out.println("IO Exception");
}
// edit, lets not hog any cpu time
try {
Thread.sleep(50);
System.out.println("nop yet");
} catch (InterruptedException ex) {
// can't do much about it can we? Ignoring
System.out.println("Interrupted Exception");
}
}
}
}

关于java - 从键盘(控制台)读取字符,而无需按Enter键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35936430/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com