Thread: GetKeyState()

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    67

    Smile GetKeyState()

    Hello
    Quick question:

    Code:
    while(1){
        if(GetKeyState(VK_SHIFT)<0){ //if SHIFT pressed
            cout<<"control "<<endl;
            if(GetKeyState('a')<0){ //if SHIFT is still being pressed and then 'a' is pressed too
                cout<<"a"<<endl;
            }
        }
        else{ //if no keys are pressed
            cout<<"No key being pressed"<<endl;
        }
    }
    GetKeyState(VK_SHIFT) works. Some don't work (like the virtual key VK_SPACE) for some reason. To detect ENTER instead of 'a', I use GetKeyState(VK_RETURN), which works.

    Is there an easy way to assign a virtual key tag to nonvirtual keys like characters, numbers, and so on?

    Compiler: Dev C++
    Would it understand GetKeyState('a') as being a character key press of 'a' if I compiled it with Visual C++?

    I did check out the table here. I tried the codes for the capital letters, the top numbers, and the numpad, but none worked.
    I checked out ASCII tables (writing decimal 97 or hex 0x61 in place of 'a'), but those didn't work either.

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Its 0x41 for key A.

    Virtual Key Codes There is table. Doesn't matter it's delphi.

  3. #3
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    I think you should write 0X41 without quotation mark...
    Give that a try...

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    You can write with quotation mark, just use capital letter 'A'.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I personally use this for key-strokes in Windows:
    Code:
    #define KEYDOWN(vkey) (GetAsyncKeyState(vkey) & 0x8000)
    #define KEYUP(vkey) !KEYDOWN(vkey)
    And yes, use capital letters. Lowercase ones won't do the trick.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-11-2010, 08:00 AM
  2. need help with getkeystate
    By newbie421 in forum C++ Programming
    Replies: 2
    Last Post: 06-05-2007, 05:44 PM
  3. GetKeyState equivalent function ...
    By twomers in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2006, 03:35 PM

Tags for this Thread