[SlimDX] RawInput help!

Started by
2 comments, last by Se1f_Distruct 12 years, 8 months ago
How would I go about getting user input (mouse and keyboard) using SlimDX and RawInput? Is RawInout the best way of doing it?
Ive tried DirectInput and switched to RawInput. Searched google, facepunch, and here.

I have this basic code:

private Dictionary<int, KeyState> KeysPressed = new Dictionary<int, KeyState>();
private Dictionary<int, KeyState> KeysPreviouslyPressed = new Dictionary<int, KeyState>();

private Dictionary<int, bool> MousePressed = new Dictionary<int, bool>();
private Dictionary<int, bool> MousePreviouslyPressed = new Dictionary<int, bool>();
Vector2 CurrentMousePos;
Vector2 PreviousMousePos;
public void InitializeInput()
{
Device.RegisterDevice(SlimDX.Multimedia.UsagePage.Generic, SlimDX.Multimedia.UsageId.Keyboard, DeviceFlags.None);
Device.KeyboardInput += new EventHandler<KeyboardInputEventArgs>(KeyboardInput);

Device.RegisterDevice(SlimDX.Multimedia.UsagePage.Generic, SlimDX.Multimedia.UsageId.Mouse, DeviceFlags.None);
Device.MouseInput += new EventHandler<MouseInputEventArgs>(MouseInput);

}
void KeyboardInput(object sender, KeyboardInputEventArgs e)
{
if (!KeysPressed.ContainsKey((int)e.Key))
{
KeysPressed.Add((int)e.Key, e.State);
}


}
void MouseInput(object sender, MouseInputEventArgs e)
{
CurrentMousePos = new Vector2(e.X, e.Y);

if ((e.ButtonFlags & MouseButtonFlags.LeftDown) == MouseButtonFlags.LeftDown)
{
if (!MousePressed.ContainsKey(0))
{
MousePressed.Add(0, true);
}
else MousePressed[0] = true;
}


if (!MousePressed.ContainsKey(1))
{
if ((e.ButtonFlags & MouseButtonFlags.RightDown) == MouseButtonFlags.RightDown)
{
MousePressed.Add(1, true);
}
}
}
public Point GetMouseMovement()
{
return new Point((int)CurrentMousePos.X, (int)CurrentMousePos.Y);
}
public bool IsLeftMouseDown()
{
if (MousePressed.ContainsKey(0))
{
if (MousePressed[0] == true)
{
return true;
}
}
return false;
}
public void Refresh()
{
KeysPreviouslyPressed = KeysPressed;
KeysPressed = new Dictionary<int, KeyState>();


MousePreviouslyPressed = MousePressed;
PreviousMousePos = CurrentMousePos;
CurrentMousePos = Vector2.Zero;
}
public bool IsKeyDown(Keys Key)
{
if (KeysPressed.ContainsKey((int)Key))
{
if (KeysPressed[(int)Key] == KeyState.Pressed)
{
return true;
}
}
return false;
}


Refresh is called after the main game loop Update() routine.

I need the basic ability to tell if a key is pressed or not right when i ask...like (DirectInput) Keyboardstate k = Keyboard.GetState();

What am I doing wrong? :o

This code is ugly, and will be cleaned when in a working state, but I just need it to work for now.
Like IsKeyPressed(Key.Space) returning the corresponding bool value.

Any questions, comments, and concerns are appreciated.

Thanks!
- Se1f_Destruct

First Post.:D
Advertisement
Bump? I'm used to using DirectInput, and cant seem to get it to act similar to it.

like: (semi psudo)


bool IsPressed(Key Key)
{
if (keystate.Released(Key)))
{
if (previouskey.ispressed(Key))
{
return true;
}
}
return false;
}
Couple of thoughts:

1) Raw Input is event-based, so you're going to pick up when you release keys too. So no need to have to refresh it (or clear it really). Not to mention, if you're calling Refresh() every frame - you're creating a new Dictionary object every frame, which is a bad thing to do since you're going to be accumulating a lot of dead objects and causing the garbage collector to invoke more often.

2) The mouse x,y values you get aren't absolute coordinates. They're changes in the motion of the mouse, so it's a delta value. Same goes with the wheel value delta. So to get absolute mouse coordinates, you'll have to keep a running sum of the x,y coordinates and keep adding what you get from the mouse event. E.g. when you startup, get the current mouse position and set this as your initial position, then as you move the mouse the event will get positive/negative x,y values that you add to this initial position.

The explanation for why this is, is because it's raw input from a device that has no idea what the monitor is or its coordinate system or resolution.

3) You can just as well get keyboard state from the the appropriate P/Invoke calls to GetKeyboardState and mouse button state from GetAsyncKeyState. And screen position of the mouse from GetCursorPos So raw input isn't your only alternative to direct input, or tapping into windows messages.
1. I figured as much, it wouldnt be as easy as DInput... :( And it was rough code, so itwould have been rewritten.

2. Mouse mickeys, same with DInput..

3. This was what I was trying to do, I just didnt realize it was so simple. I think this is my best choice to collect input.

Thanks for the help! I really appreciate it!
:)

This topic is closed to new replies.

Advertisement