我最近使用 Direct3D 11 从 MDX 2.0 切换到 SlimDX,但我在实现键盘和鼠标控制方面遇到了困难。
在 MDX 中您可以使用
keyb = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
keyb.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
keyb.Acquire();
设置键盘接口(interface),但是SlimDX有不同的方法。在 SlimDX 中,Device 是一个抽象类,而是有一个 Keyboard 类,必须通过传入 DirectInput 对象来初始化,但我一生都无法弄清楚如何创建 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 推荐它:
While DirectInput forms a part of the DirectX library, it has not been significantly revised since DirectX 8 (2001-2002). Microsoft recommends that new applications make use of the Windows message loop for keyboard and mouse input instead of DirectInput (as indicated in the Meltdown 2005 slideshow[1]), and to use XInput instead of DirectInput for Xbox 360 controllers.
(http://en.wikipedia.org/wiki/DirectInput)
原始输入鼠标示例(键盘几乎相同):
SlimDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.Mouse, SlimDX.RawInput.DeviceFlags.None);
SlimDX.RawInput.Device.MouseInput += new System.EventHandler<MouseInputEventArgs>(Device_MouseInput);
现在您可以对事件使用react。
关于c# - SlimDX DirectInput 初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4929205/