in reply to GetAsyncKeyState API Question
#!/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
|
|---|