From: Simon on
On 2010/04/07 03:50 PM, Goran wrote:
> On Apr 7, 3:11 pm, "AliR"<A...(a)online.nospam> wrote:
>> Which OS is this on. I tried this on XP and GetAsyncKeyState said that the
>> key was down.
> Vista ent. 64-bit.
> Goran.

Thanks for all the replies.

I can reproduce the same issue on WinXP pro SP3 32-bit and Win7 64-bit
Both are physical machines, (I have not tried on virtual boxes), and
both test machines are fairly clean.

On both machines, any of the buttons clicked is only 'clicked' when the
left mouse button is actually released.

From: Hector Santos on
Simon wrote:

> Hi,
> Here is a small description of what I am trying to do.
> I have simplified it to...
> // ----------------------------------
> MyDlg dlg;
> if( IDOK != dlg.DoModal() )
> {
> return;
> }
> ...
> bool left_button = ((GetAsyncKeyState(VK_LBUTTON) & 0x8000) == 0x8000 );
> assert( false == left_button ); // << left_button is down
> // ----------------------------------
> After the dialog box is closed, why is the left mouse button down?
> Is the left button really down?
> Is there something else I could do to check if the left button is down.

GetAsyncKeyState() returns the state information of the last
interrupt, not for the current GUI application.

IOW, even if your application is not the focus, calling that function
will give you information for the entire desktop who might be calling
that function as well.

Try it,

Drop a static text on your form, call it IDC_STATIC_KEYSTATE.

Create a timer in your InitDialog()


and in your OnTimer() handler do this:

void CTestKeyStateDlg::OnTimer(UINT_PTR nIDEvent)
if (nIDEvent == 2)
SHORT akey = GetAsyncKeyState(VK_LBUTTON);
CString Text;
Text.Format("%04d: akey: %04X", nIDEvent, akey);

And watch how that value changes from within any application, even
consoles, as you click the left button, hold it down, drag another
window around, etc.

In short, you really can't rely on this function for your application
state information.