Keyboard routines

Started by
5 comments, last by that1guy 22 years, 9 months ago
I am having trouble converting a WM_KEYDOWN message to an ascii character. I am using the template for Lesson 1 (largely modified by now, but the basics are still there) where we map the keys to a boolean array. This works great for moving around, key presses and what not, but now I want to print to the screen the actual letter the user presses for chat (to test out my networking code =)... I admit, I have never coded for windows in C before, VB suites me just fine for front-end work.. back in the "olden DOS days" we would use GetChr or some equivelant, what do we use now? I''ve noticed that for the number and letter keys the hex codes map directly to ascii so all I need to do is display it, I can figure out the shift/capslock myself, but here''s the kicker, the other keys don''t map directly (` '' ; , . / etc...).. I can''t beleive I''ll have to make a look-up table to convert these, that would mean all windows applications down at their root have to do this, there has to be some sort of function that will translate a virtual key code to an ascii value. I''ve searched the latest MSDN I have (April 2001) and I get nothing.. starting to wonder if I installed the C/++ help files hehehe.. MSDN has almost NEVER been any help when it comes to very low level stuff like this.. Oh, btw, I don''t want to use DirectInput either =) I want to keep my code as "ANSI" (portable) as possible. Thanks =) -t1g "Ah hell.. it doesn''''t work.. AGAIN!"
"Ah hell.. it doesn''t work.. AGAIN!"
Advertisement
Your code isn''t portable since you are using Windows specific function anyway (as messages WM_KEYDOWN, or any Win32 function). So go for DirectInput, it will just be as ''portable'' (or better: unportable) as any other Win32 function. This has nothing to do with the ANSI standard, it''s just a bunch of function calls either way.

I realize that using Windows messages automaticaly takes away from portability, however using DirectInput would make the job of porting much more complex, hence the "portable as possible" comment. Just trying to follow the K.I.S.S. method =)


"Ah hell.. it doesn''t work.. AGAIN!"

Edited by - that1guy on July 21, 2001 7:53:13 PM
"Ah hell.. it doesn''t work.. AGAIN!"
If you want best possible portability, then you should encapsulate your keyboard routines in a dedicated class. A DirectInput keyboard handling function is about 50 lines of code. If you want to compile it on another platform, just replace this single class. I think, this is the easiest way, especially for games, if you don''t need all this Win specific junk, just good and fast keyboard input. I have done an input library, it compiles under Windows, Linux, OS/2, DOS and BeOS. It''s around 400 lines of code.
Hey,

You don''t want to use the WM_KEYDOWN or WM_KEYUP messages to type text into the console. Use the WM_CHAR messages. They are timed to the users setup, and the wParam value simply maps to one of the virtual key codes. eg VK_DOWN, VK_UP etc... Look up in the MSDN for the full key code listing.

Does that help you??

FatalXC
For win32 input aren''t the keycodes just the same as their int/char values? for example, keycode for A is (int)''A''???

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
For the majority of the keys.. (well, A - Z and top row 1-0) yes they do map directly. With A-Z all you have to do is add 32 to the value and you have the capital of that letter, but that''s where the fun stops.. everything else is mapped completely different and have no pattern to follow, well that''s not true, the "[" "\" "]" keys have a pattern that can be followed..

I didn''t go with WM_CHAR mostly cuz I already have it working (without DirectX and will be easily portable, with about 50 lines of code I might add =) I might go back and re-do it correctly with WM_CHAR.. well.. I will.. later =) I''m on to bigger things now that it''s prototyped and working wonderfully =)

Wait.. I lied.. here''s another "funny" thing about windows.. if I press the "A" key with cap''s lock off, I get 65 in the window''s message, but if cap''s lock is on I still get 65. Now here''s the funny thing.. on the 10key, if I press 8 with numlock on I get (some value I dont'' remember right now) but if num lock is off, I get VK_UP from windows, t''would be nice if caps lock worked the same way =) To this point I keep a flag in code to toggle caps lock on/off state, but if the caps lock key was hit before my program runs, I don''t know about it.. Anyone know how I can query the state of caps lock from code? Not very important at this point, it''ll just go down in my "Feature" list.
"Ah hell.. it doesn''t work.. AGAIN!"

This topic is closed to new replies.

Advertisement