есть ли mouseClicked или keyPressed в perl? - PullRequest
0 голосов
/ 27 мая 2020

Я помню, когда я кодировал Java, там была команда, которая воспринимала щелчок мышью или нажатие клавиши. Могли бы вы как-нибудь сделать то же самое, но в perl? Примерно так

print "\n", "Press the 'I' key for instructions", "\n"; 
my $a = readline STDIN;

chomp $a;

#This next line is what is what I'm thinking of
     if (keyPressed eq "I" || "i")
     {
     print "\n", "Instructions: blah, blah, blah", "\n";
     }
print "\n", "Click your mouse if you want to exit the instructions", "\n";
     while (mouseClicked = True)
     {
     print "\n", "Press the 'I' key for instructions", "\n"; 
          if (keyPressed eq "I" || "i")
          {
          print "\n", "Instructions: blah, blah, blah", "\n";
          }
     print "\n", "Click your mouse if you want to exit the instructions", "\n";
     }     

Примерно так. Главный вопрос: есть ли mouseClicked или keyPressed (Java) в perl?

Ответы [ 2 ]

1 голос
/ 27 мая 2020

Взгляните на Term :: RawInput .
Вот некоторые основные c примеры:

use Term::RawInput;

my $prompt='PROMPT : ';
my ($input,$key)=('','');
($input,$key)=rawInput($prompt,0);

print "\nRawInput=$input" if $input;
print "\nKey=$key\n" if $key;

print "Captured F1\n" if $key eq 'F1';
print "Captured ESCAPE\n" if $key eq 'ESC';
print "Captured DELETE\n" if $key eq 'DELETE';
print "Captured PAGEDOWN\n" if $key eq 'PAGEDOWN';
0 голосов
/ 27 мая 2020

Отслеживание мыши и нажатие клавиш - это особенность среды GUI. Вы должны выбрать, что вы будете использовать для программирования GUI.

Для нажатия клавиш клавиатуры вам потребуется использовать Term :: ReadKey или Term :: RawInput .

Если вам нужно какое-то взаимодействие с пользователем, а не просто нажатие клавиш, тогда сценарий perl может иметь следующую форму

use strict;
use warnings;
use feature 'say';

my $input = '';

say '
    Please type one of: help, instructions, quit
';

do {

    help()          if $input eq 'help';
    instructions()  if $input eq 'instructions';

    print "Input > ";
    $input = <STDIN>;
    chomp $input;
    $input = lc $input;

} while( $input ne 'quit' );

say '
Thank you for dropping by, hope to see you soon again.

Good by ...
';

sub help {
    say '
    This is an example of help for user of this program.

    Please provide some information for purpose of the program
    and instructions how to use it....
    ';
}

sub instructions {
    say '
    This is an example of instructions assistance page.

    Please give a description of available instructions to user
    of this software....
    ';
}
...