Skip to content

Commit f9ea658

Browse files
todi1856Pauliusd01jfreire-unity
authored
NEW: Correctly expose Media keys through keyboard (#2212)
Co-authored-by: Paulius Dervinis <[email protected]> Co-authored-by: João Freire <[email protected]>
1 parent a5758e0 commit f9ea658

File tree

6 files changed

+164
-15
lines changed

6 files changed

+164
-15
lines changed

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ however, it has to be formatted properly to pass verification tests.
1010

1111
## [Unreleased] - yyyy-mm-dd
1212

13+
### Added
14+
- Exposed MediaPlayPause, MediaRewind, MediaForward keys on Keyboard.
1315

1416
## [1.14.2] - 2025-08-05
1517

Packages/com.unity.inputsystem/InputSystem/Devices/Keyboard.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ public unsafe struct KeyboardState : IInputStateTypeInfo
175175
[InputControl(name = "f22", displayName = "F22", layout = "Key", bit = (int)Key.F22)]
176176
[InputControl(name = "f23", displayName = "F23", layout = "Key", bit = (int)Key.F23)]
177177
[InputControl(name = "f24", displayName = "F24", layout = "Key", bit = (int)Key.F24)]
178+
[InputControl(name = "mediaPlayPause", displayName = "MediaPlayPause", layout = "Key", bit = (int)Key.MediaPlayPause)]
179+
[InputControl(name = "mediaRewind", displayName = "MediaRewind", layout = "Key", bit = (int)Key.MediaRewind)]
180+
[InputControl(name = "mediaForward", displayName = "MediaForward", layout = "Key", bit = (int)Key.MediaForward)]
178181
[InputControl(name = "IMESelected", layout = "Button", bit = (int)KeyEx.RemappedIMESelected, synthetic = true)] // Use the last bit to hold IME selected state.
179182
public fixed byte keys[kSizeInBytes];
180183

@@ -969,6 +972,21 @@ public enum Key
969972
/// </summary>
970973
F24,
971974

975+
/// <summary>
976+
/// The <see cref="Keyboard.mediaPlayPause"/>.
977+
/// </summary>
978+
MediaPlayPause,
979+
980+
/// <summary>
981+
/// The <see cref="Keyboard.mediaRewind"/>.
982+
/// </summary>
983+
MediaRewind,
984+
985+
/// <summary>
986+
/// The <see cref="Keyboard.mediaForward"/>.
987+
/// </summary>
988+
MediaForward,
989+
972990
/// <summary>
973991
/// Don't use this. This is a dummy key that is only used internally to represent the IME selected state.
974992
/// Will be removed in the future.
@@ -1048,7 +1066,7 @@ public class Keyboard : InputDevice, ITextInputReceiver, IEventPreProcessor
10481066
/// </summary>
10491067
/// <value>Total number of key controls.</value>
10501068
public const int KeyCount = (int)Key.OEM5; // Not updated to Key.F24 for not breaking the API
1051-
internal const int ExtendedKeyCount = (int)Key.F24;
1069+
internal const int ExtendedKeyCount = (int)Key.MediaForward;
10521070

10531071
/// <summary>
10541072
/// Event that is fired for every single character entered on the keyboard.
@@ -2270,6 +2288,30 @@ public string keyboardLayout
22702288
/// </remarks>
22712289
public KeyControl f24Key => this[Key.F24];
22722290

2291+
/// <summary>
2292+
/// The Media Play/Pause key on the keyboard or TV remote.
2293+
/// </summary>
2294+
/// <remarks><see cref="KeyControl"/> representing <see cref="Key.MediaPlayPause"/>.
2295+
/// Supported on Windows and Android.
2296+
/// </remarks>
2297+
public KeyControl mediaPlayPause => this[Key.MediaPlayPause];
2298+
2299+
/// <summary>
2300+
/// The Media Rewind key on the keyboard or TV remote.
2301+
/// </summary>
2302+
/// <remarks><see cref="KeyControl"/> representing <see cref="Key.MediaRewind"/>.
2303+
/// Supported on Android.
2304+
/// </remarks>
2305+
public KeyControl mediaRewind => this[Key.MediaRewind];
2306+
2307+
/// <summary>
2308+
/// The Media Forward key on the keyboard or TV remote.
2309+
/// </summary>
2310+
/// <remarks><see cref="KeyControl"/> representing <see cref="Key.MediaForward"/>.
2311+
/// Supported on Android.
2312+
/// </remarks>
2313+
public KeyControl mediaForward => this[Key.MediaForward];
2314+
22732315
/// <summary>
22742316
/// An artificial combination of <see cref="leftShiftKey"/> and <see cref="rightShiftKey"/> into one control.
22752317
/// </summary>
@@ -2531,6 +2573,9 @@ protected override void FinishSetup()
25312573
"f22",
25322574
"f23",
25332575
"f24",
2576+
nameof(mediaPlayPause),
2577+
nameof(mediaRewind),
2578+
nameof(mediaForward),
25342579
};
25352580
m_Keys = new KeyControl[keyStrings.Length];
25362581
for (var i = 0; i < keyStrings.Length; ++i)

