機台搖桿Arcade stick

此頁面說明使用 Windows.Gaming.Input.ArcadeStick 的 Xbox One 電動搖桿程式設計基本知識,以及通用 Windows 平台 (UWP) 的相關 API。This page describes the basics of programming for Xbox One arcade sticks using Windows.Gaming.Input.ArcadeStick and related APIs for the Universal Windows Platform (UWP).

閱讀此頁面,即可了解:By reading this page, you'll learn:

  • 如何收集所連接電動搖桿和其使用者的清單how to gather a list of connected arcade sticks and their users
  • 如何偵測已新增或移除電動搖桿how to detect that an arcade stick has been added or removed
  • 如何讀取來自一或多個電動搖桿的輸入how to read input from one or more arcade sticks
  • arcade 杆如何以 UI 導覽裝置的行為how arcade sticks behave as UI navigation devices

電動搖桿概觀Arcade stick overview

電動搖桿為輸入裝置,價值在於重新產生直立式機台風格和其高精確度數位控制項。Arcade sticks are input devices valued for reproducing the feel of stand-up arcade machines and for their high-precision digital controls. 電動搖桿是面對面格鬥和其他機台樣式遊戲的絕佳輸入裝置,並且適用於與全數位控制項搭配良好的任何遊戲。Arcade sticks are the perfect input device for head-to-head-fighting and other arcade-style games, and are suitable for any game that works well with all-digital controls. 在 Windows 10 和 Xbox One UWP 應用程式中,電動搖桿是由 Windows.Gaming.Input 命名空間所支援。Arcade sticks are supported in Windows 10 and Xbox One UWP apps by the Windows.Gaming.Input namespace.

Xbox One arcade 棒配備了8向數位操縱杆、六個 動作 按鈕 (以) 下圖中的 A1-A6 表示,以及兩個 特殊 按鈕 (以 S1 和 S2) 表示。它們都是不支援類比控制項或震動的數位輸入裝置。Xbox One arcade sticks are equipped with an 8-way digital joystick, six Action buttons (represented as A1-A6 in the image below), and two Special buttons (represented as S1 and S2); they're all-digital input devices that don't support analog controls or vibration. Xbox One arcade 杆也配備了可用來支援 UI 導覽的 [ View ] 和 [ Menu ] 按鈕,但它們並不是為了支援遊戲命令,而且無法立即存取為搖桿按鈕。Xbox One arcade sticks are also equipped with View and Menu buttons used to support UI navigation but they're not intended to support gameplay commands and can't be readily accessed as joystick buttons.

Arcade 與4方向的搖桿、6個動作按鈕 (A1-A6) ,以及2個特殊按鈕 (S1 和 S2)

UI 瀏覽UI navigation

為了減輕支援不同輸入裝置進行使用者介面瀏覽的負擔,以及鼓勵遊戲與裝置之間的一致性,大部分「實體」__ 輸入裝置同時會當成稱為 UI 瀏覽控制器的不同「邏輯」__ 輸入裝置使用。In order to ease the burden of supporting many different input devices for user interface navigation and to encourage consistency between games and devices, most physical input devices simultaneously act as a separate logical input device called a UI navigation controller. UI 瀏覽控制器提供跨輸入裝置之 UI 瀏覽命令的通用詞彙。The UI navigation controller provides a common vocabulary for UI navigation commands across input devices.

Arcade 會以 UI 流覽控制器的方式,將 所需的一組 流覽命令對應至操縱杆和 ViewMenuaction 1action 2 按鈕。As a UI navigation controller, arcade sticks map the required set of navigation commands to the joystick and View, Menu, Action 1, and Action 2 buttons.

導覽命令Navigation command 電動搖桿輸入Arcade stick input
UpUp 搖桿向上Stick up
DownDown 搖桿向下Stick down
LeftLeft 搖桿向左Stick left
RightRight 搖桿向右Stick right
檢視View 檢視按鈕View button
功能表Menu 功能表按鈕Menu button
接受Accept 動作 1 按鈕Action 1 button
取消Cancel 動作 2 按鈕Action 2 button

電動搖桿不會對應瀏覽命令的任意選用集Arcade sticks don't map any of the optional set of navigation commands.

偵測和追蹤電動搖桿Detect and track arcade sticks

除了 ArcadeStick 類別(而非 遊戲台 類別)之外,偵測和追蹤 arcade 杆的運作方式與 gamepads 完全相同。Detecting and tracking arcade sticks works in exactly the same way as it does for gamepads, except with the ArcadeStick class instead of the Gamepad class. 如需詳細資訊,請參閱遊戲台與震動See Gamepad and vibration for more information.

讀取電動搖桿Reading the arcade stick

在您找出感興趣的電動搖桿之後,即可收集其輸入。After you identify the arcade stick that you're interested in, you're ready to gather input from it. 不過,電動搖桿不是透過引發事件來溝通狀態變更,這與您可能習慣使用的一些其他類型的輸入不同。However, unlike some other kinds of input that you might be used to, arcade sticks don't communicate state-change by raising events. 相反的,您可以進行_輪詢_來定期讀取其目前狀態。Instead, you take regular readings of their current state by polling them.

