daemon3000 / InputManager

Custom InputManager for Unity
Other
587 stars 88 forks source link

Check if key is already assigned in any of the control schemes #54

Closed CraftedPvP closed 4 years ago

CraftedPvP commented 4 years ago

Would it be possible for a function like this exist in the InputManager class? I presume this would be helpful for everyone.

I can make my own but it's a feature request that I'm going for.

daemon3000 commented 4 years ago

I added two new methods to the InputManager class called IsKeyUsedInAnyControlScheme and IsKeyUsedInControlScheme which will return true if a key is used in any control scheme or a specific control scheme respectively.

The methods also have a second override with an output parameter of type KeyUsageResult which contains the control scheme name, the input action index(inside the control scheme) and the input binding index(inside the input action). You can use this result to assign the key to a new binding and clear the old one for example.

I didn't test the new methods so if you see any issues let me know and I'll fix it else you can close the issue.

CraftedPvP commented 4 years ago

Thanks for the speedy fix. I appreciate it :D

For those people who are having a hard time, there's a couple functions added to this commit. You can access the following variables after getting the result.

// get the result
string controlSchemeName = "ENTER NAME HERE";
KeyCode key = KeyCode.A; // Replace the KeyCode.A with whatever key you wanted
KeyUsageResult keyUsageResult = KeyUsageResult.None;

// check if the key is being used. if it is being used, the if condition will run
if(InputManager.IsKeyUsedInControlScheme(controlSchemeName, key, out keyUsageResult)){

    // access the values
    ControlScheme controlScheme = 
    InputManager.GetControlScheme(keyUsageResult.ControlSchemeName);
    InputAction inputAction = controlScheme.GetAction(keyUsageResult.ActionIndex);
    InputBinding inputBinding = inputAction.GetBinding(keyUsageResult.BindingIndex);

]

I look forward to using this system in the future