Gui Programming WM_KEYDOWN or WM_CHAR?

Started by
7 comments, last by MGB 17 years, 6 months ago
Hi All, I am starting on the GUI programming part of my game engine. It seems windows handles key press events two ways. WM_KEYDOWN gives keycodes, or you can translate the keycode (I am assuming based on your chosen keyboard layout) into character codes which gives a WM_CHAR message. I can see the WM_KEYDOWN being useful for the game controls, but sometimes I will want the user to key text into a dialog box. Should I use the WM_CHAR messages for this, or should I do the translation from WM_KEYDOWN into the character codes in my engine? I am guessing if I do this in my engine I will also have to track the status of the shift and caps locks keys also? How have others handled this? Thanks! Rael
Advertisement
I prefer WM_CHAR. It can hanle upper and lower cases, as well as UNICODE text, like Chinese.
Quote:Original post by garnet
I prefer WM_CHAR. It can hanle upper and lower cases, as well as UNICODE text, like Chinese.


Thanks. Maybe I'll try using both, similar to the way windows does. I can see the use of allowing the user to use the shift keys for controls too. I guess when dealing with a platform that has a keyboard as an input device you're always going to expect the operating system to be aware of the keyboard layout and regional settings so it's probably not 'cheating' to take advantage of the WM_CHAR message.
Using the WM_CHAR also accounts for special characters (shift function for numbers and what not) and symbols, which are otherwise generally kind of a pain to deal with.

I know only that which I know, but I do not know what I know.
You'll want to use WM_CHAR for any text input and WM_KEYDOWN for scrolling (in your textbox or something like a listbox), key combinations (like shortcuts), and game input.

Good luck!
I use both, and have an input flag which changes the behavior of a ui object (take text, or not). The topmost object's params then determines which events are passed in and which are dropped. Er, I also use KEYUP, not down. Down will auto-repeat, Up will flag one per keystroke, and for that sort of event (usually) you only want to know if a keystroke occurs.
Well the book I learnt from was OpenGL Game Programming, and that taught me to use DirectInput to handle keyboard input, instead of doing things the windows way. Anyone else have any views on this?
Microsoft's ATG says "Don't use DirectInput for Mouse and Keyboard" :)

Taking advantage of High-Definition Mouse Movement

Quote:DirectInput is a set of API calls that abstracts input devices on the system. Internally, DirectInput creates a second thread to read WM_INPUT data, and using the DirectInput APIs will add more overhead than simply reading WM_INPUT directly. DirectInput is only useful for reading data from DirectInput joysticks; however, if you only need to support the Xbox 360 controller for Windows, then use XInput instead. Overall, using DirectInput offers no advantages when reading data from mouse or keyboard devices, and the use of DirectInput in these scenarios is discouraged.
Good info Jaymar!
/me throws out troublesome DirectInput mouse crap.

This topic is closed to new replies.

Advertisement