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);
}
}
}
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.