c# - SlimDX DirectInput 初始化

标签 c# directx slimdx managed-directx 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();

设置键盘接口(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/

相关文章:

使用 ServiceStack/ORMLite 进行 C# 字符串修剪

c# - 如何使用带有 WPF C# GUI 的跨平台 C++

C# 通用部分类、接口(interface)和继承,都在同一个问题中! (天啊!)

c# - Swapchain.Present() 花费的时间太长,导致滞后

c# - SlimDX加载预编译着色器

wpf - VB.Net 中的 2d 图形应该使用什么技术? WPF、DirectX、SlimDX?

c# - ReactiveUI:如何从 ReactiveCommand 取消 Observable?

c# - 在 Visual Studio 中安装 Directx 和 Direct3D

c++ - 顶点着色器中的 Direct3D11 错误顶点变换

c# - 如何在 C# 中使用 "install"Directx(或其他图形 SDK)?