Packages/com.unity.inputsystem/InputSystem/Devices/Precompiled/FastKeyboard.cs

Lines changed: 93 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ internal partial class FastKeyboard : UnityEngine.InputSystem.Keyboard
2424
public const string metadata = ";AnyKey;Button;Axis;Key;DiscreteButton;Keyboard";
2525
public FastKeyboard()
2626
{
27-
var builder = this.Setup(127, 15, 7)
27+
var builder = this.Setup(130, 15, 7)
2828
.WithName("Keyboard")
2929
.WithDisplayName("Keyboard")
30-
.WithChildren(0, 127)
30+
.WithChildren(0, 130)
3131
.WithLayout(new InternedString("Keyboard"))
3232
.WithStateBlock(new InputStateBlock { format = new FourCC(1262836051), sizeInBits = 128 });
3333

@@ -414,6 +414,15 @@ public FastKeyboard()
414414
// /Keyboard/f24
415415
var ctrlKeyboardf24 = Initialize_ctrlKeyboardf24(kKeyLayout, this);
416416

417+
// /Keyboard/mediaPlayPause
418+
var ctrlKeyboardmediaPlayPause = Initialize_ctrlKeyboardmediaPlayPause(kKeyLayout, this);
419+
420+
// /Keyboard/mediaRewind
421+
var ctrlKeyboardmediaRewind = Initialize_ctrlKeyboardmediaRewind(kKeyLayout, this);
422+
423+
// /Keyboard/mediaForward
424+
var ctrlKeyboardmediaForward = Initialize_ctrlKeyboardmediaForward(kKeyLayout, this);
425+
417426
// /Keyboard/IMESelected
418427
var ctrlKeyboardIMESelected = Initialize_ctrlKeyboardIMESelected(kButtonLayout, this);
419428

@@ -444,7 +453,7 @@ public FastKeyboard()
444453
builder.WithControlAlias(6, new InternedString("RightCommand"));
445454

446455
// Control getters/arrays.
447-
this.keys = new UnityEngine.InputSystem.Controls.KeyControl[123];
456+
this.keys = new UnityEngine.InputSystem.Controls.KeyControl[126];
448457
this.keys[0] = ctrlKeyboardspace;
449458
this.keys[1] = ctrlKeyboardenter;
450459
this.keys[2] = ctrlKeyboardtab;
@@ -567,6 +576,9 @@ public FastKeyboard()
567576
this.keys[120] = ctrlKeyboardf22;
568577
this.keys[121] = ctrlKeyboardf23;
569578
this.keys[122] = ctrlKeyboardf24;
579+
this.keys[123] = ctrlKeyboardmediaPlayPause;
580+
this.keys[124] = ctrlKeyboardmediaRewind;
581+
this.keys[125] = ctrlKeyboardmediaForward;
570582
this.anyKey = ctrlKeyboardanyKey;
571583
this.shiftKey = ctrlKeyboardshift;
572584
this.ctrlKey = ctrlKeyboardctrl;
@@ -576,7 +588,7 @@ public FastKeyboard()
576588
// State offset to control index map.
577589
builder.WithStateOffsetToControlIndexMap(new uint[]
578590
{
579-
525314u, 650240u, 1049603u, 1573892u, 2098181u, 2622470u, 3146759u, 3671048u, 4195337u, 4719626u
591+
525314u, 653312u, 1049603u, 1573892u, 2098181u, 2622470u, 3146759u, 3671048u, 4195337u, 4719626u
580592
, 5243915u, 5768204u, 6292493u, 6816782u, 7341071u, 7865364u, 8389653u, 8913942u, 9438231u, 9962520u
581593
, 10486809u, 11011098u, 11535387u, 12059676u, 12583965u, 13108254u, 13632543u, 14156832u, 14681121u, 15205410u
582594
, 15729699u, 16253988u, 16778277u, 17302566u, 17826855u, 18351144u, 18875433u, 19399722u, 19924011u, 20448300u
@@ -588,7 +600,7 @@ public FastKeyboard()
588600
, 45614169u, 46138458u, 46662747u, 47187036u, 47711325u, 48235614u, 48759903u, 49284193u, 49808482u, 50332771u
589601
, 50857060u, 51381349u, 51905638u, 52429927u, 52954216u, 53478505u, 54002794u, 54527083u, 55051372u, 55575661u
590602
, 56099950u, 56624239u, 57148528u, 57672817u, 58721394u, 59245683u, 59769972u, 60294261u, 60818550u, 61342839u
591-
, 61867128u, 62391417u, 62915706u, 63439995u, 63964284u, 64488573u, 66585726u
603+
, 61867128u, 62391417u, 62915706u, 63439995u, 63964284u, 64488573u, 65012862u, 65537151u, 66061440u, 66585729u
592604
});
593605

594606
builder.WithControlTree(new byte[]
@@ -652,8 +664,8 @@ public FastKeyboard()
652664
, 0, 1, 117, 0, 255, 255, 122, 0, 1, 118, 0, 255, 255, 123, 0, 1, 119, 0, 255, 255, 124, 0, 1, 123, 0, 241, 0, 0, 0, 0
653665
, 127, 0, 247, 0, 0, 0, 0, 121, 0, 243, 0, 0, 0, 0, 123, 0, 245, 0, 0, 0, 0, 120, 0, 255, 255, 125, 0, 1, 121, 0
654666
, 255, 255, 126, 0, 1, 122, 0, 255, 255, 127, 0, 1, 123, 0, 255, 255, 128, 0, 1, 125, 0, 249, 0, 0, 0, 0, 127, 0, 251, 0
655-
, 0, 0, 0, 124, 0, 255, 255, 129, 0, 1, 125, 0, 255, 255, 0, 0, 0, 126, 0, 255, 255, 0, 0, 0, 127, 0, 253, 0, 0, 0
656-
, 0, 127, 0, 255, 255, 0, 0, 0, 127, 0, 255, 0, 0, 0, 0, 128, 0, 255, 255, 130, 0, 1, 127, 0, 255, 255, 0, 0, 0
667+
, 0, 0, 0, 124, 0, 255, 255, 129, 0, 1, 125, 0, 255, 255, 130, 0, 1, 126, 0, 255, 255, 131, 0, 1, 127, 0, 253, 0, 132, 0
668+
, 1, 127, 0, 255, 255, 0, 0, 0, 127, 0, 255, 0, 0, 0, 0, 128, 0, 255, 255, 133, 0, 1, 127, 0, 255, 255, 0, 0, 0
657669
}, new ushort[]
658670
{
659671
// Control tree node indicies
@@ -662,7 +674,7 @@ public FastKeyboard()
662674
, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58
663675
, 58, 59, 60, 61, 61, 62, 63, 64, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85
664676
, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115
665-
, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126
677+
, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129
666678
});
667679

