🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

GetAsyncKeyState or direct input?

Started by
6 comments, last by bosjoh 24 years, 5 months ago
Should I use GetAsyncKeyState for my keyboard input or direct input? I am working in a full-screen direct draw videomode, if that''s important.
Advertisement
I would use Direct input if I were you. With DI, you only have to call GetDeviceState once per frame, then you have the key data for that frame, instead of making a call (with overhead) every time you need to check a key.

-----------------------------
"And I write and I write and I don't believe it's gonna change today"
- From Yellow Ledbetter by Pearl Jam

http://www.crosswinds.net/~uselessknowledge
I would recommend using Direct Input for your main input, and for any typing the user needs to enter into your game you should probally use the Wchar msg''s.
thanks, I will use DI then.
You don't have to call GetDeviceState every frame. That's too much overhead too. Use event notification. Then you will be notified when a keypress occured and you have more processor time available for other important things like AI.
Just do something when there is something to do. Polling techniques are never a good solution under multitasking/multithreading OS's like WIN32.

Edited by - VirtualNext on 1/10/00 4:32:56 AM
To do event handling should I use the windowmessagehandler or another thing?
Hi !!

I would go with Direct Input. It''s really simple to set up and works fine. Well, polling is not always the right way, but I would poll in games with a lot of keypresses. For example if you play Quake3 or UT (and all the other FPS games) you nearly have a key pressed in every frame (moving !!). With such a steering polling is better. Flight simulators or Racing games just need key presses only a few times, there I would go with events.

Events are only useful if an event occurs sometimes but not all the time.

Phillip
bosjoh: To do event handling with DirectInput you have to create an event like this:

HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

Before your program terminates you have to call:

pKeyb->SetEventNotification(NULL);
CloseHandle(hEvent);

To setup event handling call:

// pKeyb is of type IDirectInputDevice7* and already properly initialized

pKeyb->SetEventNotification(hEvent);

Than you will be notified when a keypress or release occured. You can react on those events in threads using WaitForSingleObject or WaitForMultipleObjects or you can do it without threads in your message loop with MsgWaitForMultipleObjects. Please look in the WIN32 online help for further details and to become familiar with.

Phillip: I don't aggree. It's right that DirectInput won't notify you every frame if a key is still depressed like polling techniques do but you don't need to call GetDeviceState or GetDeviceData every frame just to know if a key is still depressed. With events you'll be notified if a key is pressed and when a key is released. This is very enaugh information even to move every frame (which a good programmer won't do). You move over time and not every single frame or every ten frames or something like that because otherwise your game speed will be dependent of the actual fps rate (100 fps = faster game speed, 25 fps = slower game speed). So you just have to save the current state (pressed/released) of a key in a variable and when it's time to move you simply check if the appropriate key is pressed and if so you move. If the key was released in the meantime you're notified and simply update the appropriate variable. This technique will save you a lot of processor time for other more important things like AI for example.

NOTE: If you are using threads to wait for an event then while the thread is waiting it will consume very little processor time (almost nothing).

Edited by - VirtualNext on 1/13/00 4:51:24 AM

Edited by - VirtualNext on 1/19/00 3:23:45 AM

This topic is closed to new replies.

Advertisement