Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!spool.mu.edu!uwm.edu!linac!att!pacbell.com!pacbell!unet!imram.net.com From: jukoff@imram.net.com (Steve Jukoff) Newsgroups: comp.lang.perl Subject: Perl keyboard processing Message-ID: <1357@unet.UUCP> Date: 17 May 91 01:08:52 GMT Article-I.D.: unet.1357 Sender: news@unet.UUCP Organization: NET Lines: 57 This perl script is simply a loop to read and process keyboard input. Some tricky problems arise: 1) I'd like a "read" routine to immediately process each character; I don't want the user to be required to hit . 2) I need to process all characters including: 1) Control chars, and 2) Special keyboard characters, i.e. UP-Arrow, HOME, etc. 3) I need "immediate flush mode" for the user prompt. (Something to do with select() but what I tried didn't work). Here is a first try at the code. # user_input is a "switch" to process keyboard input sub user_input { open (TTY, "/dev/tty") || die "Can't open /dev/tty" ; # Perl idiom to force a per-write flush on FILEHANDLE -- Doesn't # seem to work. select((select(TTY), $| = 1)[$[]) ; # Needed: immediately process single character input # Set "non-canonical mode": do not (pre-)interpret control chars # system "stty raw"; # works but now I need to know how to match control chars. # And I need to catch signals in order to reset the terminal. for (;;) { print "Enter choice: " ; # Needed: print *unbuffered* $char = getc(TTY) ; # A Perl switch construct # Needed: capability to match control chars # and special keyboard characters, i.e. DOWN-ARROW, END, etc. $char =~ /\D+/ && do { print "non-digit entered\n" ; next ; } ; $char =~ /\d+/ && do { print "digit entered\n" ; next ; } # (?) $char =~ /#Control-X#/ && do { print "Control-X entered\n" ; next ; } # (?) $char =~ /#UP-ARROW#/ && do { print "Up-arrow entered\n" ; next ; } } system "stty -raw" ; } I know this code is a rough draft. Any help would be appreciated. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Steven Jukoff DDN: jukoff@net.com Phone: 415-780-5819 uucp: ...!ames!unet!jukoff -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-