> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 模擬遊戲台輸入

> 說明如何模擬對 XBOX One 開發主機的遊戲台輸入，以用於自動化測試框架或獨立工具。

# 模擬遊戲台輸入

測試 XBOX One 遊戲時，讓人員使用實體遊戲台與開發主機互動並不一定有效率。您可以在電腦上使用 XBOX Tools Framework (XTF) 和 XBOX Test Toolkit，以程式設計方式模擬遊戲台輸入，將單元測試、壓力測試、長時間測試和使用者測試自動化。您可以同時為多部主機模擬遊戲台輸入，以測試線上多人遊戲案例。

若要模擬遊戲台輸入，請將 `XtfInput.lib` 新增至 **Configuration Properties** > **Linker** > **Input** > **Additional Dependencies**，並在測試框架或獨立工具的原始程式檔中包含 `XtfInput.h`。

* `XtfInput.lib` 位於 `<GDK Install Path>\toolKit\lib\amd64`
* `XtfInput.h` 位於 `<GDK Install Path>\toolKit\include`

<Note>
  Xtf 應用程式無法以 32 位元系統為目標。您必須確保建置選項以 64 位元系統為目標。請使用下列步驟將專案設定為以 64 位元為目標。

  1. 開啟頂端內容列上的 **BUILD** 功能表，然後選取 **Configuration Manager**。
  2. 選取 **Active solution platform:** 下拉式清單 (很可能設定為 Win32)，然後選取 **\<New\..>**。
  3. 選取 **Type or select the new platform:** 下拉式清單，然後選取 **x64**。從您偏好的設定 (很可能是 Win32) 複製設定。
  4. 選取 **OK** 以新增新的設定。
</Note>

#### 模擬遊戲台輸入

1. 呼叫 [XtfCreateInputClient](/zh-TW/reference/tools/xtf/xtfinput/methods/xtfcreateinputclient-xtfinput-xbox-windows-m)，並指定目標主機的位址，以建立 [IXtfInputClient](/zh-TW/reference/tools/xtf/xtfinput/classes/IXtfInputClient/ixtfinputclient-xtfinput-xbox-windows-t) 的執行個體。
2. 呼叫 [IXtfInputClient::ConnectGamepad](/zh-TW/reference/tools/xtf/xtfinput/classes/IXtfInputClient/methods/connectgamepad-ixtfinputclient-xtfinput-xbox-windows-m) 以建立虛擬控制器。
3. 設定 [GAMEPAD\_REPORT](/zh-TW/reference/tools/xtf/xtfinput/structures/GAMEPAD_REPORT-xtfinput-xbox-windows-t) 結構，以包含您想要的遊戲台狀態。例如，若要模擬按下 **A** 按鈕，您會使用 `gamepadReport.Buttons = A;`。
4. 使用 [ConnectGamepad](/zh-TW/reference/tools/xtf/xtfinput/classes/IXtfInputClient/methods/connectgamepad-ixtfinputclient-xtfinput-xbox-windows-m) 傳回的控制器識別碼，以及上一個步驟中的 [GAMEPAD\_REPORT](/zh-TW/reference/tools/xtf/xtfinput/structures/GAMEPAD_REPORT-xtfinput-xbox-windows-t)，呼叫 [IXtfInputClient::SendGamepadReport](/zh-TW/reference/tools/xtf/xtfinput/classes/IXtfInputClient/methods/sendgamepadreport-ixtfinputclient-xtfinput-xbox-windows-m)。
5. 等待至少 16 毫秒，然後視需要再次呼叫 [IXtfInputClient::SendGamepadReport](/zh-TW/reference/tools/xtf/xtfinput/classes/IXtfInputClient/methods/sendgamepadreport-ixtfinputclient-xtfinput-xbox-windows-m) 以重設控制器狀態。例如，若要模擬放開 **A** 按鈕，您會使用 `gamepadReport.Buttons = NONE`。
6. 重複步驟 3 到 5 以傳送其他按鈕按下動作。
7. 完成時呼叫 [DisconnectGamepad](/zh-TW/reference/tools/xtf/xtfinput/classes/IXtfInputClient/methods/disconnectgamepad-ixtfinputclient-xtfinput-xbox-windows-m)，以確保為虛擬控制器配置的資源已釋放。

下列程式碼範例示範如何以程式設計方式模擬控制器輸入。

**C++**

```cpp theme={null}
#include "XtfInput.h"  

...  

IXtfInputClient * inputClient;
UINT64 controllerID;
GAMEPAD_REPORT gamepadReport = {};

// replace with the IP address of your devkit
XtfCreateInputClient(L"1.2.3.4", IID_PPV_ARGS(&inputClient));
inputClient->ConnectGamepad(&controllerID);

// give time for the connection to settle before sending input reports
Sleep(250);

gamepadReport.Buttons = A;
inputClient->SendGamepadReport(controllerID, gamepadReport);

Sleep(16);

gamepadReport.Buttons = NONE;
inputClient->SendGamepadReport(controllerID, gamepadReport);

...

inputClient->DisconnectGamepad(controllerID);  
```

## 另請參閱

##### 參考

* [XtfInput](/zh-TW/reference/tools/xtf/xtfinput/xtfinput-xbox-microsoft-n)


## Related topics

- [使用 GDKX 建置並執行你的第一個主機遊戲](/zh-TW/home/build-first-title/first-console-title-walkthrough.md)
- [XBOX 遊戲的 XBOX 需求](/zh-TW/publishing/certification/xbox-requirements.md)
- [XR-001 遊戲穩定性](/zh-TW/publishing/certification/xr/xr-001.md)
- [XBOX 主機遊戲的認證測試 XBOX 需求](/zh-TW/publishing/certification/console-cert-tested-requirements.md)
- [無障礙功能標籤](/zh-TW/build/game-principles/accessibility/accessibility-feature-tags.md)
