By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 1,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

Clear keyboard buffer

P: n/a
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 when a
MessageBox.Show() is displayed, if the user hits enter or esc, those
keys get passed back to the form. From searching online, this is
apparently by design(for whom, I don't know, why its not optional, I
_really_ don't know). This software was designed for users who are
more used to the keyboard than the mouse and so every form has keyboard
hooks. So I thought to myself, rather than dig through all 12 megs of
source code, why don't I just clear the keyboard buffer and do
something like this:

public class MessageBox
{
private static void KillKeys()
{
// clear the keyboard buffer
}
public static DialogResult Show(string text)
{
DialogResult r=System.Windows.Forms.MessageBox.Show(text);
KillKeys();
return(r);
}
// implement all other Show() methods
}

But, I'm unable to find anything, anywhere, that tells me how to clear
the keyboard buffer. The recommendations I've seen online are for
console apps. Is there anyway to clear the keyboard buffer in C# for
winforms?

Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

Clear keyboard buffer?? Oh wait I can help here... Hang on... Digging...
Clear buffer.. I think you just read keys until empty but I know I hacked
that up... Must've been back when I was with that hot red head... Back when
rates were pretty good... I think it was... Ahhh, here it is:

Uhhm, it's in pieces because it was coded as part of a larger library, also
you need the import dll installed on the machine - I think it is installed on
most machines by default, if not you'll have to import the _kbhit() &
_getwch() functions from one of the other standard C runtime dlls:

[DllImport("msvcr71.dll")]
static extern int _kbhit();

[DllImport("msvcr71.dll")]
static extern char _getwch();

// return true/false if a keypress is pending
public static bool KeyPressed
{
get
{
return (_kbhit() != 0);
}
}

// fetch next char in buffer else null - return immediately
public static char GetChar()
{
return (KeyPressed) ? _getwch() : NullChar;
}

// flush all pending keystrokes in buffer - return immediately
public static void FlushKeyboardBuffer()
{
while (KeyPressed) GetChar();
}

Nov 17 '05 #2

P: n/a
Thanks for the super-speedy response, unfortunately, it didn't work,
the keyup event still fired. Here's my code:

public class KillKeysFilter : System.Windows.Forms.Form,IMessageFilter
{
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
public bool PreFilterMessage(ref Message m)
{
Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode;
if (
(m.Msg == WM_KEYUP && keyCode == Keys.Enter) ||
(m.Msg == WM_KEYUP && keyCode == Keys.Tab) ||
(m.Msg == WM_KEYDOWN && keyCode == Keys.Enter) ||
(m.Msg == WM_KEYDOWN && keyCode == Keys.Tab)
)
{
return true;
}
return false;
}
}
public class MessageBox
{

/*
///// Method 1, PeekMessage
struct MSG
{
IntPtr hwnd;
uint message;
IntPtr wParam;
IntPtr lParam;
int time;
int ptX;
int ptY;
}
[DllImport("user32.dll")]
static extern bool PeekMessage(out MSG lpMsg, uint hWnd, uint
wMsgFilterMin,uint wMsgFilterMax, uint wRemoveMsg);
const int PM_REMOVE = 0x0001;
const int WM_KEYFIRST = 0x0100;
const int WM_KEYLAST = 0x0109;
private static void KillKeysStart()
{
}
private static void KillKeysEnd()
{
MSG Msg;
while(PeekMessage(out Msg, 0, WM_KEYFIRST, WM_KEYLAST,PM_REMOVE ))
;
}
*/

/*
// Method 2, MessageFilter
private static KillKeysFilter kkf=new KillKeysFilter();
private static void KillKeysStart()
{
Application.AddMessageFilter(kkf);
Application.DoEvents();
}
private static void KillKeysEnd()
{
Application.DoEvents();
Application.RemoveMessageFilter(kkf);
}
*/

// Method 3, kbhit/getwch
[DllImport("msvcr71.dll")]
static extern int _kbhit();
[DllImport("msvcr71.dll")]
static extern char _getwch();
// return true/false if a keypress is pending
public static bool KeyPressed
{
get { return (_kbhit() != 0); }
}
// flush all pending keystrokes in buffer - return immediately
public static void FlushKeyboardBuffer()
{
while (KeyPressed) _getwch();
}
private static void KillKeysStart()
{
}
private static void KillKeysEnd()
{
FlushKeyboardBuffer();
Application.DoEvents();
FlushKeyboardBuffer();
}

public static DialogResult Show(string text)
{
KillKeysStart();
DialogResult r=System.Windows.Forms.MessageBox.Show(text);
KillKeysEnd();
return(r);
}
public static DialogResult Show(IWin32Window owner, string text)
{
KillKeysStart();
DialogResult r=System.Windows.Forms.MessageBox.Show(owner, text);
KillKeysEnd();
return(r);
}
public static DialogResult Show(string text, string caption)
{
KillKeysStart();
DialogResult r=System.Windows.Forms.MessageBox.Show(text, caption);
KillKeysEnd();
return(r);
}
public static DialogResult Show(IWin32Window owner, string text,
string caption)
{
KillKeysStart();
DialogResult r=System.Windows.Forms.MessageBox.Show(owner, text,
caption);
KillKeysEnd();
return(r);
}
public static DialogResult Show(string text, string caption,
MessageBoxButtons buttons)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(text,captio n,buttons);
KillKeysEnd();
return(r);
}
public static DialogResult Show(IWin32Window owner, string text,
string caption, MessageBoxButtons buttons)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(owner,text, caption,buttons);
KillKeysEnd();
return(r);
}
public static DialogResult Show(string text, string caption,
MessageBoxButtons buttons, MessageBoxIcon icon)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(text,captio n,buttons,icon);
KillKeysEnd();
return(r);
}
public static DialogResult Show(IWin32Window owner, string text,
string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(owner,text, caption,buttons,icon);

KillKeysEnd();
return(r);
}
public static DialogResult Show(string text, string caption,
MessageBoxButtons button, MessageBoxIcon icon, MessageBoxDefaultButton
defaultButton)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(text,captio n,button,icon,defaultButton);

