WIn32API and keyboard

M

mw

Hi !

I test GetAsyncKeyState function on my XP.
Here is my code :

use Win32::API;

my $rsGetAsyncKeyState = new Win32::API("user32", "GetAsyncKeyState", "N",
"I");

sub GetAsyncKeyState {
my ($keyCode) = @_;
my $ret = $rsGetAsyncKeyState->Call($keyCode);
print "ret = $ret\n";
return( $ret & 1 );
}


while () {
sleep 1 ;
$keystate=GetAsyncKeyState(97) ;
# 97 is "a" ascii code
#print "a state=$keystate\n" ; ;
}


When I run this code and press "a" key I see only :

..
..
ret = 0
ret = 0
ret = 0
..
..
..
It means that "a" key is not pressed.
What's wrong ?

Pls help
 
D

Dave

mw said:
Hi !

I test GetAsyncKeyState function on my XP.
Here is my code :

use Win32::API;

my $rsGetAsyncKeyState = new Win32::API("user32", "GetAsyncKeyState", "N",
"I");

sub GetAsyncKeyState {
my ($keyCode) = @_;
my $ret = $rsGetAsyncKeyState->Call($keyCode);
print "ret = $ret\n";
return( $ret & 1 );
}


while () {
sleep 1 ;
$keystate=GetAsyncKeyState(97) ;
# 97 is "a" ascii code
#print "a state=$keystate\n" ; ;
}


When I run this code and press "a" key I see only :

.
.
ret = 0
ret = 0
ret = 0
.
.
.
It means that "a" key is not pressed.
What's wrong ?

Pls help

I would have a look at
http://msdn.microsoft.com/library/d...e/keyboardinputfunctions/getasynckeystate.asp

especially the note for NT/2000/XP.

Also, I'm not sure what your "N" and "I" represent.
 
S

Sisyphus

mw said:
Hi !

I test GetAsyncKeyState function on my XP.
Here is my code :

use Win32::API;

my $rsGetAsyncKeyState = new Win32::API("user32", "GetAsyncKeyState", "N",
"I");

sub GetAsyncKeyState {
my ($keyCode) = @_;
my $ret = $rsGetAsyncKeyState->Call($keyCode);
print "ret = $ret\n";
return( $ret & 1 );
}


while () {
sleep 1 ;
$keystate=GetAsyncKeyState(97) ;
# 97 is "a" ascii code
#print "a state=$keystate\n" ; ;
}


When I run this code and press "a" key I see only :

.
.
ret = 0
ret = 0
ret = 0
.
.
.
It means that "a" key is not pressed.
What's wrong ?

No - it means that the numeric keypad "1" key has not been pressed. (These
are "Virtual-Key" codes, not ascii codes. There should be a listing of them
somewhere on the Microsoft website.)

Press the "1" key in your numeric keypad and you'll see 'ret = 1' appear at
the next iteration of the loop.

Cheers,
Rob
 
R

Robert May

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,795
Messages
2,569,644
Members
45,358
Latest member
TreyTritt8

Latest Threads

Top