Clearing keyboard buffer in ASM

Just starting out? Need help? Post your questions and find answers here.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

PB wrote:> 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. :)
@PB: If your code checks to see if the keyboard buffer was cleared why do you have it display a message that says "I only want to see this when pressed together?" That's seems to contradict. When I run the code with the specific line commented I don't even have to click the mouse button and it displays the message by simply pushing Shift by itself.
One other thing to note, GetAsyncKeyState_() does not use a keyboard buffer. It keeps track of the actual state of the key and whether it was pushed since the last call of GetAsyncKeyState (for that key). By the way the specific code can be fixed by the following:

Code: Select all

Repeat
  
  Sleep_(1)
  GetAsyncKeyState_(#VK_LBUTTON) ;use this line to fix your problem
  ;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")
    End
  EndIf
  
ForEver 
You don't have to clear the status of every key, just check if the desired key is down or clear the status of the desired key then check it (sounds the same as checking if it's down, but works better for more keys I suppose) .

I remember you were trying to find the fastest way to clear the "status" of each key, since we're not using a buffer. It would seem the loop you proposed originally would do that.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> @PB: If your code checks to see if the keyboard buffer was cleared
> why do you have it display a message that says "I only want to see this
> when pressed together?" That's seems to contradict.

It doesn't check to see if it's cleared. It checks to see if Shift and the LMB
are down at the same time. If they are, the MessageRequester appears.

> When I run the code with the specific line commented I don't even have
> to click the mouse button and it displays the message by simply pushing
> Shift by itself.

Exactly! That's NOT supposed to happen, though. That's why the keyboard
buffer needs clearing first with the For/Next loop. You've proved my point.

> [GetAsyncKeyState_()] keeps track of the actual state of the key and
> whether it was pushed since the last call of GetAsyncKeyState

I know, so by doing the For/Next loop you basically clear the key buffer,
so that the state for each key becomes 0. This means when you next test
it, it will be non-0, and you know it's either been clicked or is down.

> You don't have to clear the status of every key

I know, but I want to know how to clear the keyboard buffer anyway.
Again, that is the entire subject of this thread, as I've said before. My
code example is just to show why. People keep missing the point and
seem fixated on my code example instead.

> I remember you were trying to find the fastest way to clear the "status"
> of each key, since we're not using a buffer. It would seem the loop you
> proposed originally would do that.

I know, but my original question was: is there a faster way to do this, for
example in ASM?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

PB wrote:> @PB: If your code checks to see if the keyboard buffer was cleared
> why do you have it display a message that says "I only want to see this
> when pressed together?" That's seems to contradict.

It doesn't check to see if it's cleared. It checks to see if Shift and the LMB
are down at the same time. If they are, the MessageRequester appears.

> When I run the code with the specific line commented I don't even have
> to click the mouse button and it displays the message by simply pushing
> Shift by itself.

Exactly! That's NOT supposed to happen, though. That's why the keyboard
buffer needs clearing first with the For/Next loop. You've proved my point.

> [GetAsyncKeyState_()] keeps track of the actual state of the key and
> whether it was pushed since the last call of GetAsyncKeyState

I know, so by doing the For/Next loop you basically clear the key buffer,
so that the state for each key becomes 0. This means when you next test
it, it will be non-0, and you know it's either been clicked or is down.

> You don't have to clear the status of every key

I know, but I want to know how to clear the keyboard buffer anyway.
Again, that is the entire subject of this thread, as I've said before. My
code example is just to show why. People keep missing the point and
seem fixated on my code example instead.

> I remember you were trying to find the fastest way to clear the "status"
> of each key, since we're not using a buffer. It would seem the loop you
> proposed originally would do that.

I know, but my original question was: is there a faster way to do this, for
example in ASM?
As stated, GetAsyncKeyState maintains a "Key status" not a "keyboard buffer". A buffer would record the order of changes. I answered your question as well (after correcting the terms used). The answer is that there is not a faster way. The last point is that your code sample doesn't illustrate your point any more than this next example illustrates if circles are squares:

Code: Select all

Repeat

  Sleep_(1)
  
  ;For K=0 To 255 : a=1 : Next
  
  If 4 - 3 + a = 1 And  7 - 6 + a = 1
    MessageRequester("test","I only want to see this if circles are squares")
    End
  EndIf
ForEver
This should only print the message if circles are squares, which it doesn't unless you uncomment the line.

A statement like the above doesn't prove anything, it just shows a person can write a useless program that prints a message (this one doesn't prove circles are squares, regardless of whether or not the message prints, since it jerry-rigged to do so or not to do so.). The reason everybody is referring to your code is because it doesn't illustrate your point. Your code checks to see if two things are "happening at the same time" and if they don't you say it's proof that the buffer(?) is not cleared. Since you aren't checking a buffer why do you keep saying that? (Check the thread topic).

I hope that this clears up some things. I repeated these because you didn't seem to validate what was shared by myself or others. I know I've shared more than enough. As an afterthought I realize the difficulties may be language based. I agree with you though, older OS's had an easier time checking a key's status.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> Your code checks to see if two things are "happening at the same time"
> and if they don't you say it's proof that the buffer(?) is not cleared.
> Since you aren't checking a buffer why do you keep saying that?

Okay, maybe "buffer" is the wrong term here, which is confusing things.
I admit that "status" is probably the correct term. In any event, doing a
check of a key's status with GetAsyncKeyState does reset that key's state
to 0, which is what I want. So I was doing a For/Next of all keys (0-255)
to accomplish this, and just wondered if there were a faster way instead.
Sorry for all the confusion with the terms.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

PB wrote:@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. :)
It does work and it's the right way of doing what you posted in your example code :)

Can you give an example where your loop is necessary?
Mat
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

@MrMat: You're right; I've seen the light now (finally!). :D
I will use GetAsyncKeyState(key) & $8000 from now on.
Thanks for your patience, and to everyone else too.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

That's great PB! :)
Mat
Post Reply