# 输入绑定

> **输入绑定**，通常指的是由外部硬件设备发起的事件，由程序捕获并进行响应的过程。

### 输入事件类型

* IE\_Pressed    按下事件
* IE\_Released   释放事件
* IE\_DoubleClick 双击事件
* IE\_Repeat      持续按下事件

### 绑定按键

```cpp
IODevice& _device = IODeviceController::Instance()
    .GetIODevice("Standard");
    
_device.BindKey(EKeys::A, IOToolkit::IE_Pressed, []() {
    // 按键A被按下
});

_device.BindKey(EKeys::A, IOToolkit::IE_Released, []() {
    // 按键A被释放
});

_device.BindKey(EKeys::A, IOToolkit::IE_DoubleClick, []() {
    // 按键A双击事件
});

_device.BindKey(EKeys::A, IOToolkit::IE_Repeat, []() {
    // 按键A持续按下事件
});
```

### 绑定输入动作

{% tabs %}
{% tab title="application.cpp" %}
```cpp
IODevice& _device = IODeviceController::Instance()
	.GetIODevice("PCI2312A");

_device.BindAction("StartGame", IOToolkit::IE_Pressed, []() {
	// 开始游戏逻辑
});

_device.BindAction("PlayVideo", IOToolkit::IE_Pressed, [](FKey InKey) {
	if (InKey == EKeys::Button_10) {
		// PlayVideo("video1.mp4");
	}
	else if (InKey == EKeys::Button_11) {
		// PlayVideo("video2.mp4");
	}
	// ...
});
```
{% endtab %}

{% tab title="IODevice.xml" %}
```markup
<IORoot>
  <Device Name="Standard" Type="Standard" />
  <Device Name="PCI2312A" Type="External" DllName="PCI2312A" Index="0">
   
    <Action Name="StartGame">
      <Key Name="Button_00"/>
      <Key Name="S"/>
    </Action>
    
    <Action Name="PlayVideo"> 
      <Key Name="Button_10"/>
      <Key Name="Button_11"/>
    </Action>
    
  </Device>
</IORoot>
```
{% endtab %}
{% endtabs %}

#### 绑定输入动作(携带自定义参数)

{% tabs %}
{% tab title="application.cpp" %}
```cpp

class Application
{
	public:
		void RegisterEvents()
		{
			IODevice& _device = IODeviceController::Instance()
				.GetIODevice("PCI2312A");
			
			_device.BindAction("PlayVideo2", IOToolkit::IE_Pressed, 
				this, &Application::OnPlayVideo, "video1.mp4");
			
			_device.BindAction("PlayVideo2", IOToolkit::IE_Pressed, 
				this, &Application::OnPlayVideo, "video2.mp4");
		}
		
		void OnPlayVideo(const char* videoName)
		{
			//PlayVideo(videoName);
		}
}

```
{% endtab %}

{% tab title="IODevice.xml" %}
```markup
<IORoot>
  <Device Name="Standard" Type="Standard" />
  <Device Name="PCI2312A" Type="External" DllName="PCI2312A" Index="0">
   
    <Action Name="PlayVideo1">
      <Key Name="Button_00"/>
      <Key Name="Button_01"/>
      <Key Name="Button_02"/>
      <Key Name="Button_03"/>
      <Key Name="Button_04"/>
    </Action>
    
    <Action Name="PlayVideo2"> 
      <Key Name="Button_10"/>
      <Key Name="Button_11"/>
      <Key Name="Button_12"/>
      <Key Name="Button_13"/>
      <Key Name="Button_14"/>
    </Action>
    
  </Device>
</IORoot>
```
{% endtab %}
{% endtabs %}

### 绑定轴键

```cpp
IODeviceController::Instance()
	.GetIODevice("Standard")
	.BindAxisKey(EKeys::MouseX, [](float InDeltaX) {
		// 处理鼠标移动时逻辑
	});
```

### 绑定轴输入

{% tabs %}
{% tab title="Application.cpp" %}
```cpp
IODeviceController::Instance()
	.GetIODevice("Standard")
	.BindAxis("MoveHorizontal", [](float InDeltaX) {
    // player.transform.position += player.transform.left * InDeltaX;
});
```
{% endtab %}

{% tab title="IODevice.xml" %}
```cpp
IORoot>
  <Device Name="Standard" Type="Standard">
  
    <Axis Name="MoveHorizontal">
      <Key Name="Left" Scale="-10"/>
      <Key Name="Right" Scale="10"/>
    </Axis>
    
  </Device>
</IORoot>
```
{% endtab %}
{% endtabs %}
