WndProc覆盖(用于捕获KeyDown / KeyUp) [英] WndProc Override (for Capturing KeyDown / KeyUp)

查看:70
本文介绍了WndProc覆盖(用于捕获KeyDown / KeyUp)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,



我觉得这真的很愚蠢,但是我无法让它发挥作用。我需要捕获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.

推荐答案

这个问题的完整解决方案显示在我的CodeProject文章附带的演示应用程序中:动态方法调度程序 [ ^ ]。



如果您需要从事件中提取更多信息,您可以轻松修改我的代码。它很容易做到,因为它不像你那样以特别的方式编写。 :-)。



干杯,

-SA
The solution of this problem in full is shown in the demo application which comes with my CodeProject article: Dynamic Method Dispatcher[^].

If you need to extract more information from the events, you can easily modify my code. It would be easy to do, because it is not written in such an ad-hoc style as yours. :-).

Cheers,
—SA


这篇关于WndProc覆盖(用于捕获KeyDown / KeyUp)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