capture backspace in perl script

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm writing a perl program, where i need to accept the
input from the user, i also want to accept the backspace
and delete chars and take action appropriately. here's  
the program i already tried, but i still couldn't capture
the del and backspace keys. please share your ideas.

sub is_backsapce()
    if (($key =~ ['\b']) || ($key == 8)) {
        print "Entered backspace\n";
        return 1;
    else {
        print "Entered non-backspace char\n";
        return 0;

Re: capture backspace in perl script wrote:
Quoted text here. Click to load it

How does the user enter the text? Are you really writing a raw input
device driver?
Virtually all methods I am familiar with will interpret backspace and
delete themself and therefore those 2 characters will never appear in
any text that is sent to whatever application processes the text in the
first place.

Quoted text here. Click to load it

Are you sure this is a good name?


Re: capture backspace in perl script writes:
Quoted text here. Click to load it

If you're on UNIX(*) or anything like that, 'a terminal' (whatever that
specifically mean) will usually operate in so-called 'canoncial mode'
where certain 'special keys' (like backspace in usual configuration) are
interpreted by the kernel terminal driver in order to enable some sort
of line editing. A program which wants to receive them itself needs to
change the terminal mode in a suitable way.

Re: capture backspace in perl script

On 8/11/2014 08:20, wrote:
Quoted text here. Click to load it

sub is_backspace { my $ch = shift; return 1 if ord $ch == 8; return 0; }

I don't know how to get Del to work (I'm on Doze) - nothing is returned
from the read character/getc from the editing keys.

Ctrl-U (21) should cause to to delete your accumulated text and BS would
just chop the last char.

If the editing keys worked, you could keep a ptr into your accumulated
text string and have the edit keys (Home/End/Delete/LArrow/RArrow
move the ptr around in the string for edinting purposes.  Insert would
toggle the edit mode (overwrite/insert).

We have to use Term::Readkey/Readline on Doze - you could check them
out and see if they're any help on whatever you're on.

Doze example:

use Term::ReadKey;    END { ReadMode ('restore'); }    # just in case we bomb

binmode STDIN;        # handle CR->LF action
ReadMode ('raw');    # put term in raw mode

my $str = '';
while (my $ch = ReadKey ()) {
    last if $ch eq 'q' or $ch eq 'Q';    # use q to stop input
    if (ord $ch == 8) { print "Got BS\n"; chop $str; next; }    # BS
    if (ord $ch == 21) { print "Got ^U\n"; $str = ''; next; }    # ^U
    if (ord $ch == 13) { print "Got CR\n"; $str .= "\n"; next; }    # CR->LF
    $str .= $ch;
#    printf "\nch='%s' (0x%X %u)\n", $ch, ord $ch, ord $ch;
ReadMode ('restore');

# print edited string out in hex first
printf "\n%02X", ord $_ foreach split //, $str; printf "\n";
printf "\nstr='$str'\n\n";


Re: capture backspace in perl script

Le 11/08/2014 17:20, a écrit :
Quoted text here. Click to load it

If you are using Windows, you can use Win32Readch, function  
readch_block() like so:

use strict;
use warnings;

use Win32::Readch qw(readch_block);

print "Press a single keystroke:\n";
my $ch = readch_block;

if (ord($ch) == 8) {
     print "Backspace\n";
else {
     print "Character '".ord($ch)."' has been pressed\n\n";

-- Klaus

Ce courrier électronique ne contient aucun virus ou logiciel malveillant parce que la protection avast! Antivirus est active.

Site Timeline