I am wondering how I can access this to Check the String and set a boolean, to tell whether a keyboard or controller is being used.
Well, a quick search suggests that there is no optimal way of doing things, yet (at least, not without some asset or plugin).
This is someone’s answer to the problem.
Seeing as it’s quite old, I wonder if there’s a solution by now, but I did not see it - briefly looking at the unity API for the Input and Input Manager.
EDIT:
Found these additional answers;
- Using Input.GetJoystickNames: Detect if an XBOX 360 controller is plugged in - Questions & Answers - Unity Discussions
- Using an external asset: Joystick detection and direct axis detection - Unity Engine - Unity Discussions
For anyone else who comes across this, I found a solution that works really well.
I use an InputHandler class to manage the different kinds of inputs, and ask each input (e.g. Keyboard, Controller) if an input was received based on my keybindings.
public class InputHandler
{
private enum InputMode
{
Keyboard, Controller
}
private InputMode mode;
private GameManager gameManager;
private KeyboardInputHandler keyboardInput;
private ControllerInputHandler controllerInput;
public InputHandler(GameManager manager)
{
gameManager = manager;
keyboardInput = new KeyboardInputHandler(manager);
controllerInput = new ControllerInputHandler(manager);
}
public void ProcessInput()
{
bool receivedControllerInput = controllerInput.ProcessInput();
bool receivedKeyboardMouseInput = keyboardInput.ProcessInput();
if (receivedControllerInput)
{
mode = InputMode.Controller;
}
else if (receivedKeyboardMouseInput)
{
mode = InputMode.Keyboard;
}
Debug.Log("Input mode: " + mode);
}
Then I define the keybindings in the Keyboard or Controller input handler. Here’s an example of the ControllerInputHandler:
public class ControllerInputHandler
{
// axis and button definitions here, e.g.
#if UNITY_STANDALONE_WIN
private static KeyCode A_BUTTON = KeyCode.JoystickButton0;
private static KeyCode X_BUTTON = KeyCode.JoystickButton2;
private static KeyCode START_BUTTON = KeyCode.JoystickButton7;
#else
// other platforms
// see http://wiki.unity3d.com/index.php?title=Xbox360Controller
#endif
private GameManager gameManager;
private Dictionary<KeyCode, Action> getKeyDownBindings = new Dictionary<KeyCode, Action>();
private Dictionary<KeyCode, Action> getKeyUpBindings = new Dictionary<KeyCode, Action>();
private Dictionary<KeyCode, Action> getKeyBindings = new Dictionary<KeyCode, Action>();
private bool movedByAxis;
public ControllerInputHandler(GameManager manager)
{
gameManager = manager;
getKeyDownBindings.Add(A_BUTTON, gameManager.JumpPressed);
getKeyDownBindings.Add(X_BUTTON, gameManager.MeleePressed);
getKeyDownBindings.Add(START_BUTTON, gameManager.ToggleMenu);
getKeyUpBindings.Add(A_BUTTON, gameManager.JumpReleased);
}
public bool ProcessInput()
{
bool inputReceived = false;
foreach (var binding in getKeyDownBindings)
{
if (Input.GetKeyDown(binding.Key))
{
inputReceived = true;
gameManager.DirectInput(binding.Value);
}
}
foreach (var binding in getKeyUpBindings)
{
if (Input.GetKeyUp(binding.Key))
{
inputReceived = true;
gameManager.DirectInput(binding.Value);
}
}
foreach (var binding in getKeyBindings)
{
if (Input.GetKey(binding.Key))
{
inputReceived = true;
gameManager.DirectInput(binding.Value);
}
}
// I could probably tidy the axis code up, but you get the idea!
if (Input.GetAxis(LEFT_STICK_H_AXIS) > 0.5f)
{
inputReceived = true;
movedByAxis = true;
gameManager.DirectInput(gameManager.MoveRightPressed);
}
else if (Input.GetAxis(LEFT_STICK_H_AXIS) < -0.5f)
{
inputReceived = true;
movedByAxis = true;
gameManager.DirectInput(gameManager.MoveLeftPressed);
}
else if (movedByAxis)
{
movedByAxis = false;
gameManager.DirectInput(gameManager.MoveReleased);
}
return inputReceived;
}
This will only pick up keybindings you’ve defined, but it works. What’s great about this is you can serialize the key binding dictionaries for your own custom key bindings menu, if you wanted.
@Aspekt1024 Just what I was looking for, thank you!
Thanks @Aspekt1024 having a look!
Seeing as people are reading this thread again, it’s worth updating with some new knowledge I have on the topic.
Both Rewired and the newer Unity Input system have listeners for controller input, be it keyboard, mouse, gamepad or anything else, without having to define keys used for your game (which is what my previous answer details).
For Unity’s input system, for example, you can use the InputSystem.onEvent
delegate to monitor for input, which will give you access to the device (controller) used. Checking which device will let you set the appropriate controller type for glyphs on-screen etc.
For further info, check out:
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Events.html#monitoring-events
If you’re still on the default bare-bones Unity input system, i’d strongly recommend switching to Unity’s newer input package (free, inbuilt via package manager, very featured) or Rewired (not free, but also very good).