Skip to content

Commit aacee0a

Browse files
committed
wip
1 parent cd53351 commit aacee0a

File tree

5 files changed

+598
-83
lines changed

5 files changed

+598
-83
lines changed

Assets/Tests/InputSystem/CorePerformanceTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,14 @@ public void Performance_ReadEveryKey()
123123

124124
Measure.Method(() =>
125125
{
126+
int keyIndex = 0;
126127
foreach (var key in keyboard.allKeys)
128+
{
129+
if (++keyIndex == (int) KeyEx.IMESelected) // Skip IMESelected as it's not a real key.
130+
continue;
127131
key.ReadValue();
132+
133+
}
128134
})
129135
.MeasurementCount(100)
130136
.WarmupCount(5)

Assets/Tests/InputSystem/CoreTests_Devices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2674,7 +2674,7 @@ public void Devices_AnyKeyOnKeyboard_DoesNotReactToIMESelected()
26742674
{
26752675
var keyboard = InputSystem.AddDevice<Keyboard>();
26762676

2677-
InputSystem.QueueStateEvent(keyboard, new KeyboardState(Key.IMESelected));
2677+
InputSystem.QueueStateEvent(keyboard, new KeyboardState(IMESelected: true));
26782678
InputSystem.Update();
26792679

26802680
Assert.That(keyboard.anyKey.isPressed, Is.False);

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

Lines changed: 177 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public unsafe struct KeyboardState : IInputStateTypeInfo
3838
/// <seealso cref="InputStateBlock.format"/>
3939
public static FourCC Format => new FourCC('K', 'E', 'Y', 'S');
4040

41-
private const int kSizeInBits = Keyboard.KeyCount;
41+
private const int kSizeInBits = Keyboard.KeyCount + 1; // +1 for IMESelected.
4242
internal const int kSizeInBytes = (kSizeInBits + 7) / 8;
4343

44-
[InputControl(name = "anyKey", displayName = "Any Key", layout = "AnyKey", sizeInBits = kSizeInBits - 1, synthetic = true)] // Exclude IMESelected.
44+
[InputControl(name = "anyKey", displayName = "Any Key", layout = "AnyKey", offset = 1, sizeInBits = (int)Key.F24, synthetic = true)]
4545
[InputControl(name = "escape", displayName = "Escape", layout = "Key", usages = new[] {"Back", "Cancel"}, bit = (int)Key.Escape)]
4646
[InputControl(name = "space", displayName = "Space", layout = "Key", bit = (int)Key.Space)]
4747
[InputControl(name = "enter", displayName = "Enter", layout = "Key", usage = "Submit", bit = (int)Key.Enter)]
@@ -155,17 +155,42 @@ public unsafe struct KeyboardState : IInputStateTypeInfo
155155
[InputControl(name = "OEM3", layout = "Key", bit = (int)Key.OEM3)]
156156
[InputControl(name = "OEM4", layout = "Key", bit = (int)Key.OEM4)]
157157
[InputControl(name = "OEM5", layout = "Key", bit = (int)Key.OEM5)]
158-
[InputControl(name = "IMESelected", layout = "Button", bit = (int)Key.IMESelected, synthetic = true)]
158+
[InputControl(name = "f13", displayName = "F13", layout = "Key", bit = (int)Key.F13)]
159+
[InputControl(name = "f14", displayName = "F14", layout = "Key", bit = (int)Key.F14)]
160+
[InputControl(name = "f15", displayName = "F15", layout = "Key", bit = (int)Key.F15)]
161+
[InputControl(name = "f16", displayName = "F16", layout = "Key", bit = (int)Key.F16)]
162+
[InputControl(name = "f17", displayName = "F17", layout = "Key", bit = (int)Key.F17)]
163+
[InputControl(name = "f18", displayName = "F18", layout = "Key", bit = (int)Key.F18)]
164+
[InputControl(name = "f19", displayName = "F19", layout = "Key", bit = (int)Key.F19)]
165+
[InputControl(name = "f20", displayName = "F20", layout = "Key", bit = (int)Key.F20)]
166+
[InputControl(name = "f21", displayName = "F21", layout = "Key", bit = (int)Key.F21)]
167+
[InputControl(name = "f22", displayName = "F22", layout = "Key", bit = (int)Key.F22)]
168+
[InputControl(name = "f23", displayName = "F23", layout = "Key", bit = (int)Key.F23)]
169+
[InputControl(name = "f24", displayName = "F24", layout = "Key", bit = (int)Key.F24)]
170+
[InputControl(name = "IMESelected", layout = "Button", bit = (int)KeyEx.RemapedIMESelected, synthetic = true)] // Use the last bit to hold IME selected state.
159171
public fixed byte keys[kSizeInBytes];
160172

161-
public KeyboardState(params Key[] pressedKeys)
173+
// will be the default in new editor [InputControl(name = "IMESelected", layout = "Button", bit = 0, sizeInBits = 1, synthetic = true)]
174+
//public byte modifiers;
175+
176+
public KeyboardState(params Key[] pressedKeys) : this(false, pressedKeys)
177+
{
178+
179+
}
180+
181+
public KeyboardState(bool IMESelected, params Key[] pressedKeys)
162182
{
163183
if (pressedKeys == null)
164184
throw new ArgumentNullException(nameof(pressedKeys));
165-
166-
fixed(byte* keysPtr = keys)
185+
fixed (byte* keysPtr = keys)
167186
{
168187
UnsafeUtility.MemClear(keysPtr, kSizeInBytes);
188+
189+
if (IMESelected)
190+
{
191+
MemoryHelpers.WriteSingleBit(keysPtr, (uint)KeyEx.IMESelected, true);
192+
}
193+
169194
for (var i = 0; i < pressedKeys.Length; ++i)
170195
MemoryHelpers.WriteSingleBit(keysPtr, (uint)pressedKeys[i], true);
171196
}
@@ -177,6 +202,14 @@ public void Set(Key key, bool state)
177202
MemoryHelpers.WriteSingleBit(keysPtr, (uint)key, state);
178203
}
179204

205+
internal bool Get(Key key)
206+
{
207+
fixed (byte* keysPtr = keys)
208+
{
209+
return MemoryHelpers.ReadSingleBit(keysPtr, (uint)key);
210+
}
211+
}
212+
180213
public void Press(Key key)
181214
{
182215
Set(key, true);
@@ -842,9 +875,86 @@ public enum Key
842875
/// </summary>
843876
OEM5,
844877

845-
////FIXME: This should never have been a Key but rather just an extra button or state on keyboard
846-
// Not exactly a key, but binary data sent by the Keyboard to say if IME is being used.
847-
IMESelected
878+
/// <summary>
879+
/// Don't use this. This is a dummy key that is only used internally to represent the IME selected state.
880+
/// Will be removed in the future.
881+
/// </summary>
882+
[Obsolete("Don't use this. This is a dummy key that is only used internally to represent the IME selected state. Will be removed in the future.", true)]
883+
IMESelected,
884+
885+
/// <summary>
886+
/// The <see cref="Keyboard.f13Key"/>.
887+
/// </summary>
888+
F13,
889+
890+
/// <summary>
891+
/// The <see cref="Keyboard.f14Key"/>.
892+
/// </summary>
893+
F14,
894+
895+
/// <summary>
896+
/// The <see cref="Keyboard.f15Key"/>.
897+
/// </summary>
898+
F15,
899+
900+
/// <summary>
901+
/// The <see cref="Keyboard.f16Key"/>.
902+
/// </summary>
903+
F16,
904+
905+
/// <summary>
906+
/// The <see cref="Keyboard.f17Key"/>.
907+
/// </summary>
908+
F17,
909+
910+
/// <summary>
911+
/// The <see cref="Keyboard.f18Key"/>.
912+
/// </summary>
913+
F18,
914+
915+
/// <summary>
916+
/// The <see cref="Keyboard.f19Key"/>.
917+
/// </summary>
918+
F19,
919+
920+
/// <summary>
921+
/// The <see cref="Keyboard.f20Key"/>.
922+
/// </summary>
923+
F20,
924+
925+
/// <summary>
926+
/// The <see cref="Keyboard.f21Key"/>.
927+
/// </summary>
928+
F21,
929+
930+
/// <summary>
931+
/// The <see cref="Keyboard.f22Key"/>.
932+
/// </summary>
933+
F22,
934+
935+
/// <summary>
936+
/// The <see cref="Keyboard.f23Key"/>.
937+
/// </summary>
938+
F23,
939+
940+
/// <summary>
941+
/// The <see cref="Keyboard.f24Key"/>.
942+
/// </summary>
943+
F24,
944+
945+
/// <summary>
946+
/// Don't use this. This is a dummy key that is only used internally to represent the IME selected state.
947+
/// Will be removed in the future.
948+
/// FIXME: This should never have been a Key but rather just an extra button or state on keyboard
949+
/// Not exactly a key, but binary data sent by the Keyboard to say if IME is being used.
950+
/// </summary>
951+
//InternalForIMESelected = 127,
952+
}
953+
954+
internal static class KeyEx
955+
{
956+
internal const Key IMESelected = (Key)111; //IMESelected value
957+
internal const Key RemapedIMESelected = (Key)127; //IMESelected value
848958
}
849959

850960
/// <summary>
@@ -877,14 +987,15 @@ public enum Key
877987
/// keyboard has certain keys or not.
878988
/// </remarks>
879989
[InputControlLayout(stateType = typeof(KeyboardState), isGenericTypeOfDevice = true)]
880-
public class Keyboard : InputDevice, ITextInputReceiver
990+
public class Keyboard : InputDevice, ITextInputReceiver, IEventPreProcessor
881991
{
882992
/// <summary>
883993
/// Total number of key controls on a keyboard, i.e. the number of controls
884994
/// in <see cref="allKeys"/>.
885995
/// </summary>
886996
/// <value>Total number of key controls.</value>
887-
public const int KeyCount = (int)Key.OEM5;
997+
public const int KeyCount = (int)Key.F24 - 1; // without IMESelected
998+
// without IMESelected
888999

8891000
/// <summary>
8901001
/// Event that is fired for every single character entered on the keyboard.
@@ -1862,6 +1973,7 @@ public string keyboardLayout
18621973
/// <value>Control representing the F12 key.</value>
18631974
public KeyControl f12Key => this[Key.F12];
18641975

1976+
18651977
/// <summary>
18661978
/// First additional key on the keyboard.
18671979
/// </summary>
@@ -2149,17 +2261,32 @@ protected override void FinishSetup()
21492261
"oem3",
21502262
"oem4",
21512263
"oem5",
2264+
null, // IMESelected
2265+
"f13",
2266+
"f14",
2267+
"f15",
2268+
"f16",
2269+
"f17",
2270+
"f18",
2271+
"f19",
2272+
"f20",
2273+
"f21",
2274+
"f22",
2275+
"f23",
2276+
"f24",
21522277
};
21532278
m_Keys = new KeyControl[keyStrings.Length];
21542279
for (var i = 0; i < keyStrings.Length; ++i)
21552280
{
2281+
if (string.IsNullOrEmpty(keyStrings[i]))
2282+
continue;
21562283
m_Keys[i] = GetChildControl<KeyControl>(keyStrings[i]);
21572284

21582285
////REVIEW: Ideally, we'd have a way to do this through layouts; this way nested key controls could work, too,
21592286
//// and it just seems somewhat dirty to jam the data into the control here
21602287
m_Keys[i].keyCode = (Key)(i + 1);
21612288
}
2162-
Debug.Assert(keyStrings[(int)Key.OEM5 - 1] == "oem5",
2289+
Debug.Assert(keyStrings[(int)Key.F24 - 1] == "f24",
21632290
"keyString array layout doe not match Key enum layout");
21642291
anyKey = GetChildControl<AnyKeyControl>("anyKey");
21652292
shiftKey = GetChildControl<ButtonControl>("shift");
@@ -2228,6 +2355,44 @@ public void OnIMECompositionChanged(IMECompositionString compositionString)
22282355
}
22292356
}
22302357

