Keyboard parsing

Started by
7 comments, last by MichaelNett 12 years, 10 months ago
Hello

At the moment I am using the following code (DX/c++) to parse keystrokes


#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
if(KEY_DOWN(VK_F))
//handle


This code resulted in multiple "F"s spamming my screen because the key is held down multiple frames. So I resorted to make a BOOL to detect if it has already been parsed. If the key is released the BOOL is toggled back. So I have a very cumbersome code where each key has a BOOL and is checked with an IF statement.

My parser is limited by framerate etc. Most keyboard parsers also have a function where if you press a key down for more then 1 second it will add more letters, this won't work with my current code. If I were to implement it the way it is now I must add atleast 10 lines of code for each key.

Any suggestions? How does windows capture input in textboxes, can this be ported to DirectX?
Advertisement
http://msdn.microsoft.com/en-us/library/ms646276%28v=vs.85%29.aspx
Is this how "big titles" catch user input?

Is this how "big titles" catch user input?


Why wouldn't they use functionality that is already implemented, works fine, and has no bugs?

[quote name='Tispe' timestamp='1306776245' post='4817578']
Is this how "big titles" catch user input?


Why wouldn't they use functionality that is already implemented, works fine, and has no bugs?
[/quote]

is'nt DirectInput / Xinput better ?

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

Ok then. Just another quick one before the thread drops into the abyss.

Would you PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) once per frame or empty it completly before moving on?


while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
//handle


Or can this cause hiccups?

[quote name='JTippetts' timestamp='1306779481' post='4817596']
[quote name='Tispe' timestamp='1306776245' post='4817578']
Is this how "big titles" catch user input?


Why wouldn't they use functionality that is already implemented, works fine, and has no bugs?
[/quote]

is'nt DirectInput / Xinput better ?
[/quote]

Not really. DirectInput has basically been deprecated since DX 8. Microsoft recommends XInput for 360 controllers, but standard Windows messages for keyboard and mouse. (Source: http://en.wikipedia.org/wiki/XInput ) There really is no reason to use other than Windows messages for character input on Windows. Some APIs, though, will throw a wrapper around the Windows messages so you don't have to actually deal with them, but underneath they're probably processing WM_CHAR and friends.

Ok then. Just another quick one before the thread drops into the abyss.

Would you PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) once per frame or empty it completly before moving on?


while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
//handle


Or can this cause hiccups?

You actually HAVE to empty the message pump completely or you end up with "lagging" window effects (e.g. when dragging the window around).

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>


My parser is limited by framerate etc. Most keyboard parsers also have a function where if you press a key down for more then 1 second it will add more letters, this won't work with my current code. If I were to implement it the way it is now I must add atleast 10 lines of code for each key.


Do you have some kind of time stamp available (a tick count for example). Then just save time stamps instead of bools. Then you can easily find out how long a key has been pressed by checking against the current tick count.

- Michael.


This topic is closed to new replies.

Advertisement