Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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);
        }
    }
}


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?

Edit: 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.
Posted
Updated 3-Feb-13 21:00pm
v3

1 solution

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
 
Share this answer
 
Comments
Trapper-Hell 3-Feb-13 15:11pm    
Your project seems really detailed and should also prove to be helpful for my application. However, can you kindly point out what I have done wrong or why this is not working? Thanks
Sergey Alexandrovich Kryukov 3-Feb-13 18:42pm    
It seems impractical as it is implemented in ad-hoc way and so can only be useful as some research step. As such, it should work. If you override this method for a right class though... What class is that, by the way?
—SA
Trapper-Hell 4-Feb-13 3:01am    
I updated the question to include the complete code of the class (form). There is nothing much to it really, since it's just meant as a simple test. I will use your application, since it seems really useful, but for now I'm just trying to understand why the WndProc is never having a Msg equal to the set of integers I defined.

Thanks
Sergey Alexandrovich Kryukov 4-Feb-13 3:16am    
Looks correct, but I believe it's only one part of partial class. Do you have other controls, the form's children? It can be the problem. As some of them can get keyboard focus, the keyboard events can be dispatched to some child instead of the form. To make sure everything else is correct, try a form without any children. If that works, try Form.KeyPreview...
—SA
Trapper-Hell 4-Feb-13 3:41am    
Correct! I removed the textbox and the WndProc started working correctly (showing the 'Test' message box).

I have then re-inserted the textbox and enabled the form's KeyPreview property, however it is still not capturing the keys in the WndProc. Is there a way to capture the keys in WndProc while having the textbox focused?

Thanks a lot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900