private void TryExecuteCommand(KeyCommand command, PressType.KeyPressType pressType) { // If we need to press multiple keys, loop over them. if (command.commandKeyTriggers.Length > 1) { for (int ii = 0; ii < command.commandKeyTriggers.Length; ii++) { // If were at the end of the required keys to press array, check if we pressed the last one and execute the command based on that. if (ii == command.commandKeyTriggers.Length - 1) { switch (pressType) { case PressType.KeyPressType.Down: if (Input.GetKeyDown(command.commandKeyTriggers[ii])) { ExecuteCommand(command); } break; case PressType.KeyPressType.Hold: if (Input.GetKey(command.commandKeyTriggers[ii])) { ExecuteCommand(command); } break; } } // This is one of the keys we need to press in order to execute the command. Check if it is pressed down. else { if (!Input.GetKey(command.commandKeyTriggers[ii])) { break; } } } } // We only need to press one key. else { switch (pressType) { case PressType.KeyPressType.Down: if (Input.GetKeyDown(command.commandKeyTriggers[0])) { ExecuteCommand(command); } break; case PressType.KeyPressType.Hold: if (Input.GetKey(command.commandKeyTriggers[0])) { ExecuteCommand(command); } break; } } }
/// <param name="keyCodes"> /// The set of keys required to be pressed to execute this function. (Note: KeyPressType.Down: keys need to be pressed in the correct order, /// KeyPressType.Hold: keys don't need to be pressed in the correct order, KeyPressType.Up: all keys need to be released in the same frame) /// </param> public KeyCommandAttribute(KeyCode[] keyCodes, PressType.KeyPressType pressType, params object[] parameters) { if (keyCodes.Length > 1) { this.keyCodes = keyCodes; } else { this.keyCode = keyCodes[0]; } this.pressType = pressType; this.parameters = parameters; }
/// <param name="keyCode"> /// The key required to be pressed to execute this function. /// </param> /// <param name="pressType"> /// The type of keypress required to execute this function. /// </param> /// <param name="parameters"> /// Parameters for this function. /// </param> public KeyCommandAttribute(KeyCode keyCode, PressType.KeyPressType pressType, params object[] parameters) { this.keyCode = keyCode; this.pressType = pressType; this.parameters = parameters; }