Flushing the Keyboard Buffer

MorganO

Active Member
Joined
Nov 21, 2006
Messages
483
Hello all.

I've run into a problem and I cannot resolve. I am designing an Excel Application in VBA. During the execution of the VBA code, the user will press different keys on the keyboard. These keystrokes are determined by using the GetAsyncKeyState Function. The problem happens when I exit from the VBA code and move back to a worksheet outside the code execution.

Lets say a user has pressed the 'Q' and 'X' key just prior to the final VBA subroutine running and releasing the worksheets back to the user. On the worksheet, in the selected cell will appear the 'Q' and 'X'. For some reason these keystrokes are not 'flushed' from the keyboard buffer prior to the code completion and are being transfered to the active worksheet. This is really annoying if I am testing the code in the VBA editor, because the spurious keystrokes will be placed in my VBA code, causing me to search for the out of place characters.

Any advice you could give on resolving this issue would be greatly appreciated!

Owen Morgan
 
Greg,

Hmm..you are correct, I could have done that. Didn't even think of using the workbook change routine to handle this issue...

I walked down this path a different way I guess, and got it in my head that I needed to clear the buffer rather than just letting excel clear it for me using your method.

I often do walk down a different path than others though, since I am self taught in all things Microsoft. When I first learned to use Powerpoint I didn't understand the concept of multiple slide animation, but I did understand layering. I made an awesome 'Slide Show' using one slide but with many many layered objects. The guy I gave it to thought I was very strange...

Take care.

Owen
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I understand the situation (now).

I have no fix for it, but I'll work on it.

If you find a solution, please post it.
 
Upvote 0
I have run into this issue where I am looking up the current cell value in a range and want to progress to the next occurance by pressing the enter (or another) key, without having messageboxes shown.
So I capture the keystroke in a loop (as per http://www.mrexcel.com/forum/excel-questions/538074-how-pause-macro-then-resume-via-keystrokes.html) but then if the buffer is not cleared the routine just finishes because it finds the enter key in the buffer.

So with PeekMessage you can get rid of the character in the keyboard buffer. I will post the full code once it runs properly. I think peekmessage function can be simplified somewhat if you are not looking for mouse events.
 
Upvote 0
User this code which will let windows perform events by pressed keys and will wait, will again ask to perform jobs of keys pressed and then will go ahaed. Very Simple only three line code.
DoEvents
application.wait = now() + timevalue("0:00:01")
DoEvents
 
Upvote 0
Perhaps not followed, but definitely one of 2 threads that pop up when "flushing keyboard buffer" is searched for at mrexcel.com
 
Upvote 0
Yep, always useful to see some alternative solutions!
 
Upvote 0

Forum statistics

Threads
1,215,941
Messages
6,127,794
Members
449,408
Latest member
Bharathi V

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top