Skip to content

Key Rebinding

Cristian Geambasu edited this page Sep 7, 2018 · 1 revision
//==========================================
// How To Rebind A Key
//==========================================
ScanSettings settings = new ScanSettings
{
	ScanFlags = ScanFlags.Key,
	// If the player presses this key the scan will be canceled.
	CancelScanKey = KeyCode.Escape,
	// If the player doesn't press any key within the specified number
	// of seconds the scan will be canceled.
	Timeout = 10
};

// You can replace the lambda function with a member function if you want.
InputManager.StartInputScan(settings, result =>
{
	// The handle should return "true" if the key is accepted or "false" if the key is rejected.
	// If the key is rejected the scan will continue until a key is accepted or until the timeout expires.

	InputAction inputAction = InputManager.GetAction("MyControlScheme", "MyAction");
	inputAction.Bindings[0].Positive = result.key;
	return true;
});

//==========================================
// How To Rebind A Gamepad Button
//==========================================
ScanSettings settings = new ScanSettings
{
	ScanFlags = ScanFlags.JoystickButton | ScanFlags.JoystickAxis;
	// If the player presses this key the scan will be canceled.
	CancelScanKey = KeyCode.Escape,
	// If the player doesn't press any key within the specified number
	// of seconds the scan will be canceled.
	Timeout = 10
};

// You can replace the lambda function with a member function if you want.
InputManager.StartInputScan(settings, result =>
{
	// The handle should return "true" if the button is accepted or "false" if the button is rejected.
	// If the button is rejected the scan will continue until a button is accepted or until the timeout expires.

	if(result.ScanFlags == ScanFlags.JoystickButton)
	{
		if((int)key < (int)KeyCode.JoystickButton0 || (int)key > (int)KeyCode.JoystickButton19)
			return false;

		InputAction inputAction = InputManager.GetAction("MyControlScheme", "MyAction");
		inputAction.Bindings[0].Type = InputType.Button;
		inputAction.Bindings[0].Positive = result.key;
	}
	else
	{
		InputAction inputAction = InputManager.GetAction("MyControlScheme", "MyAction");
		inputAction.Bindings[0].Type = InputType.AnalogButton;
		inputAction.Bindings[0].Invert = result.JoystickAxisValue < 0.0f;
		inputAction.Bindings[0].Axis = result.JoystickAxis;
	}

	return true;
});

//==========================================
// How To Rebind A Gamepad Axis
//==========================================
ScanSettings settings = new ScanSettings
{
	ScanFlags = ScanFlags.JoystickAxis;
	// If the player presses this key the scan will be canceled.
	CancelScanKey = KeyCode.Escape,
	// If the player doesn't press any key within the specified number
	// of seconds the scan will be canceled.
	Timeout = 10
};

// You can replace the lambda function with a member function if you want.
InputManager.StartInputScan(settings, result =>
{
	// The handle should return "true" if the button is accepted or "false" if the button is rejected.
	// If the button is rejected the scan will continue until a button is accepted or until the timeout expires.

	InputAction inputAction = InputManager.GetAction("MyControlScheme", "MyAction");
	inputAction.Bindings[0].Axis = result.JoystickAxis;
	return true;
});
Clone this wiki locally