我想在用户按特定键时中断程序.我尝试下面的代码,但它只能使用回车键.
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");
}
}
Run Code Online (Sandbox Code Playgroud)
相当于C#(工作):
ConsoleKeyInfo k1 = new ConsoleKeyInfo('T', ConsoleKey.T, false, false, false);
ConsoleKeyInfo …Run Code Online (Sandbox Code Playgroud) 我试图运行我的代码直到Esc被按下.因此我ReadKey在我的控制台中使用
var input = Console.ReadKey();
do
{
} while (input.Key != ConsoleKey.Escape);
Run Code Online (Sandbox Code Playgroud)
但是在"ConsoleKey"它说,在'bool'中不可能使用ConsoleKey.我该如何解决这个问题?或者我应该使用什么呢?
我在ReadMode('cbreak')中使用Term :: ReadKey来读取单个字符并根据输入执行操作.这适用于箭头键以外的所有其他键.当按下箭头键时,动作执行3次,我明白这是因为箭头键转换为'^ [[A'等...
如何将箭头键转换为ReadKey可以解释的任意单个值?
我尝试了以下代码,但它不起作用:
use Term::ReadKey;
ReadMode('cbreak');
my $keystroke = '';
while ($keystroke ne 'h') {
print "Enter key: ";
#Read user keystroke
$keystroke = ReadKey(0);
chomp($keystroke);
if(ord($keystroke) == 27) {
$keystroke = ('0');
}
}
Run Code Online (Sandbox Code Playgroud)
这是基于建议的代码:
use Term::RawInput;
use strict;
use warnings;
my $keystroke = '';
my $special = '';
while(lc($keystroke) ne 'i' && lc($keystroke) ne 't'){
my $promptp = "Enter key: ";
($keystroke,$special) = rawInput($promptp, 1);
if ($keystroke ne '') {
print "You hit the normal …Run Code Online (Sandbox Code Playgroud) 可能的重复:
如何向 Console.ReadLine() 添加超时?
如果我有一个 Console.ReadKey(),它会使整个程序卡住,我怎样才能让它读取密钥 1 秒,如果未读取密钥,则会设置其他内容。