加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl Term :: ReadKey with Arrow Keys

发布时间:2020-12-16 06:26:38 所属栏目:大数据 来源:网络整理
导读:我在ReadMode(‘cbreak’)中使用Term :: ReadKey来读取单个字符并根据输入执行操作.这适用于箭头键以外的所有其他键.当按下箭头键时,动作执行3次,我明白这是因为箭头键转换为’^ [[A’等… 如何将箭头键转换为ReadKey可以解释的任意单个值? 我尝试了以下代
我在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' keyn";
    } else {
        print "You hit the special '$special' keyn";
    }

    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

解决方法

Term::RawInput并未涵盖所有内容,但这是此任务的良好开端:

use Term::RawInput;
my ($keystroke,$special) = rawInput("",1);
if ($keystroke ne '') {
    print "You hit the normal '$keystroke' keyn";
} else {
    print "You hit the special '$special' keyn";
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读