2011-07-26 59 views
0

我一張有2個標籤形式直接輸入,第一個標籤顯示USB遊戲手柄名稱(一旦發現)第二我想顯示的按鈕按下時,這裏是我到目前爲止有:VB 2010的GamePad

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的上輸出,這意味着被推向小鍵盤上的按鍵的arent被檢測

任何人都知道我可以解決這個問題?

+0

得到了它,我需要一個_device.Acquire()在那裏。現在,這樣做有沒有人知道如何檢測按鈕推? –

回答

2

滑稽的是,它是完全一樣的錯誤說:你需要開始輪詢之前獲取設備。

_device.Acquire(); 

請注意,這隻發生在實際的輪詢功能之前。

+0

是啊,我只是想出一樣,你會知道如何檢測哪些鍵按下? –

+0

當然,一旦你有你'_state'(可怕的命名方案的方式),你叫'GetButtons'它返回一個'字節()'。然後迭代按鈕數組,如果它不是'0',則按下它。 – Blindy

+0

我知道我需要設置_state.GetButtons()爲一個字節數組 暗淡按鈕()作爲字節 按鈕= _state.GetButtons() 將類似的東西的工作? –