我最近使用Direct3D 11从MDX2.0切换到SlimDX,但我正在努力实现键盘和鼠标控制。
在MDX中,您可以使用
keyb = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
keyb.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
keyb.Acquire();然而,要设置键盘接口,SlimDX有一种不同的方法。在SlimDX中,设备是一个抽象类,相反,有一个必须通过传入DirectInput对象来初始化的键盘类,但我终生无法弄清楚如何创建DirectInput对象或它的用途。
据我所知,SlimDX的文档非常少,如果有人知道任何好的资源来学习它的特殊特性,那就太好了,谢谢。
发布于 2011-02-08 19:17:30
我是这样用的。鼠标处理是相同的。
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,因为微软推荐这样做:
虽然DirectInput是DirectX库的一部分,但自DirectX 8 (2001-2002)以来,它就没有进行过重大修订。微软建议新的应用程序为键盘和鼠标输入使用Windows消息循环,而不是DirectInput (如Meltdown 2005 slideshow1中所述),并为Xbox360控制器使用XInput而不是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);现在你可以对事件做出反应了。
发布于 2011-11-27 09:14:06
使用SlimDX.RawInput实际从hWnd (控件/窗体的句柄)中获取光标,您需要从"user32.dll“中外部函数
使用System.Runtime.Interlop和System.Drawing.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);让我们像这样得到Point:
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回调来检查处理过的消息会更好。
https://stackoverflow.com/questions/4929205
复制相似问题