[英]Perl Term::ReadKey with Arrow Keys
我在 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');
}
}
這是我基於建議的代碼:
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 '$keystroke' key\n";
} else {
print "You hit the special '$special' key\n";
}
chomp($keystroke);
$keystroke = lc($keystroke);
}
if($keystroke eq 'i') {
#Do something
}
if($keystroke eq 't') {
#Do something
}
現在,無論我按什么,我都無法退出這個循環
這是輸出:
Enter key:
Enter key:
Enter key: You hit the normal 't' key
#Proceeds to function
這是我的工作解決方案......
use Term::ReadKey;
ReadMode('cbreak');
{
#Temporarily turn off warnings so no messages appear for uninitialized $keystroke
#that for some reason appears for the if statement
no warnings;
my $keystroke = '';
while ($keystroke ne 'h') {
print "\nEnter key: ";
#Read user keystroke
$keystroke = ReadKey(0);
#The first character for the arrow keys (ex. '^[[A') evaluates to 27 so I check for
#that
if(ord($keystroke) == 27) {
#Flush the rest of the characters from input buffer
#This produces an 'Use of uninitialized value...' error
#for the other two characters, hence 'no warnings' at the beginning.
#This will ignore the other 2 characters and only cause a single iteration
while( defined ReadKey(-1) ) {}
}
ReadMode 0;
}
}
Term::RawInput
並未涵蓋所有內容,但對於此任務來說,這是一個不錯的開始:
use Term::RawInput;
my ($keystroke,$special) = rawInput("", 1);
if ($keystroke ne '') {
print "You hit the normal '$keystroke' key\n";
} else {
print "You hit the special '$special' key\n";
}
如果您想閱讀“按鍵”的高級語義思想,而不是“來自終端的字節”的低級思想,您將需要一些可以為您解析和收集這些多字節序列的東西。
對於此類任務,我編寫了Term::TermKey :
use Term::TermKey;
my $tk = Term::TermKey->new( \*STDIN );
print "Press any key\n";
$tk->waitkey( my $key );
print "You pressed: " . $tk->format_key( $key, 0 );
基於mob
answer,使用Term::RawInput ,我做了這個腳本來模擬更復雜的輸入交互。 最重要的區別是使用 'rawInput': mob
rawInput("",1)
: rawInput("",1)
,我發現實際上使用rawInput("> ")
(沒有第二個參數)使事情更容易使用,並且有提示“>”更有用。
此代碼接受命令和特殊鍵。 此外,它可以很好地用作系統的交互式 shell。
DELETE
鍵將刪除所有字符並BACKSPACE
單個字符。 ESC
將退出外殼。 您可以添加更多鍵以包含箭頭或其他任何執行特殊功能的鍵。 此代碼將打印出任何未包含在if..elsif
特殊按鍵(因此您知道需要添加什么)。
use warnings;
use strict;
use Term::RawInput;
sub out {
my $out = shift;
print "[ $out ]\n";
}
do {
my ($keystroke,$special) = rawInput("> ");
if($special eq 'ESC') {
print "\n";
exit;
} elsif($special eq 'ENTER') {
out($keystroke);
} elsif($special ne 'DELETE') {
if ($keystroke ne '') {
out($keystroke);
} else {
print "'$special' key is not associated\n";
}
}
} while(1);
您可以在“out”內執行您的命令;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.