CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23

Thread: GetKeyState C++

  1. #1
    Join Date
    May 2010
    Posts
    8

    GetKeyState C++

    How can i find the key state of individual characters/numbers?
    I guess what i mean is i need some help understanding VK and the hexedecimal pointer.
    Anyone care to explain? thanks alot!


    Code:
    #include <iostream>
    #include  <windows.h>
    
    using namespace std;
    
    int main()
    {
    while(1)
    {
        if(GetKeyState(VK_CONTROL) & 0x80)
        {
            cout<<"hey"<<endl;
        }
            
        // continue looping regardless, without waiting for any input.
    }
    
    system("pause");
    return 0;
    }

  2. #2
    Join Date
    May 2010
    Posts
    8

    Re: GetKeyState C++

    I guess what i mean is instead of just using VK_CONTROL i'd like to be able to use
    VK_KEY_A
    or
    VK_KEY_L etc...
    but i can't get them to work.
    what am i doing wrong?

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    Re: GetKeyState C++

    Well, first & 0x80 is not a hexidecimal pointer. In this case & is acting as a binary AND.

    Other than that I can't help, I don't know windows.

  4. #4
    Join Date
    Jun 2005
    Posts
    315

    Re: GetKeyState C++

    Try 'A' ....'Z' in place of VK_KEY_A....VK_KEY_A.

  5. #5
    Join Date
    May 2010
    Posts
    8

    Re: GetKeyState C++

    thanks a lot!
    now i have run into another problem.
    everytime i press my key (a)
    it runs the command like 100 times.
    How can i make it only run once per evertime a is pressed?
    thanks.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: GetKeyState C++

    Quote Originally Posted by Jameslat View Post
    thanks a lot!
    now i have run into another problem.
    everytime i press my key (a)
    it runs the command like 100 times.
    How can i make it only run once per evertime a is pressed?
    thanks.
    You're in the wrong forum. There's a special forum for windows issues called C++ and WinAPI.

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: GetKeyState C++

    Quote Originally Posted by Jameslat View Post
    thanks a lot!
    now i have run into another problem.
    everytime i press my key (a)
    it runs the command like 100 times.
    How can i make it only run once per evertime a is pressed?
    thanks.
    "sleep(10)" to add a 10ms pause between each iteration. That's just one solution. More advanced solutions would be to use keyboard hooks and notifications, but that's a whole other level.

    Depending on your needs though, the program called AutoHotKey could be what you are looking for.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  8. #8
    Join Date
    May 2010
    Posts
    8

    Re: GetKeyState C++

    I'll check out autohotkey
    but what are these keyboard hooks?

  9. #9
    Join Date
    Jun 2005
    Posts
    315

    Re: GetKeyState C++

    Could you put the returned by GetKeyState() into a variable, then compare the next GetKeyState() with that variable and do something only when they are different?

  10. #10
    Join Date
    May 2010
    Posts
    8

    Re: GetKeyState C++

    That is a good idea.

  11. #11
    Join Date
    May 2010
    Posts
    8

    Re: GetKeyState C++

    I am running into some errors tho.
    How should i best put it into a varible?

  12. #12
    Join Date
    Jun 2005
    Posts
    315

    Re: GetKeyState C++

    Something along the lines of this, note that I have not compiled or run it though.
    Code:
    #include <iostream>
    #include  <windows.h>
    
    using namespace std;
    
    int main()
    {
    	SHORT last = 0;
    while(1)
    {
        if(last != (last = (GetKeyState(VK_CONTROL) & 0x80)))
        {
            cout<<"hey"<<endl;
        }
            
        // continue looping regardless, without waiting for any input.
    }
    
    system("pause");
    return 0;
    }

  13. #13
    Join Date
    Jun 2008
    Posts
    592

    Re: GetKeyState C++

    Quote Originally Posted by Jameslat
    now i have run into another problem.
    everytime i press my key (a)
    it runs the command like 100 times.
    How can i make it only run once per evertime a is pressed?
    if you want to capture keys from the keyboard, do this with a keyboard hook

    Code:
    #include <iostream>
    #include <windows.h>
     
    using namespace std;
    HHOOK hooked = 0;
     
    LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam)
    {
        KBDLLHOOKSTRUCT* ABoutKeyboard = (KBDLLHOOKSTRUCT*)lParam;
     
        cout << "Recieved key" << ABoutKeyboard->vkCode << endl;
     
        return CallNextHookEx ( hooked, nCode , wParam, lParam );
    }
     
    int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
    {
     
        hooked = SetWindowsHookEx(WH_KEYBOARD_LL, &LowLevelKeyboardProc, hThisInstance, NULL);
        if (hooked == NULL)
        {
            cout << "Error" << endl;
            return 1;
        }
     
        MSG messages;
     
        while (GetMessage (&messages, NULL, 0, 0))
        {
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
     
        return messages.wParam;
    }
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  14. #14
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: GetKeyState C++

    Quote Originally Posted by jeron View Post
    if(last != (last = (GetKeyState(VK_CONTROL) & 0x80)))[/code]
    This, while fancy, is undefined behavior. C++ does not guarantee which side of operator!= is evaluated first. The right side could be evaluated first, making the condition always true.

    Just take the time to create both a "current" and "previous" variable. The execution cost is virtually null. The code is longer, but easier to read and with clearer intent.

    Section 1.9 of the standard.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  15. #15
    Join Date
    Jun 2005
    Posts
    315

    Re: GetKeyState C++

    This, while fancy, is undefined behavior. C++ does not guarantee which side of operator!= is evaluated first. The right side could be evaluated first, making the condition always true.

    Just take the time to create both a "current" and "previous" variable. The execution cost is virtually null. The code is longer, but easier to read and with clearer intent.

    Section 1.9 of the standard.
    Excellent points!

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured