如何在不按Enter的情况下从键盘(控制台)读取字符,请帮助我。是否可以在控制台应用程序中使用KeyEvent?
答案 0 :(得分:-3)
看到这个链接,你可能得到了答案:
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");
}
}
}
}
答案 1 :(得分:-5)
<input id="yourID" onkeypress="return isNumberKey(event)">
<script type="text/javascript">
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
else
return true;
}
</script>
信息:您可以在google中获得有关charCode的更多信息。