473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do I clear the keyboard buffer?

Don
I've got a loop from within which I make a call to the API function
GetAsyncKeystate to see if the Escape key was pressed. If it is, then the
exit condition of the loop is met and the program goes on to display a
simple messagebox. Unfortunately, the program seems to think the Escape key
was meant for this messagebox, even though it appeared *after* the Escape
key was pressed(!) and instantly closes the messagebox before you can even
see it. Thru testing, I have become 100% positive that hitting the Escape
key to exit the loop is causing the problem.

I've tried two methods of clearing the keyboard buffer that I've found (both
were for VB5/6). On involved using the SetKeyboardState API call and the
other involved PeekMessage. Neither worked. I even tried making a loop
where GetAsyncKeystate was called repeatedly (i.e. Do: Loop Until
(GetAsyncKeyState(Keys.Escape) And &H8000) = 0)until it thought the Escape
key was no longer pressed...and the message box still didn't appear!

If I tested for another key, like the 'E' key, it worked fine and the
messagebox appeared (it didn't receive any Escape character to close
itself).

I can't figure out how to get around this. Is there anything I can do to
absorb the Escape key if it's there and do nothing if it's not? Is there
anyway to clear the real keyboard buffer that must exist somewhere that
GetAsyncKeystate doesn't have access to?

This is essentially what the code looks like (extraneous stuff removed):
Do
' Do whatever; hit Escape in here to exit loop
Loop until (GetAsyncKeyState(Keys.Escape) And &H8000) <> 0

Msgbox "Hello!" ' <--- Will not appear if we tested for Keys.Escape
' in call to GetAsyncKeyState above.
Msgbox "Goodbye!" ' <--- Will appear always.
Does anyone have any insight?

- Don
Nov 20 '05 #1
2 18794
Hi Don,

Putting Application.DoEvents after your loop will cause messages on the
message queue to be used up. This will include your Escape Key which will go
to the currently active control. If your Form has a button set as the Cancel
button (ie, in response to Escape), this will be triggerred, oops. If not,
however, the key will be discarded

Another possible solution is to call Application.DoEvents <within> your
loop which would allow the user to click a [Stop] button. This could then set
a variable which your loop checks.

Another way is to do this with threads, but that may be getting a bit
heavy for what your app is doing.

Regards,
Fergus
Nov 20 '05 #2
Don

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:e#**************@TK2MSFTNGP10.phx.gbl...
Hi Don,

Putting Application.DoEvents after your loop will cause messages on the message queue to be used up.


And here I thought they got rid of good ol' DoEvents. Thanks! It worked!

- Don
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard...
1
by: Larry | last post by:
Hi my friends. How can I read keyboard buffer by C++ ? Best regards, Larry
5
by: nx-2000 | last post by:
I've got a very large C# forms app and now that its being used in bigger environments we're getting a steady stream of "why does it do this?" problems. The most nagging of which right now is that...
0
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as...
2
by: rs | last post by:
Hi guys, I am trying to read from a USB keyboard using vb.net and HID classes. the USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and is detected in device manager as...
2
by: Mike | last post by:
Does anyone have a simple function to clear the keyboard buffer? I need to make sure and clear any keystrokes before I exit my application. Thanks, Mike
1
by: Damir | last post by:
Hallo everybody Does anyone knows how to access Keyboard buffer? At the moment I'm catching KeyDown event of the form, and waiting for carrige return or return key, but sometimes the Keyboard...
3
by: muthursyamburi | last post by:
Hello All, I'm really looking for a help in reading/writing the keyboard buffer in AIX (IBM Unix). In DOS, there is some memory addres (I remember it as 0x417) from where the keyboard buffer...
3
by: NaN | last post by:
I've been trying to use _kbhit() but it didn't do what I thought it would from books, "Detects whether a keypress is available for reading." Herbert Schildt says, "If the user has pressed a key,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.