WIn32API and keyboard

Discussion in 'Perl Misc' started by mw, Sep 26, 2006.

  1. mw

    mw Guest

    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
    mw, Sep 26, 2006
    #1
    1. Advertising

  2. mw

    Dave Guest

    "mw" <> wrote in message news:efbam2$t17$...
    > 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.
    Dave, Sep 26, 2006
    #2
    1. Advertising

  3. mw

    Sisyphus Guest

    "mw" <> wrote in message news:efbam2$t17$...
    > 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
    Sisyphus, Sep 27, 2006
    #3
  4. mw

    Robert May Guest

    Sisyphus wrote:
    > "mw" <> wrote in message news:efbam2$t17$...
    >> Hi !
    >>
    >> I test GetAsyncKeyState function on my XP.
    >> Here is my code :


    [snip code calling GetAsyncKeyState(97)]

    >> When I run this code and press "a" key I see only :
    >> 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.)


    URL may wrap:
    http://msdn.microsoft.com/library/d...wsUserInterface/UserInput/VirtualKeyCodes.asp

    97 (or 0x61) is VK_NUMERIC1
    65 (or 0x41) is the 'A' key - for English layout keyboards use the ascii
    code for the LOWER case letter.

    Regards,
    Rob.
    Robert May, Sep 27, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rbt
    Replies:
    4
    Views:
    2,173
  2. brianrpsgt1
    Replies:
    2
    Views:
    257
  3. darkstorme
    Replies:
    0
    Views:
    315
    darkstorme
    Apr 2, 2009
  4. Daniel Berger

    Win32API and frozen objects?

    Daniel Berger, Jun 11, 2005, in forum: Ruby
    Replies:
    0
    Views:
    80
    Daniel Berger
    Jun 11, 2005
  5. Replies:
    2
    Views:
    477
Loading...

Share This Page