473,387 Members | 1,891 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.

Clear keyboard buffer

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
5 20988

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
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

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
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
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 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
2
by: Don | last post by:
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.