668680
builder.Finish();
@@ -684,7 +696,7 @@ private UnityEngine.InputSystem.Controls.AnyKeyControl Initialize_ctrlKeyboardan
684696
format = new FourCC(1112101920),
685697
byteOffset = 0,
686698
bitOffset = 1,
687-
sizeInBits = 123
699+
sizeInBits = 126
688700
})
689701
.WithMinAndMax(0, 1)
690702
.Finish();
@@ -2075,7 +2087,7 @@ private UnityEngine.InputSystem.Controls.KeyControl Initialize_ctrlKeyboardright
20752087
})
20762088
.WithMinAndMax(0, 1)
20772089
.Finish();
2078-
ctrlKeyboardrightAlt.keyCode = UnityEngine.InputSystem.Key.RightAlt;
2090+
ctrlKeyboardrightAlt.keyCode = UnityEngine.InputSystem.Key.AltGr;
20792091
return ctrlKeyboardrightAlt;
20802092
}
20812093

@@ -2196,7 +2208,7 @@ private UnityEngine.InputSystem.Controls.KeyControl Initialize_ctrlKeyboardleftM
21962208
})
21972209
.WithMinAndMax(0, 1)
21982210
.Finish();
2199-
ctrlKeyboardleftMeta.keyCode = UnityEngine.InputSystem.Key.LeftWindows;
2211+
ctrlKeyboardleftMeta.keyCode = UnityEngine.InputSystem.Key.LeftCommand;
22002212
return ctrlKeyboardleftMeta;
22012213
}
22022214

@@ -3583,11 +3595,80 @@ private UnityEngine.InputSystem.Controls.KeyControl Initialize_ctrlKeyboardf24(I
35833595
return ctrlKeyboardf24;
35843596
}
35853597