2358+
public unsafe bool PreProcessEvent(InputEventPtr currentEventPtr)
2359+
{
2360+
if (currentEventPtr.type == StateEvent.Type)
2361+
{
2362+
var stateEvent = StateEvent.FromUnchecked(currentEventPtr);
2363+
if (stateEvent->stateFormat == KeyboardState.Format)
2364+
{
2365+
var keyboardState = ((KeyboardState*)(stateEvent->stateData));
2366+
if(keyboardState->Get(KeyEx.IMESelected))
2367+
{
2368+
keyboardState->Set(KeyEx.IMESelected, false);
2369+
keyboardState->Set(KeyEx.RemapedIMESelected, true);
2370+
}
2371+
2372+
//stateEvent->stateSizeInBytes = KeyboardState.kSizeInBytes;
2373+
2374+
//keyboardState->
2375+
//if (stateEvent->)
2376+
//var imeSelected = ((KeyboardState*)(stateEvent))->Get(Key.InternalForIMESelected);
2377+
2378+
//((KeyboardState*)(stateEvent))->Set(Key.IMESelected, ((KeyboardState*)(stateEvent))->Get(Key.DiscardedIMESelected));
2379+
////((KeyboardState*)(stateEvent))->keys[]
2380+
//if (stateEvent->stateSizeInBytes != KeyboardState.kSizeInBytes)
2381+
//{
2382+
// //imeSelected
2383+
// ((KeyboardState*)(stateEvent))->Set(Key.IMESelected, ((KeyboardState*)(stateEvent))->Get(Key.DiscardedIMESelected));
2384+
// ((KeyboardState*)(stateEvent))->Set(Key.DiscardedIMESelected, false);
2385+
// Debug.Log($"FastKeyboard: StateEvent size mismatch {stateEvent->stateSizeInBytes} {KeyboardState.kSizeInBytes}");
2386+
//}
2387+
2388+
}
2389+
2390+
2391+
}
2392+
2393+
return true;
2394+
}
2395+
22312396
private InlinedArray<Action<char>> m_TextInputListeners;
22322397
private string m_KeyboardLayoutName;
22332398
private KeyControl[] m_Keys;

0 commit comments

Comments
 (0)