KillKeysEnd();
return(r);
}
public static DialogResult Show(IWin32Window owner, string text,
string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(owner,text, caption,buttons,icon,defaultButton);

KillKeysEnd();
return(r);
}
public static DialogResult Show(string text, string caption,
MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton
defaultButton, MessageBoxOptions options)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(text,captio n,buttons,icon,defaultButton,options);

KillKeysEnd();
return(r);
}
public static DialogResult Show(IWin32Window owner, string text,
string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(owner,text, caption,buttons,icon,defaultButton,options);

KillKeysEnd();
return(r);
}

}

Nov 17 '05 #3

P: n/a

The code didn't work or it did work and the issue now is the key up event??
Original post did not mention key up event issues...

If key up event is the issue then you might be in a spot. You'll either
have to set up a finite state machine such that you know when you are
flushing the keyboard {or some other mechanism to ignore event} else learn
enough about keyboard communications to PInvoke the chars out of the keyboard
buffer before windows sees them...

"nx*****@winvoice.com" wrote:
Thanks for the super-speedy response, unfortunately, it didn't work,
the keyup event still fired. Here's my code:

public class KillKeysFilter : System.Windows.Forms.Form,IMessageFilter
{
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
public bool PreFilterMessage(ref Message m)
{
Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode;
if (
(m.Msg == WM_KEYUP && keyCode == Keys.Enter) ||
(m.Msg == WM_KEYUP && keyCode == Keys.Tab) ||
(m.Msg == WM_KEYDOWN && keyCode == Keys.Enter) ||
(m.Msg == WM_KEYDOWN && keyCode == Keys.Tab)
)
{
return true;
}
return false;
}
}
public class MessageBox
{

/*
///// Method 1, PeekMessage
struct MSG
{
IntPtr hwnd;
uint message;
IntPtr wParam;
IntPtr lParam;
int time;
int ptX;
int ptY;
}
[DllImport("user32.dll")]
static extern bool PeekMessage(out MSG lpMsg, uint hWnd, uint
wMsgFilterMin,uint wMsgFilterMax, uint wRemoveMsg);
const int PM_REMOVE = 0x0001;
const int WM_KEYFIRST = 0x0100;
const int WM_KEYLAST = 0x0109;
private static void KillKeysStart()
{
}
private static void KillKeysEnd()
{
MSG Msg;
while(PeekMessage(out Msg, 0, WM_KEYFIRST, WM_KEYLAST,PM_REMOVE ))
;
}
*/

/*
// Method 2, MessageFilter
private static KillKeysFilter kkf=new KillKeysFilter();
private static void KillKeysStart()
{
Application.AddMessageFilter(kkf);
Application.DoEvents();
}
private static void KillKeysEnd()
{
Application.DoEvents();
Application.RemoveMessageFilter(kkf);
}
*/

// Method 3, kbhit/getwch
[DllImport("msvcr71.dll")]
static extern int _kbhit();
[DllImport("msvcr71.dll")]
static extern char _getwch();
// return true/false if a keypress is pending
public static bool KeyPressed
{
get { return (_kbhit() != 0); }
}
// flush all pending keystrokes in buffer - return immediately
public static void FlushKeyboardBuffer()
{
while (KeyPressed) _getwch();
}
private static void KillKeysStart()
{
}
private static void KillKeysEnd()
{
FlushKeyboardBuffer();
Application.DoEvents();
FlushKeyboardBuffer();
}

public static DialogResult Show(string text)
{
KillKeysStart();
DialogResult r=System.Windows.Forms.MessageBox.Show(text);
KillKeysEnd();
return(r);
}
public static DialogResult Show(IWin32Window owner, string text)
{
KillKeysStart();
DialogResult r=System.Windows.Forms.MessageBox.Show(owner, text);
KillKeysEnd();
return(r);
}
public static DialogResult Show(string text, string caption)
{
KillKeysStart();
DialogResult r=System.Windows.Forms.MessageBox.Show(text, caption);
KillKeysEnd();
return(r);
}
public static DialogResult Show(IWin32Window owner, string text,
string caption)
{
KillKeysStart();
DialogResult r=System.Windows.Forms.MessageBox.Show(owner, text,
caption);
KillKeysEnd();
return(r);
}
public static DialogResult Show(string text, string caption,
MessageBoxButtons buttons)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(text,captio n,buttons);
KillKeysEnd();
return(r);
}
public static DialogResult Show(IWin32Window owner, string text,
string caption, MessageBoxButtons buttons)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(owner,text, caption,buttons);
KillKeysEnd();
return(r);
}
public static DialogResult Show(string text, string caption,
MessageBoxButtons buttons, MessageBoxIcon icon)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(text,captio n,buttons,icon);
KillKeysEnd();
return(r);
}
public static DialogResult Show(IWin32Window owner, string text,
string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(owner,text, caption,buttons,icon);

KillKeysEnd();
return(r);
}
public static DialogResult Show(string text, string caption,
MessageBoxButtons button, MessageBoxIcon icon, MessageBoxDefaultButton
defaultButton)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(text,captio n,button,icon,defaultButton);

