问题描述
嘿所有,
我觉得这真的很愚蠢,但是我无法让它发挥作用。我需要捕获C#应用程序中的KeyDown / KeyUp事件以确定密钥的ScanCodes和虚拟密钥代码。
我简单地覆盖了WndProc方法并检查了Msg等于KeyDown / KeyUp事件之一。代码如下所示:
Hey all,
I''m feeling really stupid asking this, but I just can''t get it to work. I need to capture the KeyDown / KeyUp events in a C# application to determine the ScanCodes and Virtual Key Codes of the keys.
I simply overrided the WndProc method and checked if the Msg is equal to one of the KeyDown / KeyUp events. The code is shown below:
using System.Windows.Forms;
namespace ProcTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const int WM_KEYDOWN = 0x0100, WM_KEYUP = 0x0101, WM_CHAR = 0x0102, WM_SYSKEYDOWN = 0x0104, WM_SYSKEYUP = 0x0105;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_KEYDOWN || m.Msg == WM_KEYUP || m.Msg == WM_CHAR || m.Msg == WM_SYSKEYDOWN || m.Msg == WM_SYSKEYUP)
{
MessageBox.Show("Test");
}
base.WndProc(ref m);
}
}
}
该应用程序是标准的Windows窗体 - 带有简单的文本框在它(允许我输入它),但我不能让它工作。正在调用WndProc方法,但是没有Msg似乎与定义的整数集匹配(因此,''Test''消息框永远不会显示)。
我缺少什么?
编辑:我知道我可以使用TextBox事件,但我相信捕获密钥的最佳方法扫描代码和虚拟密钥代码是通过WndProc。
The application is a standard Windows Form - with a simple textbox in it (to allow me to type in it), however I can''t get it to work. The WndProc method is being called, however no Msg seems to match the defined set of integers (therefore, the ''Test'' message box is never shown).
What am I missing?
I know I can use the TextBox events, but I believe that the best way to capture the keys Scan Codes and Virtual Key Codes is through the WndProc.
推荐答案
这篇关于WndProc覆盖(用于捕获KeyDown / KeyUp)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!