Skip to content

Commit

Permalink
Exclude modifier keys from being used in AnyKey methods #2428
Browse files Browse the repository at this point in the history
  • Loading branch information
jefetienne committed Oct 4, 2022
1 parent fa93846 commit 76f6d2d
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions Assets/Scripts/Game/InputManager.cs
Expand Up @@ -51,6 +51,24 @@ public class InputManager : MonoBehaviour
const float controllerCursorHorizontalSpeed = 900.0f;
const float controllerCursorVerticalSpeed = 900.0f;

// Use 'int' because Unity creates overhead for hashed lookups of enums
static readonly HashSet<int> unacceptedAnyKeys = new HashSet<int>()
{
(int)KeyCode.LeftShift,
(int)KeyCode.RightShift,
(int)KeyCode.LeftAlt,
(int)KeyCode.RightAlt,
(int)KeyCode.AltGr,
(int)KeyCode.LeftControl,
(int)KeyCode.RightControl,
(int)KeyCode.LeftWindows,
(int)KeyCode.RightWindows,
(int)KeyCode.LeftCommand,
(int)KeyCode.RightCommand,
(int)KeyCode.LeftApple,
(int)KeyCode.RightApple,
};

KeyCode[] keyCodeList;
KeyCode[] reservedKeys = new KeyCode[] { };

Expand Down Expand Up @@ -150,7 +168,7 @@ public bool IsPaused

public KeyCode[] KeyCodeList
{
get { return GetKeyCodeList(); }
get { return keyCodeList ?? (keyCodeList = GetKeyCodeList()); }
}

public KeyCode[] ReservedKeys
Expand Down Expand Up @@ -1104,7 +1122,8 @@ public bool AnyKeyUpIgnoreAxisBinds
public KeyCode GetAnyKeyDown()
{
foreach (KeyCode k in KeyCodeList)
if (GetUnaryKey(k, getKeyDownMethod, true, false))
if (!unacceptedAnyKeys.Contains((int)k)
&& GetUnaryKey(k, getKeyDownMethod, true, false))
return k;

return KeyCode.None;
Expand All @@ -1113,7 +1132,8 @@ public KeyCode GetAnyKeyDown()
public KeyCode GetAnyKeyUp()
{
foreach (KeyCode k in KeyCodeList)
if (GetUnaryKey(k, getKeyUpMethod, false, false))
if (!unacceptedAnyKeys.Contains((int)k)
&& GetUnaryKey(k, getKeyUpMethod, false, false))
return k;

return KeyCode.None;
Expand All @@ -1122,7 +1142,9 @@ public KeyCode GetAnyKeyUp()
public KeyCode GetAnyKeyDownIgnoreAxisBinds()
{
foreach (KeyCode k in KeyCodeList)
if (!IsUsedInAxisBinding(k) && GetUnaryKey(k, getKeyDownMethod, true, false))
if (!unacceptedAnyKeys.Contains((int)k)
&& !IsUsedInAxisBinding(k)
&& GetUnaryKey(k, getKeyDownMethod, true, false))
return k;

return KeyCode.None;
Expand All @@ -1132,7 +1154,9 @@ public KeyCode GetAnyKeyDownIgnoreAxisBinds()
public KeyCode GetAnyKeyUpIgnoreAxisBinds()
{
foreach (KeyCode k in KeyCodeList)
if (!IsUsedInAxisBinding(k) && GetUnaryKey(k, getKeyUpMethod, false, false))
if (!unacceptedAnyKeys.Contains((int)k)
&& !IsUsedInAxisBinding(k)
&& GetUnaryKey(k, getKeyUpMethod, false, false))
return k;

return KeyCode.None;
Expand Down Expand Up @@ -1548,9 +1572,6 @@ private void UpdateControllerCursorPosition()
// returns a list of all the Unity Input.KeyCodes and the custom axis KeyCodes
KeyCode[] GetKeyCodeList()
{
if (keyCodeList != null)
return keyCodeList;

HashSet<KeyCode> list = new HashSet<KeyCode>();

foreach (var e in Enum.GetValues(typeof(KeyCode)))
Expand Down

0 comments on commit 76f6d2d

Please sign in to comment.