Java:从控制台读取密钥,而不按Enter键 [英] Java: read key from console without press enter key
问题描述
我想在用户按特定键时中断程序。
我尝试下面的代码,但它唯一的工作与输入键。
java.io.InputStreamReader reader = new java.io.InputStreamReader(System.in);
boolean b = false;
while(!b)
{
try {
if(reader.ready())
{
//读取一个字符并处理
System.out.println(key pressed);
b = true;
}
}
catch(java.io.IOException ioex)
{
System.out.println(IO Exception);
}
//编辑,不允许任何cpu时间
try
{
Thread.sleep(50);
System.out.println(nop yet);
}
catch(InterruptedException ex)
{
//不能做很多事情吗?忽略
System.out.println(Interrupted Exception);
}
}
在C#
ConsoleKeyInfo k1 = new ConsoleKeyInfo('T',ConsoleKey.T,false,false,false);
ConsoleKeyInfo k = Console.ReadKey(false);
if(k == k1)
{
Environment.Exit(1);
}
编辑1
我尝试了线程和这个listners,但不行。
public void keyTyped(KeyEvent e)
public void keyPressed(KeyEvent e)
public void keyReleased(KeyEvent e)
您不需要任何库。 Java使用此方法只读取一个按键(无输入键)。
示例:
try {
System.in.read();
} catch(IOException ex){}
在您的代码中,我认为应该是这样:
boolean b = false;
while(!b)
{
try {
String key = System.in.read();
//读取字符并处理
System.out.println(key pressed);
b = true;
} catch(java.io.IOException ioex){
System.out.println(IO Exception);
}
//编辑,不允许任何cpu时间
try {
Thread.sleep(50);
System.out.println(nop yet);
} catch(InterruptedException ex){
//不能做很多关于它我们可以吗?忽略
System.out.println(Interrupted Exception);希望它会帮助你:)
编辑1:
也许使用InputStreamReader和StringBuffer会修复错误。请检查此代码:
StringBuffer str = new StringBuffer();
char c;
try {
Reader entrada = new InputStreamReader(System.in);
while((c =(char)entrada.read())!='\\\
'){
str.append(c);
}
} catch(IOException ex){}
你不需要while子句,因为你只需要一个键。
I want to interrupt the program when user press especific key.
I try the code bellow, but its only work with enter key.
java.io.InputStreamReader reader = new java.io.InputStreamReader(System.in);
boolean b = false;
while(!b)
{
try{
if ( reader.ready())
{
// 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");
}
}
Equivalent in C# (working):
ConsoleKeyInfo k1 = new ConsoleKeyInfo('T', ConsoleKey.T, false, false, false);
ConsoleKeyInfo k = Console.ReadKey(false);
if (k== k1)
{
Environment.Exit(1);
}
Edit 1:
I tried with threads and this listners, but not works too.
public void keyTyped(KeyEvent e)
public void keyPressed(KeyEvent e)
public void keyReleased(KeyEvent e)
解决方案 You don't need any library. Java uses this method to read only one key pressed (without enter key).
Example:
try {
System.in.read();
} catch (IOException ex) { }
The line has to be in the try-catch block in order to do it.
In your code I think it should be like this:
boolean b = false;
while(!b)
{
try {
String 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");
}
}
Hope it will help you :)
Edit 1:
Maybe using InputStreamReader and StringBuffer will fix the error. Check this out this code:
StringBuffer str = new StringBuffer();
char c;
try {
Reader entrada = new InputStreamReader(System.in);
while ((c=(char)entrada.read())!='\n'){
str.append(c);
}
} catch(IOException ex){}
In your case, you don't need the while clause, because you want only one key.
这篇关于Java:从控制台读取密钥,而不按Enter键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!