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?
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.