Ace128 has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks,

I'm kinda stuck getting the special keys (PgUp, PgDn, Left Arrow. F1 etc...) to work in Windows. Even if I follow links found on the internet.
use Term::ReadKey; use Time::HiRes qw( usleep ); use strict; use warnings; ReadMode(4); my $old_ioctl = ioctl(STDIN,0,0) || -1; # Gets device info printf "System returned %d\n", $old_ioctl; $old_ioctl &= 0xff; ioctl(STDIN,1,$old_ioctl | 32); # Writes it back, setting bit 5 $| = 1; while(1){ my $char; while (!defined ($char = ReadKey(-1))){ usleep 1_100; } my $ord = ord($char); last if ($ord == 3); last if ($ord == 4); last if ($ord == 27); if ($ord == 0) { #my $c; #sysread(STDIN,$c,1); #print "C: $c\n"; } print $ord . "\n"; } $| = 0;
According to: http://www.perl.com/doc/manual/html/pod/perlfaq5.html#How_can_I_read_a_single_characte it should be rather correct, but Ive found stuff about some needed ioctl.ph http://www.perl.com/doc/manual/html/pod/perlfunc/ioctl.html aswell... In either case, I'm lost and I was hoping someone could provide me with something that actually works! I don't mind if usage of Win32::API or simulair is used. Running the provided code and pressing a special key gives $ord = 0. So far so good I suppose. But the rest doesn't work. Actually, not even the ioctl hack mentioned first doesn't work. (Returns -1)

So, how to make those special keys work propely (so I know which one I pressed) ?

Thanks,
Ace

Replies are listed 'Best First'.
Re: Getting special keys to work with Term::ReadKey in Windows
by gellyfish (Monsignor) on Jul 21, 2006 at 09:34 UTC

    Term::ReadKey doesn't return the special keys on Windows at the current time, this is because the underlying API returns 0 for those keys and the windows API expects you to look up the actual key elsewhere. I have a nearly working patch for this that will return the ANSI character sequences for the keys which should be available with the next release, but I'm not making any promises as to when this will be released. You might want to consider using instead Win32::Console which gives you the "virtual key code" for the special keys.

    /J\

      Hey, if the patch is done, release it already! :)
      Ok, what is not working with it?

      Is it possible to use Win32::Console together (in conjunction) with this Term::ReadKey you think? That is, use Win32::Console just for the special keys. The thing is, all other stuff works nice as I want. It's just these special keys that aint working, and thinking of all the code I've done I would prefer not doing it all over again, just for Win32::Console...

      Update: Seems not possible after a few testings I've made...
Re: Getting special keys to work with Term::ReadKey in Windows
by Anonymous Monk on Jul 24, 2006 at 07:49 UTC