Page 1 of 2

Clearing keyboard buffer in ASM

Posted: Sat Feb 09, 2008 11:07 am
by PB
How would I do this as inline ASM in the smallest possible way? :)

Code: Select all

For k=0 To 255 : GetAsyncKeyState_(k) : Next
Because I use it to clear the keyboard buffer in a loop, and it works well,
but I think it's probably not as fast or as optimized as it could be.

I miss the old C64 days of just doing POKE 198,0 to do it. :)

Posted: Sat Feb 09, 2008 11:18 am
by Derek
Can't you just use one call to setkeyboardstate_() and fill the buffer with #null's?

I haven't tested this but I would guess it would work.

Posted: Sat Feb 09, 2008 12:31 pm
by PB
Not really, because you still need to set all 256 bytes of the buffer with
nulls, which is really no different to doing it my way in the first place.

Posted: Sat Feb 09, 2008 12:53 pm
by Derek
Yeah, but it's all done with one call instead of 256.

Saying that, I doubt if the time taken will be that much different.

Posted: Sat Feb 09, 2008 1:06 pm
by PB
> it's all done with one call instead of 256

Yes, the call is SetKeyboardState, but you still must do a For/Next from
0 to 255 to fill the state with nulls first. Or am I misunderstanding it?

Posted: Sat Feb 09, 2008 1:09 pm
by Derek
Doesn't it just copy a block of memory to the buffer so you could preset the block once and use it over again when needed.

Posted: Sat Feb 09, 2008 1:12 pm
by PB
I'm not sure. :lol: I'll give it a miss and use my way; it's too hard. :)

Posted: Sat Feb 09, 2008 1:17 pm
by Dare
Looks interesting. Would this do it:

Code: Select all

clearBuffer = AllocateMemory(256)
..
SetKeyboardState_(clearBuffer)
I would test it but I have no idea what it does. What actually is cleared? (Not numlock, etc, say the docs). So what disappears. Any key being held down?

Posted: Sat Feb 09, 2008 1:18 pm
by Derek
I did the same thing and ran a loop to check which keys were held down and they all came back as 0 so I'm guessing it works.

Posted: Sat Feb 09, 2008 1:26 pm
by Dare
Interesting. Thanks for this (both of you).

Posted: Sat Feb 09, 2008 1:53 pm
by PB
> SetKeyboardState_(clearBuffer)

Doesn't work. Thanks for trying.

Here's how to test it. Run this code. I want it to show the MessageRequester
ONLY when I hold down Shift and the left mouse button at the same time.

If you leave the commented line as it is, you can click the left mouse button
and then press Shift, and the MessageRequester shows, even though you
didn't do both actions at the same time. This is because the keyboard buffer
isn't cleared.

If you remove the comment mark to enable the For/Next loop, it works.
That's what I want to achieve, in as little and tight as code as possible.

Code: Select all

Repeat

  Sleep_(1)
  
  ;For k=0 To 255 : GetAsyncKeyState_(k) : Next

  If GetAsyncKeyState_(#VK_SHIFT)<>0 And GetAsyncKeyState_(#VK_LBUTTON)<>0
    MessageRequester("test","I only want to see this when pressed together")
  EndIf
  
ForEver

Posted: Sat Feb 09, 2008 2:35 pm
by #NULL
works here no matter how i comment :o *phew*

Posted: Sat Feb 09, 2008 4:39 pm
by MrMat
MSDN says to check the MSB so try GetAsyncKeyState with & $8000.

Posted: Sun Feb 10, 2008 1:13 am
by PB
> works here no matter how i comment :o *phew*

So, if the For/Next is removed/commented, you can run the app, and click
the left mouse button once, release it, then press the Shift key, and you're
saying NO MessageRequester appears? Because it does on three PCs that
I've tried it with. Try it in Safe Mode, because you may be running an app
that is clearing those keys states. And what OS are you running?

@MrMat: I want to know how to clear the keyboard buffer, so doing a check
with $8000 is of no concern to me in this discussion. It may make it work,
but that's not the question I asked. :)

Posted: Sun Feb 10, 2008 3:00 am
by Dare
Well, you're right PB. SetKeyboardState_(clearBuffer) does not give the result you require.

As you already knew that, this post so far is pointless. :D

To make it more so ...

Just exactly what does that map do? I pulled it in (with the complementary get* function) and looked at it and saw lots of zeros and some ones.

Then read the docs. And read the docs. And read the ...

And still don't have a clue.