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

I am looking for some code examples for GetAsyncKeyState written in Perl. All I find are VB examples.

Here is what I have so far and all it gives me are zeros:

$pressed = <STDIN>; $Key = Win32::API ("user32", "GetAsyncKeyState", [P], "N"); $key = $Key->Call($pressed); print "$key";
Where am I missing the boat?

Edited by dws to add <code> tags

Replies are listed 'Best First'.
Re: GetAsyncKeyState API Question
by jplindstrom (Monsignor) on Feb 06, 2002 at 01:32 UTC
    Try this, but note that you should take care of any key presses unless you run this in a window environment (e.g. Win32::GUI):
    #!/usr/bin/perl -w use strict; use Win32::API; use constant VK_SHIFT => 0x10; use constant VK_CONTROL => 0x11; use constant VK_MENU => 0x12; =head2 GetAsyncKeyState($keyCode) Retrieve the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off-- alternating each time the key is pressed). $keyCode -- A single character string: A..Z0..9, or a virtual key code. Example: VK_SHIFT, "C" Return 1 if the key is depressed, 0 if it's not. =cut my $rsGetAsyncKeyState = new Win32::API("user32", "GetAsyncKeyState", +"I", "N"); sub GetAsyncKeyState { my ($keyCode) = @_; $keyCode = ord($keyCode) if($keyCode =~ /^[A-Z0-9]$/); my $ret = $rsGetAsyncKeyState->Call($keyCode); $ret = unpack("S", pack("s", $ret)); return( $ret & 2**15 ? 1 : 0 ); } my $key = VK_SHIFT; #my $key = "8"; #my $key = "J"; while(1) { print "state: " . GetAsyncKeyState($key) . "\n"; sleep(1); }

    There is more to read about this if you look up GetAsyncKeyState in the Win32 API, e.g. here: http://www.sxlist.com/techref/os/win/api/win32/func/src/f27_3.htm

    /J