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,caption,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,caption,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,caption,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,caption,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);
}
}