Sign in to follow this  
Sepiantum

Raw Input... How?

Recommended Posts

SimonForsman    7642
[quote name='Sepiantum' timestamp='1306607026' post='4816850']
So I'm working on a game, and I need to set up raw input somewhere. How would I do this?
[/quote]

What platform/API/Framework/Engine are you using ?

Share this post


Link to post
Share on other sites
Texus    248
I don't know which programming language u are using, but if you are working with C++ and with windows then I can help you.
If you are using another language, this won't help you much, but I will post it anyway.


I suggest you look here: [url="http://www.toymaker.info/Games/html/raw_input.html"]http://www.toymaker..../raw_input.html[/url]

I also found this code in an old project (I didn't tested it again before posting it):

Global declaration (raw input for keyboard and mouse)
[code]
RAWINPUTDEVICE Rid[2];
[/code]
When initializing:
[code]
// Keyboard
Rid[0].usUsagePage = 1; // generic desktop controls (default)
Rid[0].usUsage = 6; // keyboard
Rid[0].dwFlags = RIDEV_NOLEGACY; // no more messages like WM_KEYDOWN
Rid[0].hwndTarget = NULL; // don't restrict messages to a particular window

// Mouse
Rid[1].usUsagePage = 1; // generic desktop controls (default)
Rid[1].usUsage = 2; // mouse
Rid[1].dwFlags = RIDEV_NOLEGACY; // no more messages like WM_LBUTTONDOWN
Rid[1].hwndTarget = NULL; // don't restrict messages to a particular window

if (RegisterRawInputDevices(mApp->Rid, 2, sizeof(RAWINPUTDEVICE)) == FALSE)
MessageBoxW(NULL, L"Could not register the raw input device", "error", 0);
[/code]
Trap the WM_INPUT message in WndProc:
[code]
case WM_INPUT:
{
UINT bufferSize = 40;
BYTE *buffer= new BYTE[40]; // mouse = 40, keyboard = 32
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, (LPVOID)buffer, &bufferSize, sizeof (RAWINPUTHEADER));

RAWINPUT *raw = (RAWINPUT*) buffer;

// Check what the input is: mouse or keyboard?
if (raw->header.dwType== RIM_TYPEMOUSE) // mouse
{
// raw->data.mouse.lLastX contains the relative X position of the mouse
// raw->data.mouse.lLastY contains the relative Y position of the mouse

// (raw->data.mouse.ulButtons & RI_MOUSE_LEFT_BUTTON_DOWN) will be 0 when left mouse button is not down.

}
else if (raw->header.dwType== RIM_TYPEKEYBOARD) //keyboard
{
// Get key value from the keyboard member (of type RAWKEYBOARD)
USHORT keyCode = raw->data.keyboard.VKey;
}
}
[/code]

Share this post


Link to post
Share on other sites
Sepiantum    102
I read a few more posts about Raw Input and I found out that all you get is last action that happened. How do you transform this data into useful key_up/key_down/key_held information?

Share this post


Link to post
Share on other sites
Sepiantum    102
I encountered another problem. When using raw input for mouse input, my cursor stays busy. I know it's not my handling code because I commented out all of it and it still gives me a busy cursor. Not registering the mouse for raw input fixes the problem but I don't get to use raw input for mice. Any help?

Share this post


Link to post
Share on other sites
Sepiantum    102
It seems to me as if I have pinpointed the problem. Keeping the mouse at RIDEV_NOLEGACY will cause a constant busy cursor and will hang your process at some points. Setting that to 0 will fix the problem. But why is this?

Share this post


Link to post
Share on other sites
Zoner    232
[quote name='GregMichael' timestamp='1306704768' post='4817246']
RTFM ?
[/quote]

raw input isn't well documented.

At its core raw input messages are USB HID device commands for mouse, touch screen/tablet/touchpad, and keyboard devices. The various magic numbers match to the numbers in the USB specs for those types of HID devices.

In addition, unless you are doing a purely fullscreen D3D application, the nolegacy option for mouse input is not a good choice. Its much easier to filter the message based on the mouse location and let the normal mouse messages get sent and processed for client areas outside your game HWND, (for clicking the 'X' close button, mouse dragging your frames around, resizing the window frames etc).

Share this post


Link to post
Share on other sites
Prune    224
How much on average can one expect the latency to be improved over WM_MOUSEMOVE? And how much difference between doing GetRawInputData on WM_INPUT, vs GetRawInputBuffer? What about plain async calls to GetCursorPos? I'm assuming the latter gets the coordinates of the hardware cursor.

Share this post


Link to post
Share on other sites
kdmiller3    178
I'm confident you won't notice any performance difference between raw input and normal input as long as your message handler processes [i]all[/i] pending messages and not just the first one. Messages don't come every frame, but when they do they tend to come in batches and there are often a bunch of system-related messages that you don't specifically care about. In any case, the overhead of processing input messages is so insignificant that you're far better off putting effort into other things like the rendering system and game logic.

Share this post


Link to post
Share on other sites
shurcool    439
As far as I know, the main benefit of using raw input is for more precise, acceleration-free delta mouse movements.

[url="http://msdn.microsoft.com/en-us/library/ee418864(v=VS.85).aspx"]http://msdn.microsoft.com/en-us/library/ee418864(v=VS.85).aspx[/url]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this