Reputation: 1
I am writing a microsoft windows program that starts in a console window and then spawns a separate gui window to do some directx rendering in.
I am trying to get the keyboard characters while the focus is on the directx window but when I type characters, getkey doesn't register that I hit anything. After some messing around, I discovered that if I change focus to the console window after pressing a key in the directx window, my program registers the keyboard hit. Do I need some special initialization of the directx window to be able to capture the keyboard hits?
Upvotes: 0
Views: 654
Reputation: 104524
I suspect kbhit listens only on the console (stdin).
Call the GetKeyboardState API to poll the keyboard state (on every frame) instead of using those other functions you referred to. This will probably be what you want and will work fine. It's what we used when I worked in a game studio.
If you want to be more event driven than polling, and you have a message pump (GetMessage/DispatchMessage loop), just can listen for some combination of WM_CHAR, WM_KEYDOWN, and WM_KEYUP on the window handle (hwnd) of your DirectX window.
And if you need focus gain/loss detection, look for WM_SETFOCUS/WM_KILLFOCUS or just use GetFocus().
Upvotes: 1