Skip to content

Conversation

ekcoh
Copy link
Collaborator

@ekcoh ekcoh commented Sep 3, 2025

Description

Provides a suggested bug fix for https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1541 which is reported as a regression for 1.14.0 due to #2075.

In 1.13.1 subscript operator throws ArgumentOutOfRangeException for Key.IMESelected and Key enumeration type contain IMESelected defined as 111.

Identified the following potential solutions:

  1. Since IMESelected was accidentally added to Key enumeration type when Input System was first released and later marked obsolete in NEW: Added support for F13 to F24 keys #2075, make sure we still provide a KeyControl for IMESelected but advise to not rely on it due to obsoletion status. Let IMESelected be part of Keyboard.allKeys.
  2. Exclude IMESelected from allKeys (since it is not a key). This is cleaner since it is inline with 1.13.1 where IMESelected also wasn't part of allKeys.

This PR contains a fix according to 1). Investigating possibility for 2), hence draft status.

Summary of changes:

  • Addressed issue by defining an aliased KeyControl for IMESelected bit and returning this KeyControl for any sub-script operator look-up using the deprecated key Key.IMESelected. This should be removed when IMESelected can be removed from API. This fix should be ok given the following API contracts:
    • IMESelected key control part of Keyboard.keys which makes sense since it has incorrectly been exposed as a key in Key enumeration type from version 1.0.
    • Sub-script operator states "Look up a key control by its key code." It isn't specific for whether KeyControl may be null or not so in theory a fix here could be to return null for Key.IMESelected since it is not a key. However, it also states "This is equivalent to allKeys[(int)key - 1]" which implies that result of this operation should be equivalent to subtracting one from key and looking it up via allKeys. This constraint is now also tested via Device_Keyboard_SubscriptOperatorShouldBeEquivalentToShiftedAllKeysLookup to capture this requirement.
  • Added a new test class CoreTests_Device_Keyboard.cs and added some missing new test cases tied to Keyboard to this class. I removed some obsolete test cases from CoreTests_Devices.cs. I did not port existing keyboard tests from CoreTests_Devices.cs to the new file as part of this PR since this could be done separately and would make the PR more difficult to review. Hopefully the extended functional tests increase test coverage of keyboard functionality since it turned out many key properties were never accessed in tests. I introduced this file since a single enormous test file just makes it difficult to assess what is being tested. Generally all individual devices need their dedicated tests (which is the case for plugin devices but not for built-in it seems).
  • Updated FastKeyboard.cs (precompiled layout) to match changes to Keyboard.cs.

Testing status & QA

Fix should be covered by new (and existing) automated tests, but it is suggested to test keyboard functionality and IME as part of quality assuring this PR. Fix developed and tested on Unity 6000.0.56f1.

Open issue: Check if this really is a regression - investigate behaviour on version before 1.14.0 for using subscript operator with IMESelected before transitioning this out of draft mode.

Overall Product Risks

Medium due to central functionality.

  • Complexity: Small
  • Halo Effect: Medium

Comments to reviewers

Nothing special to call out apart from that it should be mentioned there is still no property API for IMESelected which is intentional to avoid accessing it.

Checklist

Before review:

  • Changelog entry added.
    • Explains the change in Changed, Fixed, Added sections.
    • For API change contains an example snippet and/or migration example.
    • JIRA ticket linked, example (case %%). If it is a private issue, just add the case ID without a link.
    • Jira port for the next release set as "Resolved".
  • Tests added/changed, if applicable.
    • Functional tests added Devices_Keyboard_CanGetKeyCodeFromKeyboardKey, Devices_Keyboard_AllKeysEnumeratesAllKeyControls, Devices_Keyboard_AllKeysShouldContainKeyControlsCorrespondingToAllKeys, Devices_Keyboard_SubscriptOperatorCanLookupKeyControlOfCorrespondingKey, Devices_Keyboard_SubscriptOperatorThrowsForInvalidOrOutOfRangeKey.
  • Docs for new/changed API's.
    • Xmldoc cross references are set correctly.
    • Added explanation how the API works.
    • Usage code examples added.
    • The manual is updated, if needed.

During merge:

  • Commit message for squash-merge is prefixed with one of the list:
    • NEW: ___.
    • FIX: ___.
    • DOCS: ___.
    • CHANGE: ___.
    • RELEASE: 1.1.0-preview.3.

After merge:

  • Create forward/backward port if needed. If you are blocked from creating a forward port now please add a task to ISX-1444.

@ekcoh ekcoh changed the title FIX: [ISXB-1541] Sub-script operator returns ´null´KeyControl for deprecated key Key.IMESelected. FIX: [ISXB-1541] Sub-script operator returns null KeyControl for deprecated key Key.IMESelected. Sep 3, 2025
@codecov-github-com
Copy link

codecov-github-com bot commented Sep 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

@@           Coverage Diff            @@
##           develop    #2230   +/-   ##
========================================
  Coverage    68.15%   68.16%           
========================================
  Files          367      367           
  Lines        53661    53668    +7     
