Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi everyone, I've tried to capture keyboard event (and the key data of course) for a specified form through a NativeWindow class by handling its WndProc function. For the first, I tried at the WndProc function of the form directly, and it works fine. And then, when I tried through NativeWindow, the WM_KEY* and WM_SYSKEY* never fired in its WndProc, but messages like mouse message is fired :

C#
// Using NativeWindow
public class FormHook : NativeWindow {
    // some implementation
    protected override void WndProc(ref Message m) {
        switch (m.Msg) {
            // others messages that works on hooked form e.g : 
            // mouse message, paint message, etc.
            // fired perfectly.
            case 0x0100:	// WM_KEYDOWN
            case 0x0104:	// WM_SYSKEYDOWN
            case 0x0101:	// WM_KEYUP
            case 0x0105:	// WM_SYSKEYP
                // these event never fired.
                break;
            default:
                base.WndProc(ref m);
                break;
        }
    }
}

// Using Form
public class AForm : Form {
    // some implementation
    protected override void WndProc(ref Message m) {
        switch (m.Msg) {
            // others messages e.g : mouse message, paint message, etc.
            // fired perfectly.
            case 0x0100:	// WM_KEYDOWN
            case 0x0104:	// WM_SYSKEYDOWN
            case 0x0101:	// WM_KEYUP
            case 0x0105:	// WM_SYSKEYP
                // these event fired perfectly.
                break;
            default:
                base.WndProc(ref m);
                break;
        }
    }
}


This is the similar question by others on different forum.

Is there anything why only WM_KEY* and WM_SYSKEY* (as far as I know) that doesn't fired when using NativeWindow ? Or is there a way to catch keyboard event on a target form besides that way ?
Posted
Comments
Sergey Alexandrovich Kryukov 3-Jul-12 11:57am    
Why would you ever need to do it in Windows-specific way, breaking platrform compatibility? Why not using .NET events?
--SA

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