Clearing keyboard buffer in ASM

Just starting out? Need help? Post your questions and find answers here.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Clearing keyboard buffer in ASM

Post 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. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post 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.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post 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.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post 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.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

I'm not sure. :lol: I'll give it a miss and use my way; it's too hard. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post 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?
Dare2 cut down to size
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post 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.
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Interesting. Thanks for this (both of you).
Dare2 cut down to size
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Post by #NULL »

works here no matter how i comment :o *phew*
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

MSDN says to check the MSB so try GetAsyncKeyState with & $8000.
Mat
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post 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.
Dare2 cut down to size
Post Reply