Theres got to be a better way

Recommended Posts

chxfryer    138
There has to be a better way to do this, or more efficeint (spelling?) way. Right now, to get input for a chat program im writing, i check each and every key for a press through DirectInput; if a key is pressed, it returns the character for that key. ie. if (KeyDown(DIK_A)) return 'A'; if (KeyDown(DIK_B)) return 'B'; if (KeyDown(DIK_LSHIFT)) shift_down = true; if ((KeyDown(DIK_COMMA) && (shift_down)) return '<'; ... KeyDown is a is just a dummy function to return a keypress. is there someother way to quickly check the keys and return the character? Any thoughts/suggestions would be appreciated. Thanks

Share on other sites
Dave Hunt    4872
For text input, Microsoft recommends using Windows messages (WM_CHAR/WM_KEYDOWN), instead of DirectInput. The WM_CHAR message covers all of the "regular" text keys. You would only need WM_KEYDOWN for cursor keys.

Share on other sites
dalep    331
If you really wanted to do something like that, you could set up a map of some sort, that mapped DIK constants to VK constants (VK is the set of constants used in the Windows API WM_KEYxxx messages and related places). It would tidier, I suppose.

But like Dave Hunt says, you'll make your life a lot easier if you use a regular message proc for this. The TranslateMessage part of the TranslateMessage/DispatchMessage loop will take care of all that shift logic for you, for example.

Share on other sites
evanofsky    2913
Inside your WndProc function, you'd do something like this:
switch( msg ){	case WM_KEYDOWN:	keyPressedID = wParam;	break;}// later use keyPressedID to find out which key was pressed

Share on other sites
chxfryer    138
Thanks for the input. Would the wm_char be decent for text input in a game? Basically, i am just making this chat program as a learning experience for a multiplayer game im wanting to develop.

Share on other sites
Dave Hunt    4872
Yes, you can use WM_CHAR for text input in a game. You would not want to use it for keyboard game controls, but if you are doing things like getting the player's name, etc., then WM_CHAR is the way to go.