I add this for anyone who has the same problem. Keyboardhooks are global, one cannot hook to a specific control. What you need to do is capture the messages of the given handle. To do so, you have to subclass your window/handle.
[DllImport("user32")]
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, Win32WndProc newProc);
[DllImport("user32")]
private static extern int CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, int Msg, int wParam, int lParam);
// A delegate that matches Win32 WNDPROC:
private delegate int Win32WndProc(IntPtr hWnd, int Msg, int wParam, int lParam);
// from winuser.h:
private const int GWL_WNDPROC = -4;
private const int WM_KEYDOWN = 0x0100;
// program variables
private IntPtr oldWndProc = IntPtr.Zero;
private Win32WndProc newWndProc = null;
private void SubclassHWnd(IntPtr hWnd)
{
// hWnd is the window you want to subclass..., create a new
// delegate for the new wndproc
newWndProc = new Win32WndProc(MyWndProc);
// subclass
oldWndProc = SetWindowLong(hWnd, GWL_WNDPROC, newWndProc);
}
private const int ENTER_KEY = 1835009;
// this is the new wndproc, just show a messagebox on left button down:
private int MyWndProc(IntPtr hWnd, int Msg, int wParam, int lParam)
{
switch (Msg)
{
case WM_KEYDOWN:
int vkCode = lParam;
if (vkCode == ENTER_KEY)
doSomething();
return 0;
default:
break;
}
return CallWindowProc(oldWndProc, hWnd, Msg, wParam, lParam);
}