class KeyBoardMessageFilter : IMessageFilter
{
public const int WM_KEYDOWN = 0x100;
public event KeyEventHandler GlobalKeyDown;
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_KEYDOWN)
{
if (GlobalKeyDown != null)
{
GlobalKeyDown(this, new KeyEventArgs((Keys)m.WParam));
}
return true;
}
return false;
}
}
以上是把进程中所有的键盘消息都拦截到并引发一个全局的键盘事件。调用见下:
在窗体的构造函数里,这样写:
KeyBoardMessageFilter kbmf = new KeyBoardMessageFilter();
Application.AddMessageFilter(kbmf);
kbmf.GlobalKeyDown += new KeyEventHandler(kbmf_GlobalKeyDown);
然后写全局键盘消息处理函数,例如:
void kbmf_GlobalKeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
panel1.Turn();
break;
//其他的键
}
}
{
public const int WM_KEYDOWN = 0x100;
public event KeyEventHandler GlobalKeyDown;
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_KEYDOWN)
{
if (GlobalKeyDown != null)
{
GlobalKeyDown(this, new KeyEventArgs((Keys)m.WParam));
}
return true;
}
return false;
}
}
以上是把进程中所有的键盘消息都拦截到并引发一个全局的键盘事件。调用见下:
在窗体的构造函数里,这样写:
KeyBoardMessageFilter kbmf = new KeyBoardMessageFilter();
Application.AddMessageFilter(kbmf);
kbmf.GlobalKeyDown += new KeyEventHandler(kbmf_GlobalKeyDown);
然后写全局键盘消息处理函数,例如:
void kbmf_GlobalKeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
panel1.Turn();
break;
//其他的键
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23109131/viewspace-661544/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/23109131/viewspace-661544/