KillKeysEnd();
return(r);
}
public static DialogResult Show(IWin32Window owner, string text,
string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(owner,text, caption,buttons,icon,defaultButton);

KillKeysEnd();
return(r);
}
public static DialogResult Show(string text, string caption,
MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton
defaultButton, MessageBoxOptions options)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(text,captio n,buttons,icon,defaultButton,options);

KillKeysEnd();
return(r);
}
public static DialogResult Show(IWin32Window owner, string text,
string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
{
KillKeysStart();
DialogResult
r=System.Windows.Forms.MessageBox.Show(owner,text, caption,buttons,icon,defaultButton,options);

KillKeysEnd();
return(r);
}

}

Nov 17 '05 #4

P: n/a
The issue is that when I fire a MessageBox.Show() in my code, if the
user presses Enter or Esc to dismiss the MessageBox, the form
underneath gets a KeyUp event with that enter or esc key. I was trying
to create a wrapper around the standard MessageBox class that would
clear any keys from the buffer. So far, I've tried an IMessageFilter,
PeekMessage, and now kbhit/getwch. No matter what, anywhere I've got
MessageBox.Show(), that form is going to get those enter keys, which
are used for navigation(SendKeys(Keys.Tab) on enter). So if I pop up a
messagebox and tell the user they have an error someplace and then set
the focus to that textbox, the focus actually changes to the next
control in the tab order.