對電動搖桿進行輪詢Polling the arcade stick

輪詢可在精確的時間點擷取電動搖桿的快照。Polling captures a snapshot of the arcade stick at a precise point in time. 這種輸入收集方式適用於大部分遊戲,因為其邏輯一般是以決定性迴圈執行,而不是透過事件驅動;透過一次所收集的輸入來解譯遊戲命令,一般也比透過不同時間所收集的許多單一輸入來解譯遊戲命令更為簡單。This approach to input gathering is a good fit for most games because their logic typically runs in a deterministic loop rather than being event-driven; it's also typically simpler to interpret game commands from input gathered all at once than it is from many single inputs gathered over time.

呼叫 GetCurrentReading,即可輪詢電動搖桿;此函式會傳回包含電動搖桿狀態的 ArcadeStickReadingYou poll an arcade stick by calling GetCurrentReading; this function returns an ArcadeStickReading that contains the state of the arcade stick.

下列範例會對電動搖桿的目前狀態進行輪詢。The following example polls an arcade stick for its current state.

auto arcadestick = myArcadeSticks[0];

ArcadeStickReading reading = arcadestick->GetCurrentReading();

除了電動搖桿狀態之外,每次讀取都會包含精確指出狀態擷取時間的時間戳記。In addition to the arcade stick state, each reading includes a timestamp that indicates precisely when the state was retrieved. 時間戳記適用於與先前讀取的計時或遊戲模擬的計時建立關聯。The timestamp is useful for relating to the timing of previous readings or to the timing of the game simulation.

讀取按鈕Reading the buttons

每個 [arcade] 按鈕 — 是搖桿的四個方向、六個 [ 動作 ] 按鈕,以及兩個 特殊 按鈕,會 — 提供數位閱讀,指出是否已按下 () ,或 (向上) 放開。Each of the arcade stick buttons—the four directions of the joystick, six Action buttons, and two Special buttons—provides a digital reading that indicates whether it's pressed (down) or released (up). 為了提高效率,按鈕讀數不會以個別布林值表示;相反地,它們會封裝成 ArcadeStickButtons 列舉所表示的單一位位。For efficiency, button readings aren't represented as individual boolean values; instead, they're all packed into a single bitfield that's represented by the ArcadeStickButtons enumeration.

注意

Arcade 杆配備了其他按鈕,用於 UI 導覽,例如 [ 視圖 ] 和 [ 功能表 ] 按鈕。Arcade sticks are equipped with additional buttons used for UI navigation such as the View and Menu buttons. 這些按鈕不是 ArcadeStickButtons 列舉的一部分,而且只能將電動搖桿當成 UI 瀏覽裝置存取來進行讀取。These buttons are not a part of the ArcadeStickButtons enumeration and can only be read by accessing the arcade stick as a UI navigation device. 如需詳細資訊,請參閱 UI 瀏覽裝置For more information, see UI Navigation Device.

按鈕值讀取自 ArcadeStickReading 結構的 Buttons 屬性。The button values are read from the Buttons property of the ArcadeStickReading structure. 因為此屬性是位元欄位,所以使用位元遮罩來隔離感興趣按鈕的值。Because this property is a bitfield, bitwise masking is used to isolate the value of the button that you're interested in. 設定對應位元時,即按下 (向下) 按鈕;否則為放開 (向上)。The button is pressed (down) when the corresponding bit is set; otherwise, it's released (up).

下列範例會決定是否按下 [ 動作 1 ] 按鈕。The following example determines whether the Action 1 button is pressed.

if (ArcadeStickButtons::Action1 == (reading.Buttons & ArcadeStickButtons::Action1))
{
    // Action 1 is pressed
}

下列範例會決定是否要釋放 [ 動作 1 ] 按鈕。The following example determines whether the Action 1 button is released.

if (ArcadeStickButtons::None == (reading.Buttons & ArcadeStickButtons::Action1))
{
    // Action 1 is released (not pressed)
}

您有時必須要判斷按鈕何時從按下轉換為放開或從放開轉換為按下、是否按下或放開多個按鈕,或是否以特定方式排列一組按鈕 (部分為按下,部分則否)。Sometimes you might want to determine when a button transitions from pressed to released or released to pressed, whether multiple buttons are pressed or released, or if a set of buttons are arranged in a particular way—some pressed, some not. 如需如何偵測這些條件的相關資訊,請參閱偵測按鈕轉換偵測複雜按鈕排列For information on how to detect these conditions, see Detecting button transitions and Detecting complex button arrangements.

執行 InputInterfacing 範例Run the InputInterfacing sample

InputInterfacingUWP 範例 (github) 示範如何同時使用電動搖桿和不同類型的輸入裝置,以及這些輸入裝置作為 UI 瀏覽控制器表現的方式。The InputInterfacingUWP sample (github) demonstrates how to use arcade sticks and different kinds of input devices in tandem, as well as how these input devices behave as UI navigation controllers.

另請參閱See also