3598+
private UnityEngine.InputSystem.Controls.KeyControl Initialize_ctrlKeyboardmediaPlayPause(InternedString kKeyLayout, InputControl parent)
3599+
{
3600+
var ctrlKeyboardmediaPlayPause = new UnityEngine.InputSystem.Controls.KeyControl();
3601+
ctrlKeyboardmediaPlayPause.Setup()
3602+
.At(this, 126)
3603+
.WithParent(parent)
3604+
.WithName("mediaPlayPause")
3605+
.WithDisplayName("MediaPlayPause")
3606+
.WithLayout(kKeyLayout)
3607+
.IsButton(true)
3608+
.WithStateBlock(new InputStateBlock
3609+
{
3610+
format = new FourCC(1112101920),
3611+
byteOffset = 0,
3612+
bitOffset = 124,
3613+
sizeInBits = 1
3614+
})
3615+
.WithMinAndMax(0, 1)
3616+
.Finish();
3617+
ctrlKeyboardmediaPlayPause.keyCode = UnityEngine.InputSystem.Key.MediaPlayPause;
3618+
return ctrlKeyboardmediaPlayPause;
3619+
}
3620+
3621+
private UnityEngine.InputSystem.Controls.KeyControl Initialize_ctrlKeyboardmediaRewind(InternedString kKeyLayout, InputControl parent)
3622+
{
3623+
var ctrlKeyboardmediaRewind = new UnityEngine.InputSystem.Controls.KeyControl();
3624+
ctrlKeyboardmediaRewind.Setup()
3625+
.At(this, 127)
3626+
.WithParent(parent)
3627+
.WithName("mediaRewind")
3628+
.WithDisplayName("MediaRewind")
3629+
.WithLayout(kKeyLayout)
3630+
.IsButton(true)
3631+
.WithStateBlock(new InputStateBlock
3632+
{
3633+
format = new FourCC(1112101920),
3634+
byteOffset = 0,
3635+
bitOffset = 125,
3636+
sizeInBits = 1
3637+
})
3638+
.WithMinAndMax(0, 1)
3639+
.Finish();
3640+
ctrlKeyboardmediaRewind.keyCode = UnityEngine.InputSystem.Key.MediaRewind;
3641+
return ctrlKeyboardmediaRewind;
3642+
}
3643+
3644+
private UnityEngine.InputSystem.Controls.KeyControl Initialize_ctrlKeyboardmediaForward(InternedString kKeyLayout, InputControl parent)
3645+
{
3646+
var ctrlKeyboardmediaForward = new UnityEngine.InputSystem.Controls.KeyControl();
3647+
ctrlKeyboardmediaForward.Setup()
3648+
.At(this, 128)
3649+
.WithParent(parent)
3650+
.WithName("mediaForward")
3651+
.WithDisplayName("MediaForward")
3652+
.WithLayout(kKeyLayout)
3653+
.IsButton(true)
3654+
.WithStateBlock(new InputStateBlock
3655+
{
3656+
format = new FourCC(1112101920),
3657+
byteOffset = 0,
3658+
bitOffset = 126,
3659+
sizeInBits = 1
3660+
})
3661+
.WithMinAndMax(0, 1)
3662+
.Finish();
3663+
ctrlKeyboardmediaForward.keyCode = UnityEngine.InputSystem.Key.MediaForward;
3664+
return ctrlKeyboardmediaForward;
3665+
}
3666+
35863667
private UnityEngine.InputSystem.Controls.ButtonControl Initialize_ctrlKeyboardIMESelected(InternedString kButtonLayout, InputControl parent)
35873668
{
35883669
var ctrlKeyboardIMESelected = new UnityEngine.InputSystem.Controls.ButtonControl();
35893670
ctrlKeyboardIMESelected.Setup()
3590-
.At(this, 126)
3671+
.At(this, 129)
35913672
.WithParent(parent)
35923673
.WithName("IMESelected")
35933674
.WithDisplayName("IMESelected")

Packages/com.unity.inputsystem/InputSystem/Editor/InputSystemPackageControl.cs.meta

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.unity.inputsystem/InputSystem/Editor/InputSystemPluginControl.cs.meta

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.unity.inputsystem/InputSystem/InputExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ public static bool IsTextInputKey(this Key key)
145145
case Key.OEM3:
146146
case Key.OEM4:
147147
case Key.OEM5:
148+
case Key.MediaPlayPause:
149+
case Key.MediaForward:
150+
case Key.MediaRewind:
148151
case KeyEx.IMESelected:
149152
return false;
150153
}

0 commit comments

Comments
 (0)