From: Simon on
On 2010/04/06 07:39 PM, David Ching wrote:
> "Simon" <bad(a)example.com> wrote in message
> news:#4ARD0Y1KHA.4724(a)TK2MSFTNGP02.phx.gbl...
>> Because if I click on the OK button the dialog box only closes when I
>> release the left button, so why would (GetAsyncKeyState(VK_LBUTTON)&
>> 0x8000) == 0x8000 ) return true.
>>
>
> VK_LBUTTON is always the physical left mouse button, so if you have
> swapped mouse buttons, you need to check VK_RBUTTON instead:
> http://msdn.microsoft.com/en-us/library/ms646293%28VS.85%29.aspx

Thanks, I did not swap the buttons.

>
> Also, it should be OK the way you have it, but just do:
>
> if ( GetAsyncKeyState(VK_LBUTTON & 0x8000) ) // don't compare == 0x8000
> ;

I am curious, why do you suggest that? I agree that both essentially
mean the same thing, but why do you suggest not comparing to 0x8000?

Also are you sure it is
if ( GetAsyncKeyState(VK_LBUTTON & 0x8000) )
and not
if ( GetAsyncKeyState(VK_LBUTTON) & 0x8000 )
?

>
> If this still does not work, what if you close the dialog by setting
> focus to the OK button and use the keyboard? Does it then work? There
> might be something going on with the dialog's DoModal() as that is a
> separate message loop running, which might screw up the cached keyboard
> state (although I understand GetAsyncKeyState doesn't use a cached
> state, it is supposed to be what the hardware state is at the moment you
> call it.)

When I select the Ok button with the keyboard then the left button does
not report as been down.

Regards,

Simon
From: David Ching on
"Simon" <bad(a)example.com> wrote in message
news:Ok9B0Jh1KHA.5996(a)TK2MSFTNGP05.phx.gbl...
>> if ( GetAsyncKeyState(VK_LBUTTON & 0x8000) ) // don't compare == 0x8000
>> ;
>
> I am curious, why do you suggest that? I agree that both essentially mean
> the same thing, but why do you suggest not comparing to 0x8000?
>
> Also are you sure it is
> if ( GetAsyncKeyState(VK_LBUTTON & 0x8000) )
> and not
> if ( GetAsyncKeyState(VK_LBUTTON) & 0x8000 )
> ?
>

Yes, you are right, it should be the latter one:

if ( GetAsyncKeyState(VK_LBUTTON) & 0x8000 )


Essentially GetAsyncKeyState() returns a bitfield, you are masking the most
significant bit, and you need to test whether that is non-zero. That intent
is most clearly expressed by not doing == 0x8000, which implies comparison
to an integer (not a bitfield) and does not imply testing for bits set.


>>
>> If this still does not work, what if you close the dialog by setting
>> focus to the OK button and use the keyboard? Does it then work? There
>> might be something going on with the dialog's DoModal() as that is a
>> separate message loop running, which might screw up the cached keyboard
>> state (although I understand GetAsyncKeyState doesn't use a cached
>> state, it is supposed to be what the hardware state is at the moment you
>> call it.)
>
> When I select the Ok button with the keyboard then the left button does
> not report as been down.
>

I don't exactly know what's going on, but try using GetKeyState() instead of
GetAsyncKeyState(). I'm not sure if this works with VK_LBUTTON, so also try
GetKeyboardState(). These functions sync the results with the message loop.

-- David

From: Goran on
On Apr 6, 8:13 pm, Joseph M. Newcomer <newco...(a)flounder.com> wrote:
> Perhaps because you had to click the OK button?  And the mouse button is still down?  You
> are essentially seeing the result of the difference between computer speed and human
> speed; within a few tens to a small integer number of hundreds of microseconds after you
> have clicked the button, the mouse button state is examined, and according to the
> instantaneous state, the mouse button is still down, because the message that it has come
> up has not yet been processed.  I find the basic idea of this test more than a little
> weird, actually.  Why would you care if the mouse button was down a few nanoseconds after
> the DoModal() returns?

No, there really is something fishy (I tried yesterday).

You have to __release__ the mouse button for the UI button to fire
"clicked" event (I am guessing, I didn't trace through that, I just
called DoModal()). That's normal behavior. So when I release my mouse
button, UI button should fire "clicked" and mouse button it should
exactly be released. But GetAsyncKeyState says it isn't! Amazing!
Why?! How!? I have no explanation.

Looks like a question for MS people.

> >Is the left button really down?
>
> >Is there something else I could do to check if the left button is down.
>
> ****
> Please explain why you think this is important.

That, I would like to know why, too. Just out of curiosity, might
learn about some use-case I never met in the wild.

Goran.
From: David Lowndes on
>Looks like a question for MS people.

One for Raymond Chen maybe?

I wonder what the value may be if a sleep were added before the
GetAsyncKeyState call?

Dave
From: AliR on
Which OS is this on. I tried this on XP and GetAsyncKeyState said that the
key was down.

AliR.


"Goran" <goran.pusic(a)gmail.com> wrote in message
news:e9e9d8fa-1de1-4415-a311-87ccd1ed5f78(a)g10g2000yqh.googlegroups.com...
On Apr 6, 8:13 pm, Joseph M. Newcomer <newco...(a)flounder.com> wrote:
> Perhaps because you had to click the OK button? And the mouse button is
> still down? You
> are essentially seeing the result of the difference between computer speed
> and human
> speed; within a few tens to a small integer number of hundreds of
> microseconds after you
> have clicked the button, the mouse button state is examined, and according
> to the
> instantaneous state, the mouse button is still down, because the message
> that it has come
> up has not yet been processed. I find the basic idea of this test more
> than a little
> weird, actually. Why would you care if the mouse button was down a few
> nanoseconds after
> the DoModal() returns?

No, there really is something fishy (I tried yesterday).

You have to __release__ the mouse button for the UI button to fire
"clicked" event (I am guessing, I didn't trace through that, I just
called DoModal()). That's normal behavior. So when I release my mouse
button, UI button should fire "clicked" and mouse button it should
exactly be released. But GetAsyncKeyState says it isn't! Amazing!
Why?! How!? I have no explanation.

Looks like a question for MS people.

> >Is the left button really down?
>
> >Is there something else I could do to check if the left button is down.
>
> ****
> Please explain why you think this is important.

That, I would like to know why, too. Just out of curiosity, might
learn about some use-case I never met in the wild.

Goran.