Thread: GetAsyncKeyState help

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    13

    GetAsyncKeyState help

    hi guys
    well ive been learning C++ the last few days and have started to create an address book app

    but ....

    if you have a look at the code : http://x-kane.com/programming/addressbookmain.cpp

    this one sort of works (so far)
    the problem i have is in the switch statement i have had to change the VK_RETURN to VK_SPACE.
    the reason ... well after the switch called the "addEntries" function, it worked but then after i pressed escape in the addEntries it seemed to recall itself .. like it would go bak to the main menu (could see it flash on) but then went straight back to addEntries.
    hard to explain but if you change the switch like i suggested above and enter data then try and press escape ..you will see what i mean.

    its like its keeping the VK_RETURN in memory ... do i have to "clear" it or something?


    and one more question ... the while loops i have in the program use 100% cpu ... what other options is there if these loops do this?

    thanks alot
    Kane

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    GetAsyncKeyState will let you know what is being press when you call the function. Your code goes by so fast, by the time the user lets the key go, it already told the program that it's still being pressed. A common solution is to have a flag that keeps track of the status of the return key (up or down) and use that in your checks as well.
    Code:
    bool returnDown = true;
    
    if(GetAsyncKeyState(VK_RETURN) & 0x80 && returnDown)
    {
       //do whatever you want
       returnDown = false;
    }
    else
    {
       if(!GetAsyncKeyState(VK_RETURN) & 0x80)
            returnDown = true;
    }
    This is a quick and dirty way of handling buffered input. The CPU problem shouldn't be much of an issue, unless it freezes the system. How else will you keep the program running (assuming you want to keep this a console app)?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    13
    thanks alot that worked.

    and yes i was talking about console apps
    Last edited by Kane; 01-29-2005 at 06:39 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    13
    right i have that fixed sort of now ....

    but i have another problem (instead of creating a new thread)

    http://x-kane.com/programming/addressbookmain.cpp

    this is the updated code.

    if you run it and go straight into add entry then it works fine.

    but if yu first go into view entries then into add ...it looks like enter has already been pressed on the first value. so i tried a cin.ignore();

    but that didnt work.

    and if you keep going into view then into add it will complete the entries the same amount of times you entered the view entries

    but i cant see how as no variables like that are passed into the addentries function

    Kane

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    13
    got it working fine at last

    2 days .... (im such a newbie)

    i used getch() for the return and escape keys. and then just getasynckeystate for the up/down keys.

    thanks
    Kane

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GetAsyncKeyState.
    By kevinawad in forum Windows Programming
    Replies: 9
    Last Post: 11-09-2008, 05:02 PM
  2. GetAsyncKeyState Problem
    By kevinawad in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2008, 06:00 PM
  3. Problems with cin and GetAsyncKeyState
    By KgNe in forum C++ Programming
    Replies: 32
    Last Post: 08-21-2008, 10:00 AM
  4. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM