my $key = ReadKey(-1); next unless defined $key; #next or return depending on your code structure if ($key eq "\e") { #expecting more $key .= ReadKey(-1); # should be a '[' $key .= ReadKey(-1); } my $callback= $keymap{$key}; if (defined $callback) { $callback->($key); } else { beep(); } #no such key is meaningful here