【问题标题】:VB 2010 Direct input with GamePadVB 2010 使用 GamePad 直接输入
【发布时间】:2011-07-26 14:31:54
【问题描述】:

我有一个带有 2 个标签的表单,第一个标签显示 USB 游戏手柄名称(一旦找到),第二个标签显示按下的按钮,这是我目前所拥有的:

 Imports Microsoft.DirectX.DirectInput

Public Class Form1
Public _device As Device
Public _state As JoystickState
Public arm As Boolean = True


Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim gameControllerList As DeviceList
    gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly)

    If (gameControllerList.Count > 0) Then

        Dim deviceInstance As DeviceInstance
        label.Text = "Found"
        For Each deviceInstance In gameControllerList
            _device = New Device(deviceInstance.InstanceGuid)
            label.Text = deviceInstance.InstanceName
            _device.SetDataFormat(DeviceDataFormat.Joystick)
            Exit For
        Next
    Else
        label.Text = "not found"
    End If
    output.Clear()
    _device.Acquire()

    Call Poll()
End Sub


Public Sub Poll()
    Dim buttons() As Byte
    Dim i As Integer = 0
    _device.Poll()
    _state = _device.CurrentJoystickState
    buttons = _state.GetButtons()
    Dim word As String
    word = BitConverter.ToString(buttons)
    output.AppendText(word)

End Sub

结束类

我在输出上看到的都是 0,这意味着键盘上按下的按钮未被检测到

有人知道我该如何解决这个问题吗?

【问题讨论】:

  • 知道了,我需要一个 _device.Acquire() 。现在已经完成了,有人知道如何检测按下的按钮吗?

标签: .net vb.net joystick


【解决方案1】:

很有趣,就像错误所说的那样:您需要在开始轮询之前获取设备。

_device.Acquire();

请注意,这只会在实际轮询函数之前发生一次。

【讨论】:

  • 是的,我也是这么想的,你知道如何检测按下了哪些键吗?
  • 当然,一旦你有了_state(顺便说一下,这是一个可怕的命名方案),你调用GetButtons,它会返回一个Byte()。然后遍历按钮数组,如果不是0,则按下它。
  • 我知道我需要将 _state.GetButtons() 设置为字节数组 Dim buttons() 因为 Byte buttons = _state.GetButtons() 会这样吗?
  • 所以我做了如下操作:buttons = _state.GetButtons() Dim word As String word = BitConverter.ToString(buttons) output.AppendText(word) 当我运行它时,我看到一堆 0,一旦我按下一个按钮,它仍然是 0,我做错了什么,你可以看到吗?
  • 您是否在每次GetButtons 调用之前轮询并获取当前 状态?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
  • 2020-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多