Board index » cppbuilder » Multiple-Keyboard System

Multiple-Keyboard System


2007-01-03 11:22:32 PM
cppbuilder92
Hello All:
I am trying to create a system that takes input from multiple keyboards
at once. I would like the system to know which keyboard is sending the
data so I cannot just simply use a splitter.
Has anyone come across this or does anyone have any ideas for a
solution?
The only idea I have right now is register each "RAWINPUTDEVICE" using
the windows api but I don't have a very good idea of how this is done.
Your help is much appreciated.
~fakepoo
 
 

Re:Multiple-Keyboard System

I have tried using RAWINPUTDEVICE but the compiler can't seem to find a
definition for this. I have tried including <Windows.h>and <Winuser.h>
but it does not seem to find it. Any help?
Thanks,
~fakepoo
 

Re:Multiple-Keyboard System

Having scanned over 12,000 lines of code in winuser.h, I found that the
reason that RAWINPUTDEVICE is not recognized is because of section:
#if(_WIN32_WINNT>= 0x0501)
..defined here
#endif
where _WIN32_WINNT = 0x0500.
What does this mean? Is this some kind of version number?
Thanks for future replies,
~fakepoo
 

{smallsort}

Re:Multiple-Keyboard System

This _WIN32_WINNT is defined in windows.h as 0x500. I simply changed it
to 0x501 and I got the RAWINPUTDEVICE stuff to work correctly.
I've viewed the help files for this on
msdn2.microsoft.com/en-us/library/ms674827.aspx and I still have
a few questions.
My code:
bool TMainForm::RegisterKeyboards()
{ RAWINPUTDEVICE KB[1];
KB[0].usUsagePage = 0x01;
KB[0].usUsage = 0x06; //this is for keyboard I guess???
KB[0].dwFlags = RIDEV_NOLEGACY;
KB[0].hwndTarget = NULL;
if(RegisterRawInputDevices(KB, 1, sizeof(KB[0])) == FALSE)
{ ShowMessage("Error: Keyboard Registration Failed!");
return false;
}
return true;
}
bool TMainForm::ProcessKeystroke(tagMSG& Message, char& result, int&
keyboardAddr)
{ UINT dwSize;
GetRawInputData((HRAWINPUT)Message.lParam, RID_INPUT, NULL, &dwSize,
sizeof(RAWINPUTHEADER));
LPBYTE lpb = new BYTE[dwSize];
if (lpb == NULL)
{ return false;
}
if( GetRawInputData((HRAWINPUT)Message.lParam, RID_INPUT, lpb,
&dwSize, sizeof(RAWINPUTHEADER)) != dwSize )
{ delete[] lpb;
return false;
}
RAWINPUT* raw = (RAWINPUT*)lpb;
if(raw->header.dwType == RIM_TYPEKEYBOARD)
{ if(raw->data.keyboard.Message != 256) return false;
result = (char)raw->data.keyboard.VKey;
HOW CAN I FIGURE OUT WHICH KEYBOARD THIS IS? I NEVER DEFINED AN
ADDRESS. HELP PLEASE.
}
delete[] lpb;
return true;
}
/////////////////////////////////////////////////
1) Does anyone know what the initialization values mean?...specifically
usUsagePage and usUsage. Is there a place to specify which keyboard I
would like to initialize?
2) How can I detect which keyboard is sending the character?
Thanks for any help. If anyone knows of a better newsgroup to post this
at where I could get some responses please let me know.
Thanks again,
~fakepoo
 

Re:Multiple-Keyboard System

For those of you who may need to do this eventually:
1) Registering the keyboards
// This is for all keyboards, you only need to register one keyboard to
receive
// input from all of the keyboards.
KB[0].usUsagePage = 0x01;
KB[0].usUsage = 0x06;
2) You would need to store the handles of your keyboards at the
beginning of your application.
int i;
StationKeyboard* keyboard;
UINT nDevices;
PRAWINPUTDEVICELIST pRawInputDeviceList;
if(GetRawInputDeviceList(NULL, &nDevices, sizeof(RAWINPUTDEVICELIST))
!= 0)
{ return false;
}
if((pRawInputDeviceList =
(PRAWINPUTDEVICELIST)malloc(sizeof(RAWINPUTDEVICELIST) * nDevices)) ==
NULL)
{ return false;
}
if(GetRawInputDeviceList(pRawInputDeviceList, &nDevices,
sizeof(RAWINPUTDEVICELIST)) == -1)
{ return false;
}
// Loop through all devices and save the device handles and names
for (i = 0; i < (int)nDevices; i++)
{ if(pRawInputDeviceList[i].dwType == RIM_TYPEKEYBOARD)
{ keyboard = new StationKeyboard(pRawInputDeviceList[i].hDevice);
//store the handle in the data structure
keyboard->Station = -1; //we'll use this to weed out unnecessary
keyboards later and map them
Add(keyboard); //add this keyboard to the StationKeyboardList
object
}
}
// after the job, free the RAWINPUTDEVICELIST
free(pRawInputDeviceList);
3) You need to map the keyboards to their structures. I did this by
looping through all of the stations that I had asking "Station X -
please enter a key...", then looping through all of the keyboards and
setting the Station property of the keyboard with the matching handle
equal to X.
void __fastcall TMapKeyboardForm::ApplicationEvents1Message(tagMSG
&Msg,
bool &Handled)
{ UINT dwSize;
int i;
StationKeyboard* keyboard;
AnsiString temp;
if(Msg.message == WM_INPUT)
{ Handled = true;
GetRawInputData((HRAWINPUT)Msg.lParam, RID_INPUT, NULL, &dwSize,
sizeof(RAWINPUTHEADER));
LPBYTE lpb = new BYTE[dwSize];
if (lpb == NULL)
{ return;
}
if( GetRawInputData((HRAWINPUT)Msg.lParam, RID_INPUT, lpb, &dwSize,
sizeof(RAWINPUTHEADER)) != dwSize )
{ delete[] lpb;
return;
}
RAWINPUT* raw = (RAWINPUT*)lpb;
if(raw->header.dwType == RIM_TYPEKEYBOARD &&
raw->data.keyboard.Message == WM_KEYDOWN)
{ for(i=0;i<Keyboards->Count();i++)
{ keyboard = (StationKeyboard*)Keyboards->GetStationKeyboard(i);
if(raw->header.hDevice == keyboard->DeviceHandle)
{ keyboard->Station = Step;
Step++;
break;
}
}//end for()
}//end if()
delete[] lpb;
}
else Handled = false;
if(Step == STATIONS)
{ ModalResult = mrOk;
}
else
{ temp.sprintf("Station %d: Please press a key...",Step+1);
Panel1->Caption = temp;
}
}