========================================
+ Hits         36572    36581    +9     
+ Misses       17089    17087    -2     
Flag Coverage Δ
linux_2021.3_pkg 5.47% <81.81%> (+0.01%) ⬆️
linux_2021.3_project 70.40% <100.00%> (+<0.01%) ⬆️
linux_2022.3_pkg 5.24% <81.81%> (+0.01%) ⬆️
linux_2022.3_project 65.25% <100.00%> (+<0.01%) ⬆️
linux_6000.0_pkg 5.25% <81.81%> (+0.01%) ⬆️
linux_6000.0_project 68.05% <100.00%> (+<0.01%) ⬆️
linux_6000.2_pkg 5.25% <81.81%> (+0.01%) ⬆️
linux_6000.2_project 68.05% <100.00%> (+<0.01%) ⬆️
linux_trunk_pkg 5.25% <81.81%> (+0.01%) ⬆️
linux_trunk_project 68.05% <100.00%> (+<0.01%) ⬆️
mac_2021.3_pkg 5.46% <81.81%> (+0.01%) ⬆️
mac_2021.3_project 70.41% <100.00%> (+<0.01%) ⬆️
mac_2022.3_pkg 5.24% <81.81%> (+0.01%) ⬆️
mac_2022.3_project 65.27% <100.00%> (+<0.01%) ⬆️
mac_6000.0_pkg 5.24% <81.81%> (+0.01%) ⬆️
mac_6000.0_project 68.08% <100.00%> (+<0.01%) ⬆️
mac_6000.2_pkg 5.24% <81.81%> (+0.01%) ⬆️
mac_6000.2_project 68.07% <100.00%> (+<0.01%) ⬆️
mac_trunk_pkg 5.25% <81.81%> (+0.01%) ⬆️
mac_trunk_project 68.09% <100.00%> (+<0.01%) ⬆️
win_2021.3_pkg 5.47% <81.81%> (+0.01%) ⬆️
win_2021.3_project 70.49% <100.00%> (+<0.01%) ⬆️
win_2022.3_pkg 5.24% <81.81%> (+0.01%) ⬆️
win_2022.3_project 65.35% <100.00%> (+<0.01%) ⬆️
win_6000.0_pkg 5.25% <81.81%> (+0.01%) ⬆️
win_6000.0_project 68.15% <100.00%> (+<0.01%) ⬆️
win_6000.2_pkg 5.25% <81.81%> (+0.01%) ⬆️
win_6000.2_project 68.15% <100.00%> (+<0.01%) ⬆️
win_trunk_pkg 5.25% <81.81%> (+0.01%) ⬆️
win_trunk_project 68.15% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
....unity.inputsystem/InputSystem/Devices/Keyboard.cs 95.60% <100.00%> (+0.34%) ⬆️
...em/InputSystem/Devices/Precompiled/FastKeyboard.cs 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ekcoh ekcoh changed the title FIX: [ISXB-1541] Sub-script operator returns null KeyControl for deprecated key Key.IMESelected. FIX: [ISXB-1541] Keyboard.allKeys contain null KeyControl for deprecated key Key.IMESelected which results in exception if using sub-script operator for lookup during enumeration of allKeys. Sep 3, 2025
@ekcoh ekcoh marked this pull request as ready for review September 3, 2025 14:25
@ekcoh
Copy link
Collaborator Author

ekcoh commented Sep 3, 2025

Opening this up for review for now since code generator seem to need to be fixed/changed to cope with nonconsecutive enum. So option (2) I haven't been able to solve yet. Another option would be to optionally just return null from subscript-operator if key is IMESelected.

{
throw new ArgumentOutOfRangeException($"{nameof(key)}: {key}");
}
return m_Keys[index];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am leaving towards it would be better to return null for index == (int)Key.IMESelected - 1?

if (index < 0 || index >= m_Keys.Length)
throw new ArgumentOutOfRangeException(nameof(key));
{
throw new ArgumentOutOfRangeException($"{nameof(key)}: {key}");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's very odd Codecov is mentioning these lines since there is now a test explicitly testing key/index outside of bounds. Need to investigate if the test is really running in CI (it did locally I believe)

@Pauliusd01
Copy link
Collaborator

Do different platforms have relevance here? Do I need to check Mac/Linux?

Copy link
Collaborator

@jfreire-unity jfreire-unity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some questions but they shouldn't block the PR since it seems that it fixes the bug. Tests look good!

I definetly wished we could just obsolete and remove this :)

@ekcoh
Copy link
Collaborator Author

ekcoh commented Sep 5, 2025

Do different platforms have relevance here? Do I need to check Mac/Linux?

Platform shouldn't matter as far as I am aware. There is no platform specific code at play here for the scope of this PR.

Copy link
Collaborator

@Pauliusd01 Pauliusd01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, checked user project as well as IME testing scene in player/playmode using Japanese, Chinese, Russian, Hebrew, Arabic and Turkish

…in allKeys or if it is present it should not evaluate to a non-null control.
@ekcoh
Copy link
Collaborator Author

ekcoh commented Sep 5, 2025

Added some questions but they shouldn't block the PR since it seems that it fixes the bug. Tests look good!

I definetly wished we could just obsolete and remove this :)

Addressed in b803b1d

@ekcoh ekcoh merged commit e0d9edc into develop Sep 9, 2025
112 of 114 checks passed
@ekcoh ekcoh deleted the isxb-1541-keyboard-key-enumeration-issue branch September 9, 2025 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants