Sign in to follow this  
Se1f_Distruct

[SlimDX] RawInput help!

Recommended Posts

Se1f_Distruct    100
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:
[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;
}

[/code]
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

Share this post


Link to post
Share on other sites
Se1f_Distruct    100
Bump? I'm used to using DirectInput, and cant seem to get it to act similar to it.

like: (semi psudo)

[CODE]
bool IsPressed(Key Key)
{
if (keystate.Released(Key)))
{
if (previouskey.ispressed(Key))
{
return true;
}
}
return false;
}
[/CODE]

Share this post


Link to post
Share on other sites
Starnick    1892
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 [url="http://pinvoke.net/default.aspx/user32/GetKeyboardState.html"]GetKeyboardState[/url] and mouse button state from [url="http://pinvoke.net/default.aspx/user32/GetAsyncKeyState.html"]GetAsyncKeyState[/url]. And screen position of the mouse from [url="http://pinvoke.net/default.aspx/user32/GetCursorPos.html"]GetCursorPos[/url] So raw input isn't your only alternative to direct input, or tapping into windows messages.

Share this post


Link to post
Share on other sites
Se1f_Distruct    100
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!
:)

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