WndProc覆盖(用于捕获KeyDown / KeyUp)

编程入门 行业动态 更新时间:2024-10-11 11:20:45
本文介绍了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)

本文发布于:2023-11-11 05:25:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577501.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WndProc   KeyDown   KeyUp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!