2013-11-01 64 views
0

到目前爲止的代碼:檢測手柄輸入

Device gamepad; 
public bool initializeGamePad() 
     { 
      foreach (DeviceInstance di in Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly)) 
      { 
       gamepad = new Device(di.InstanceGuid); 
       break; 
      } 

      if (gamepad==null)//no gamepads detected 
       return false; 
      else 
      { 
       configureGamePad(); 
       return true; 
      } 
     } 

     public void configureGamePad() 
     { 
      //Set axis ranges 
      foreach (DeviceObjectInstance doi in gamepad.Objects) 
      { 
       if ((doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0) 
       { 
        gamepad.Properties.SetRange(ParameterHow.ById, doi.ObjectId, new InputRange(-5000, 5000)); 
       } 
      } 

      //Set joystick axis mode absolute 
      gamepad.Properties.AxisModeAbsolute = true; 

      //set cooperative level. 
      gamepad.SetCooperativeLevel(new Form1(), CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Background); 

      //Acquire devices for capturing 
      gamepad.Acquire(); 

      UpdateJoystick(); 
     } 

     private void UpdateJoystick() 
     { 
      string info = "Joystick: "; 

      //Get Mouse State 
      JoystickState state = gamepad.CurrentJoystickState; 

      //Capture Position 
      info += "X:" + state.X + " "; 
      info += "Y:" + state.Y + " "; 
      info += "Z:" + state.Z + " "; 
      info += "ARx:" + state. + "\n"; 

      //Capture Buttons 
      byte[] buttons = state.GetButtons(); 
      for (int i = 0; i < buttons.Length; i++) 
      { 
       if (buttons[i] != 0) 
       { 
        info += "Button:" + i + " "; 
       } 
      } 

      MessageBox.Show(info); 
     } 

的問題是,信息字符串包含只值0的問候按鈕state.X/Y/Z並沒有什麼。

我需要這樣的:button_down & button_release爲了得到2個或更多的同時按鈕按下。和軸pozisions。

此外,我只使用DirectX SKD沒有SlimDX或任何其他。

+0

一般來說,遊戲手柄輸入是用於遊戲。我只通過XNA使用了遊戲手柄。你能指定你使用的是什麼類型的遊戲手柄嗎? Xbox控制器,飛行操縱桿,通用D-Pad /模擬/ 4Button /觸發器? –

+0

我正在使用無線天才遊戲手柄 http://s1.emagst.ro/products/24/23618/images/img50860_02092009105416_350x350c_71dt.jpg –

回答

1

您可能必須以託管形式與DirectX進行交互。查看SO上的this article瞭解更多詳情。但實際上它只是輪詢輸入,然後處理自己的事件。如果您還有其他問題超出了「我該怎麼做這件事」,請隨時發表評論,如果可以的話,我會進行編輯。

+0

我似乎無法開始編寫1行代碼。似乎沒有XNA沒有任何作用,但我不想做一個XNA項目,因爲我沒有做一個遊戲。你可以給我一個視覺工作室的小項目,可以檢測遊戲手柄輸入並每次按下按鍵時創建一個味精盒? –

+0

你要求的是沒有爲了SO。我提供了一篇包含示例代碼和其他參考的文章。我不能寫一個能爲你做的項目。 –

+0

我改變了我的問題 –