Skip to content

Commit 5d3bbca

Browse files
authored
Merge pull request #6679 from bdach/expose-virtual-key-helper
Expose public helper to get a virtual key for a physical modifier key
2 parents e6ba569 + 0a3359b commit 5d3bbca

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

osu.Framework/Extensions/InputKeyExtensions.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,43 @@ public static bool IsVirtual(this InputKey key)
3838
return false;
3939
}
4040
}
41+
42+
/// <summary>
43+
/// If <paramref name="key"/> is a <see cref="IsPhysical">physical</see> key which is covered by another <see cref="IsVirtual">virtual</see> key, returns that virtual key.
44+
/// Otherwise, returns <see langword="null"/>.
45+
/// </summary>
46+
/// <example>
47+
/// <code>
48+
/// &gt; InputKey.LShift.GetVirtualKey()
49+
/// Shift
50+
/// &gt; InputKey.RSuper.GetVirtualKey()
51+
/// Super
52+
/// &gt; InputKey.A.GetVirtualKey()
53+
/// null
54+
/// </code>
55+
/// </example>
56+
public static InputKey? GetVirtualKey(this InputKey key)
57+
{
58+
switch (key)
59+
{
60+
case InputKey.LShift:
61+
case InputKey.RShift:
62+
return InputKey.Shift;
63+
64+
case InputKey.LControl:
65+
case InputKey.RControl:
66+
return InputKey.Control;
67+
68+
case InputKey.LAlt:
69+
case InputKey.RAlt:
70+
return InputKey.Alt;
71+
72+
case InputKey.LSuper:
73+
case InputKey.RSuper:
74+
return InputKey.Super;
75+
}
76+
77+
return null;
78+
}
4179
}
4280
}

osu.Framework/Input/Bindings/KeyCombination.cs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -108,30 +108,6 @@ public bool IsPressed(KeyCombination pressedKeys, InputState inputState, KeyComb
108108
return ContainsAll(Keys, pressedKeys.Keys, matchingMode);
109109
}
110110

111-
private static InputKey? getVirtualKey(InputKey key)
112-
{
113-
switch (key)
114-
{
115-
case InputKey.LShift:
116-
case InputKey.RShift:
117-
return InputKey.Shift;
118-
119-
case InputKey.LControl:
120-
case InputKey.RControl:
121-
return InputKey.Control;
122-
123-
case InputKey.LAlt:
124-
case InputKey.RAlt:
125-
return InputKey.Alt;
126-
127-
case InputKey.LSuper:
128-
case InputKey.RSuper:
129-
return InputKey.Super;
130-
}
131-
132-
return null;
133-
}
134-
135111
/// <summary>
136112
/// Check whether the provided set of pressed keys matches the candidate binding.
137113
/// </summary>
@@ -192,7 +168,7 @@ internal static bool ContainsAll(ImmutableArray<InputKey> candidateKeyBinding, I
192168
internal static bool KeyBindingContains(ImmutableArray<InputKey> candidateKeyBinding, InputKey physicalKey)
193169
{
194170
return candidateKeyBinding.Contains(physicalKey) ||
195-
(getVirtualKey(physicalKey) is InputKey vKey && candidateKeyBinding.Contains(vKey));
171+
(physicalKey.GetVirtualKey() is InputKey vKey && candidateKeyBinding.Contains(vKey));
196172
}
197173

198174
/// <summary>
@@ -210,7 +186,7 @@ internal static bool IsPressed(ImmutableArray<InputKey> pressedPhysicalKeys, Inp
210186

211187
foreach (var pk in pressedPhysicalKeys)
212188
{
213-
if (getVirtualKey(pk) == candidateKey)
189+
if (pk.GetVirtualKey() == candidateKey)
214190
return true;
215191
}
216192

0 commit comments

Comments
 (0)