What is the prefered way of handling keyboard input in programs like games? Is directX input the easiest choice? What are the other options?
5 回答
DirectInput (if that's what you mean by directX input) has been deprecated. I, personally, find it sufficient to handle the messages sent to the WndProc of your main window. See WMINPUT, or WMKEYDOWN and WMKEYUP
EDIT: I was assuming you're making a game using Directx. If you're not, it really depends on how you're making your game to choose input. If you're using a cross-platform existing library it will have a way of handling input that's built in.
DirectInput 对键盘输入所做的所有工作都是创建一个线程并查看 WM_* 消息并增加额外线程的开销。甚至微软都说你应该只使用 WM_* 消息。我看到一些EPIC FAIL,但不是 Windows 消息队列的一部分。
It mostly depends on what framework/OS you intend to use for your game.
Options are too numerous to all list here, but include :
- XNA (Windows + XBox 360)
- DirectX (Windows)
- Flash (Cross-Platform)
- Silverlight (Should be cross-platform)
Various integrated game engines :
- Torque
- Dark gdk
- ...
Cross-Platform libraries
- Allegro
- LWJGL
- ...
Frankly, it's highly unprobable than handling input is going to be your biggest problem, so pick a solution that fits your need (in particular in terms of grahic capability/complexity) and choose your way of handling input from there.
Typically you choose the input handling method that best fits your windowing system. If you're using GLUT/SDL/SFML or any of the other windowing toolkits you'll get your input from them. Otherwise if you're rolling your own then it's between using the OS input messages or DirectInput, both of which work swimmingly for games.
If you're thinking about mouse input, do NOT use DirectInput unless you have very specific mouse ballistics you intend to apply. DirectInput mouse motion does not have the user's mouse ballisitics applied to it (set via the mouse control panel) and will feel very awkward for most users.
Nothing wrong with DirectInput. Works well and gives you direct access to the hardware via a ... er... "friendly" interface :) Polling the hardware is the only way to go if you want real responsiveness in your game.
Relying on the Windows message pump and handling windows messages is, imho, an EPIC FAIL.