Created
November 6, 2019 19:35
-
-
Save beardordie/632ed7dbdd4d71689517ea0360179132 to your computer and use it in GitHub Desktop.
Unity Component for debug identification of pressed input, including keyboard, mouse, connected controllers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
/// <summary> | |
/// Put this monobehaviour on any object in the Scene. Now any PRESSED keyboard key or mouse button or scrollwheel.y or joystick button (up to four axes) will Debug Log for identification. | |
/// </summary> | |
public class DebugInputPressed : MonoBehaviour | |
{ | |
public bool checkAxes = false; | |
[Tooltip("For XboxOne Windows 10 Right Trigger: Set up in Input Manager as Joystick 1, type: Axis, Axis: 3rd axis")] | |
public string axis1Name = "Player1_ShootAxis"; | |
[Tooltip("For XboxOne Windows 10 Left Trigger: Set up in Input Manager as Joystick 1, type: Axis, Axis: 3rd axis INVERTED")] | |
public string axis2Name = "Player1_SecondaryShootAxis"; | |
[Tooltip("For XboxOne Windows 10 D-Pad Left/Right: Set up in Input Manager as Joystick 1, type: Axis, Axis: 6th axis")] | |
public string axis3Name = "Joystick1_DPadHorizontal"; | |
[Tooltip("For XboxOne Windows 10 D-Pad Up/Down: Set up in Input Manager as Joystick 1, type: Axis, Axis: 7th axis")] | |
public string axis4Name = "Joystick1_DPadVertical"; | |
// Detects if any key has been pressed down. | |
Event e; | |
// Cached values for axes | |
float cached_axis1Value = 0f; | |
float cached_axis2Value = 0f; | |
float cached_axis3Value = 0f; | |
float cached_axis4Value = 0f; | |
float axisDeadzone = 0.3f; | |
private void OnGUI() | |
{ | |
e = Event.current; | |
} | |
void Update() | |
{ | |
if(checkAxes) | |
{ | |
float axis1 = Input.GetAxis(axis1Name); | |
float axis2 = Input.GetAxis(axis2Name); | |
float axis3 = Input.GetAxis(axis3Name); | |
float axis4 = Input.GetAxis(axis4Name); | |
if (axis1 > axisDeadzone && cached_axis1Value == 0f) | |
{ | |
Debug.Log("Axis: " + axis1Name); | |
cached_axis1Value = axis1; | |
return; | |
} | |
else if (axis1 == 0f) | |
{ | |
cached_axis1Value = 0f; | |
} | |
if (axis2 > axisDeadzone && cached_axis2Value == 0f) | |
{ | |
Debug.Log("Axis: " + axis2Name); | |
cached_axis2Value = axis2; | |
return; | |
} | |
else if (axis2 == 0f) | |
{ | |
cached_axis2Value = 0f; | |
} | |
if (cached_axis3Value == 0f) | |
{ | |
if (axis3 > axisDeadzone) | |
{ | |
// dpad Right | |
Debug.Log("Axis: " + axis3Name + " Positive"); | |
cached_axis3Value = axis3; | |
return; | |
} | |
else if (axis3 < -axisDeadzone) | |
{ | |
// dpad Left | |
Debug.Log("Axis: " + axis3Name + " Negative"); | |
cached_axis3Value = axis3; | |
return; | |
} | |
} | |
else if (axis3 == 0f) | |
{ | |
cached_axis3Value = 0f; | |
} | |
if (cached_axis4Value == 0f) | |
{ | |
if (axis4 > axisDeadzone) | |
{ | |
// dpad | |
Debug.Log("Axis: " + axis4Name + " Positive"); | |
cached_axis4Value = axis4; | |
return; | |
} | |
else if (axis4 < -axisDeadzone) | |
{ | |
// dpad | |
Debug.Log("Axis: " + axis4Name + " Negative"); | |
cached_axis4Value = axis4; | |
return; | |
} | |
} | |
else if (axis4 == 0f) | |
{ | |
cached_axis4Value = 0f; | |
} | |
} | |
if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift)) | |
{ | |
Debug.Log("Key Down: Shift"); | |
} | |
else if (Input.anyKeyDown) | |
{ | |
string readableName = string.Empty; | |
foreach (char c in Input.inputString) | |
{ | |
if (c == '\b') | |
{ | |
readableName = "Backspace"; | |
} | |
else if (c == '\0') | |
{ | |
readableName = "Shift"; | |
} | |
else if ((c == '\n') || (c == '\r')) // enter/return | |
{ | |
readableName = "Enter/Return"; | |
} | |
else if (c == ' ') | |
{ | |
readableName = "Spacebar"; | |
} | |
else | |
{ | |
readableName = c.ToString(); | |
} | |
} | |
if (readableName.Length > 0) | |
{ | |
Debug.Log("Key Down: " + readableName); | |
} | |
else | |
{ | |
if (e != null) | |
{ | |
if (e.isKey) | |
{ | |
if (e.keyCode != KeyCode.None) | |
{ | |
readableName = e.keyCode.ToString(); | |
} | |
else | |
{ | |
// a key is down, but the KeyCode is "None" | |
if (e.alt) | |
{ | |
readableName = "alt or option key"; | |
} | |
else if (e.control) | |
{ | |
readableName = "control"; | |
} | |
else if (e.command) | |
{ | |
readableName = "Command or Windows Key"; | |
} | |
else | |
{ | |
if (e.character == '\t') | |
{ | |
readableName = "Tab"; | |
} | |
else | |
{ | |
readableName = "Other key????????????? character: '" + e.character.ToString() + "'"; | |
} | |
} | |
} | |
} | |
else if (e.isMouse) | |
{ | |
if (e.button == 0) | |
{ | |
readableName = "Left Click"; | |
} | |
else if (e.button == 1) | |
{ | |
readableName = "Right Click"; | |
} | |
else if (e.button == 2) | |
{ | |
readableName = "Middle Click"; | |
} | |
} | |
else | |
{ | |
for (int h = 1; h < 9; h++) | |
{ | |
for (int i = 0; i < 20; i++) | |
{ | |
if (Input.GetKeyDown("joystick " + h + " button " + i)) | |
{ | |
readableName = "joystick " + h + " button " + i; | |
break; | |
} | |
} | |
} | |
if (readableName == string.Empty) | |
{ | |
readableName = "Not a 'key' and not a 'button' and not a button on joystick 1 through 8"; | |
} | |
} | |
Debug.Log("Key Down: " + readableName); | |
} | |
else | |
{ | |
Debug.Log("A key press is detected, but event is null."); | |
} | |
} | |
} | |
else if (Input.mouseScrollDelta.y != 0f) | |
{ | |
Debug.Log("Mouse Scrollwheel: " + Input.mouseScrollDelta.y); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment