Vahokif's picture

Low-level keyboard

I've been writing a 3D browser using OpenTK and Awesomium. Everything works great, except the function used to simulate keyboard events: void injectKeyboardEvent(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam). I tried using SetWindowsHookEx with WH_GETMESSAGE to catch keyboard messages to GameWindow and pass them on, but all I got are CHAR messages and bogus results. What would be the best way to send GameWindow keyboard events to the browser?

Inline Images


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

WM_KEYDOWN/WM_KEYUP messages are intercepted by the IInputDriver before they reach GameWindow, so this approach probably won't work. Try installing a global WH_KEYBOARD_LL hook.

Vahokif's picture

I'd prefer to write most of the app in C# which means I can't use global hooks, and I'd like to capture some messages that KEYBOARD_LL doesn't. Also, I need the HWND of the GameWindow. Can I modify OpenTK's keyboard input somehow so I have access to the raw messages?

the Fiddler's picture
  • You can write global hooks in pure C#: this example implements a WH_KEYBOARD_LL.
  • You can obtain the GameWindow handle through the WindowInfo.Handle property.
  • The current keyboard driver resides in Source/OpenTK/Platform/Windows/WMInput.cs, but modifying that is not advisable - OpenTK input will be completely rewritten over the next few weeks (it will become independent from the GameWindow).

Fortunately, there may be a simpler method to implement this functionality: hook GameWindow.KeyDown/KeyUp and synthesize WM_KEYDOWN/WM_KEYUP events for the browser via PostMessage.