I'm trying to set up keyboard and mouse controls for a space game using SlimDX and RawInput. My current code is as follows:
Device.RegisterDevice(UsagePage.Generic, UsageId.Keyboard, DeviceFlags.None);
Device.KeyboardInput += new EventHandler<KeyboardInputEventArgs>(keyboardInput);
Device.RegisterDevice(UsagePage.Generic, UsageId.Mouse, DeviceFlags.None);
Device.MouseInput += new EventHandler<MouseInputEventArgs>(mouseInput);
However I read here: http://code.google.com/p/slimdx/issues/detail?id=785 that for WPF I need to use a different overload for Device.RegisterDevice(), as well as assigning a HandleMessage using Device.HandleMessage(IntPtr message)
I've found the correct overload for RegisterDevice() which is:
RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target, bool addThreadFilter)
What I can't work out, though, is:
1) Now that I have to use a target, what am I meant to set as a target?
2) Where do I get this IntPtr message from?
This might help, I made this for a game of pong. Hope it helps
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
spaceKeyDown = true;
}
if (e.KeyCode == Keys.Up)
{
spaceKeyUp = true;
}
if (e.KeyCode == Keys.W)
{
spaceKeyW = true;
}
if (e.KeyCode == Keys.S)
{
spaceKeyS = true;
}
if (direction == 0)
{
if (e.KeyCode == Keys.Space)
{
Random rand = new Random();
direction = rand.Next(1, 5);
}
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
spaceKeyDown = false;
}
if (e.KeyCode == Keys.Up)
{
spaceKeyUp = false;
}
if (e.KeyCode == Keys.W)
{
spaceKeyW = false;
}
if (e.KeyCode == Keys.S)
{
spaceKeyS = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (spaceKeyDown == true)
{
if (p2y < picPicture.Height - Paddle2.Height)
p2y += 15;
}
if (spaceKeyUp == true)
{
if (p2y > 0)
p2y -= 15;
}
if (spaceKeyS == true)
{
if (p1y < picPicture.Height - Paddle1.Height)
p1y += 15;
}
if (spaceKeyW == true)
{
if (p1y > 0)
p1y -= 15;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With