Let's say we have three controls..two textboxes and a button
TextBox1
TextBox2
Button1

I set the user on TextBox1, the user types in a number, presses enter,
the form changes their focus to TextBox2, they type in another number,
press enter, they are now on Button1, they press enter again and our
Button1 click event occurs. In this event, I decide there's something
wrong with TextBox1, give them a long winded description in the
MessageBox and send them back
to TextBox1 like this:
if(SomethingWrongWithTextBox1)
{
MessageBox.Show("Long winded") ;
Control1.Focus();
return;
}

If the user click Ok to dismiss the messagebox, or pressed the space
bar, the user is then sitting on Control1, as expected.

If the user presses Enter, the user is sitting on Control2. The reason
why is that, apparently, by design,if the user presses enter or esc in
the messagebox, it keeps that key in buffer for whatever fired the
messagebox.

So I figured that if I could clear the keyboard buffer after the
MessageBox.Show(), I wouldn't get the focusing problem.

Nov 17 '05 #5

P: n/a
Ron
Your problem is not the MessageBox button but the Textbox's KeyUp event. The
focus changes on KeyDown (from the MessageBox), so when you release the key
you are already on the textbox, and therefore the KeyUp event for the textbox
fires and changes focus to the other textbox. If you do your focus changes on
KeyDown instead, you will not have that problem.

"nx*****@winvoice.com" wrote:
The issue is that when I fire a MessageBox.Show() in my code, if the
user presses Enter or Esc to dismiss the MessageBox, the form
underneath gets a KeyUp event with that enter or esc key. I was trying
to create a wrapper around the standard MessageBox class that would
clear any keys from the buffer. So far, I've tried an IMessageFilter,
PeekMessage, and now kbhit/getwch. No matter what, anywhere I've got
MessageBox.Show(), that form is going to get those enter keys, which
are used for navigation(SendKeys(Keys.Tab) on enter). So if I pop up a
messagebox and tell the user they have an error someplace and then set
the focus to that textbox, the focus actually changes to the next
control in the tab order.

Let's say we have three controls..two textboxes and a button
TextBox1
TextBox2
Button1

I set the user on TextBox1, the user types in a number, presses enter,
the form changes their focus to TextBox2, they type in another number,
press enter, they are now on Button1, they press enter again and our
Button1 click event occurs. In this event, I decide there's something
wrong with TextBox1, give them a long winded description in the
MessageBox and send them back
to TextBox1 like this:
if(SomethingWrongWithTextBox1)
{
MessageBox.Show("Long winded") ;
Control1.Focus();
return;
}

If the user click Ok to dismiss the messagebox, or pressed the space
bar, the user is then sitting on Control1, as expected.

If the user presses Enter, the user is sitting on Control2. The reason
why is that, apparently, by design,if the user presses enter or esc in
the messagebox, it keeps that key in buffer for whatever fired the
messagebox.

So I figured that if I could clear the keyboard buffer after the
MessageBox.Show(), I wouldn't get the focusing problem.

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.