I'm sorry for replying late.I have solved my problem by using RawInput.And I will paste my unfinished rawinput class here
Hey,Krohm,I really thank you for your reply.I want to make a game that allowing high frequency input with keyboard, I don't know if raw input can handle that. I havn't tried that before, just feel like maybe DirectInput is more efficient for its complexity? Anyway,I will try raw input.
As far as I know DirectInput is something that works but it is almost deprecated. Basically DirectInput is just reading the windows messages for input, which you could do also by yourself. Raw input should be slightly more efficient than DirectInput.
Cheers!
I have read a lot about keyboard input on MSDN, I think you are right, thank you.
I want to make a game that allowing high frequency input with keyboard, I don't know if raw input can handle that.
I don't know what you mean by "high frequency". But I can tell you it has enough throughput to manage hi-dpi mices and wiimotes. I don't think keyboards will cause trouble. I cannot really tell, I never had any trouble with them and I can guarantee you I have bashed my keyboards as fast as humanly possible.
Keep in mind that many keyboards cannot read many buttons at the same time because of an hardware limitation.
If you find that useful, do you mind rating up messages? Hopefully this will also help other people in pinpointing information with ease.
I can handle multiple keyboards inputs now, and thank you for let me knowing the raw input API. Here is the code for handling multiple keyboards with raw input. The principles are basically the same for other devices, like mouse,game controll,etc.
[code]
//RawInput.h
#include <Windows.h>
#include <vector>
class RawInput
{
public:
struct KeyboardArgs
{
UINT index; //index in keyboards vector
HANDLE handle; //handle to differentiate devices
UINT vk; //key
};
//enum DeviceType
// {
// DT_KEY,
// DT_MOUSE,
// DT_OEM
// };
//keystroke events, call in ProcessKeyStroke
typedef void (*KeyEventHandler)(KeyboardArgs);
KeyEventHandler KeyDown;
//those 3 event havn't finished
KeyEventHandler KeyPressed;
KeyEventHandler KeyReleased;
KeyEventHandler KeyUp;
private:
HWND hwnd; //the handle for dest window
std::vector<KeyboardArgs> keyboards;
public:
RawInput(HWND h):hwnd(h){}
bool RegisterKeyboard()
{
RAWINPUTDEVICE Rid[1];
Rid[0].usUsagePage = 0x01;
Rid[0].usUsage = 0x06;
Rid[0].dwFlags = 0;
Rid[0].hwndTarget = hwnd;
if (RegisterRawInputDevices(Rid, 1, sizeof(Rid[0])) == FALSE) {
MessageBox(hwnd, "RegisterRawInputDevices() - FAILED","Error",0);
return false;
}
return true;
}
void RegisterKeyEventHandler( //register for event
KeyEventHandler keyPressFunc,
KeyEventHandler keyDownFunc = 0,
KeyEventHandler keyReleaseFunc = 0,
KeyEventHandler keyUpFunc = 0)
{
KeyPressed = keyPressFunc;
KeyDown = keyDownFunc;
KeyReleased = keyReleaseFunc;
KeyUp = keyUpFunc;
}
//this function will enumerate all the devices
int EnumerateDevices()
{
int NumberOfKeyboards = 0;
UINT deviceCount = 0;
if(GetRawInputDeviceList(NULL, &deviceCount, sizeof(RAWINPUTDEVICELIST)) != 0)
{
MessageBox(hwnd, "GetRawInputDeviceList() - First Call Failed","Error",0);
return -1;
}
RAWINPUTDEVICELIST* rid = new RAWINPUTDEVICELIST[deviceCount];
if(GetRawInputDeviceList(rid, &deviceCount, sizeof(RAWINPUTDEVICELIST)) != deviceCount)
{
MessageBox(hwnd, "GetRawInputDeviceList() - Second Call Failed","Error",0);
return -1;
}
//store all the keyboards handles, and give them indices for identification in game logic
for(int i = 0; i < deviceCount; i++)
{
if (rid[i].dwType == RIM_TYPEKEYBOARD )
{
KeyboardArgs kb = {NumberOfKeyboards,rid[i].hDevice};
NumberOfKeyboards++;
keyboards.push_back(kb);
}
}
delete[] rid;
return NumberOfKeyboards;
}
void ProcessKeyStroke(LPARAM lParam)
{
UINT dwSize;
if(GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize,
sizeof(RAWINPUTHEADER)) != 0)
{
MessageBox(hwnd,"GetRawInputData() - First Call Failed","Error",0);
return;
}
LPBYTE lpb = new BYTE[dwSize];
if (lpb == NULL) return;
if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize,
sizeof(RAWINPUTHEADER)) != dwSize )
{
MessageBox(hwnd,"GetRawInputData() - Second Call Failed","Error",0);
return;
}
RAWINPUT* raw = (RAWINPUT*)lpb;
if (raw->header.dwType == RIM_TYPEKEYBOARD)
{
if(raw->data.keyboard.Message == WM_KEYDOWN || raw->data.keyboard.Message == WM_SYSKEYDOWN)
{
USHORT key = raw->data.keyboard.VKey;
if (key > 0xFE) return;
//a example for call a event
if(KeyDown != NULL)
{
//iterate through all the devices have been enumerated
for(int i = 0; i < keyboards.size();i++)
{//search the keyboard in enumerated keyboards
if(raw->header.hDevice == keyboards[i].handle)
{
keyboards[i].vk = key; //change the current key code
KeyDown(keyboards[i]); //call event
}
}
}
}
}
delete[] lpb; //release the rawinput resources
}
};
//---usage
RawInput* input; //global pointer
//init and register callback functions for rawinput somewhere
void init()
{
input = new RawInput(d3d->WindowHandle());
int numkeyboards = input->EnumerateDevices();
input->RegisterKeyEventHandler(KeyPressed,KeyDown);
input->RegisterKeyboard();
}
LRESULT CALLBACK WndProc(
HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
switch (msg)
{
case WM_INPUT:
input->ProcessKeyStroke(lParam);
break;
}
}
//an example for KEYDOWN callback
void KeyDown(RawInput::KeyboardArgs args)
{
strstm.str("");
strstm <<"keyboards index:"<< args.index;
}
ok, that's how it looks like. I haven't finish it yet, and still needs to improve it a lot.Anyway, I really hope my post can help someone who also have this problem.
Edited by AntiMight, 06 October 2012 - 11:41 AM.