3

我正在尝试使用 SlimDX 和 RawInput 为太空游戏设置键盘和鼠标控件。我目前的代码如下:

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);

但是我在这里读到:http ://code.google.com/p/slimdx/issues/detail?id=785 ,对于 WPF,我需要使用不同的重载Device.RegisterDevice(),以及使用分配 HandleMessageDevice.HandleMessage(IntPtr message)

我发现 RegisterDevice() 的正确重载是:

RegisterDevice(UsagePage usagePage, UsageId usageId, DeviceFlags flags, IntPtr target, bool addThreadFilter)

但是,我无法解决的是:

1)既然我必须使用一个目标,我打算将什么设置为目标?

2) 我从哪里得到这个 IntPtr 消息?

4

1 回答 1

2

这可能会有所帮助,我做这个是为了打乒乓球。希望能帮助到你

            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;
        }
    }
于 2013-04-19T18:03:37.133 回答