SlimDX DirectInput初始化

我最近使用Direct3D 11从MDX 2.0换成了SlimDX,但我很难实现键盘和鼠标控制。 在MDX中,您可以使用
keyb = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
keyb.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
keyb.Acquire();
设置键盘界面,但SlimDX有不同的方法。在SlimDX中,Device是一个抽象类,而是有一个必须通过传入DirectInput对象来初始化的Keyboard类,但我不能在我的生活中解决如何创建DirectInput对象或它的用途。 据我所知,对于SlimDX来说,文档非常渺茫,如果有人知道任何有用的资源来学习它的特殊怪癖,那就太棒了,谢谢。     
已邀请:
我用这种方式使用它。鼠标处理是一样的。
using SlimDX.DirectInput;

private DirectInput directInput;
private Keyboard keyboard;

[...]

//init
directInput = new DirectInput();
keyboard = new Keyboard(directInput);
keyboard.SetCooperativeLevel(form, CooperativeLevel.Nonexclusive | CooperativeLevel.Background);
keyboard.Acquire();

[...]

//read
KeyboardState keys = keyboard.GetCurrentState();
但是你应该使用SlimDX.RawInput,因为Microsoft推荐它:   而DirectInput构成了一部分   DirectX库,它还没有   自DirectX 8以来进行了重大修订   (2001- 2002年)。微软推荐   新的应用程序利用了   键盘和Windows的Windows消息循环   鼠标输入而不是DirectInput(如   在Meltdown 2005中指出   幻灯片[1]),并使用XInput   而不是Xbox 360的DirectInput   控制器。 (http://en.wikipedia.org/wiki/DirectInput) rawinput鼠标样本(键盘几乎相同):
SlimDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.Mouse, SlimDX.RawInput.DeviceFlags.None);
            SlimDX.RawInput.Device.MouseInput += new System.EventHandler<MouseInputEventArgs>(Device_MouseInput);
现在您可以对事件做出反应。     
使用SlimDX.RawInput 要实际从hWnd(控件的/窗体的句柄)获取光标,您需要从“user32.dll”中获取外部函数 BOOL GetCursorPos(LPOINT lpPoint) 使用System.Runtime.Interlop和System.Drawing.Point(除非您决定改为创建POINT结构)。
[DllImport("user32.dll",CallingConvention=CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.Bool)]
internal unsafe static extern bool GetCursorPos(Point* lpPoint);
这将为您提供光标在桌面屏幕上的实际位置 接下来,您将获取lpPoint地址并将其传递给ScreenToClient(HWND hWnd,LPPOINT lpPoint),后者也返回BOOL。
[DllImport("user32.dll",CallingConvention=CallingConvention.StdCall,SetLastError=true)]
internal static extern int ScreenToClient(IntPtr hWnd, Point* p);
让我们像这样得到点:
public unsafe Point GetClientCurorPos(IntPtr hWnd, Point*p)
{
    Point p = new Point();
    if (GetCursorPos(&p))
    {
       ScreenToClient(hWnd, &p);
    }
    return p;
}
你可以使用你想要的SlimDX.RawInput.Device.MouseInput处理程序,或者你可以在WndProc的覆盖中做一些编码,这是你用来处理我们所有的WINAPI程序员都习惯的消息和繁琐的写作用它。但是,你越低,你得到的控制就越多。 就像我说你从处理程序的MouseInputEventArgs中获取所有信息但是鼠标位置。我发现通过WndProc回调检查已处理的消息会更好。     

要回复问题请先登录注册