Skip to content

FIX: Touchscreen losing focus then input fails on UI afterwards#2431

Open
Darren-Kelly-Unity wants to merge 8 commits into
developfrom
bugfix/UUM-139662-touchscreen-lost-focus-ui-input-breaks
Open

FIX: Touchscreen losing focus then input fails on UI afterwards#2431
Darren-Kelly-Unity wants to merge 8 commits into
developfrom
bugfix/UUM-139662-touchscreen-lost-focus-ui-input-breaks

Conversation

@Darren-Kelly-Unity

@Darren-Kelly-Unity Darren-Kelly-Unity commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator

Purpose of this PR

Fixes a bug where UI Toolkit elements become unresponsive to touchscreen input after an Alt+Tab cycle on Windows. The root cause was that InputSystemProvider.OnFocusChanged() did not reset pointer state on focus loss, leaving PointerState.ClickCount (and other fields) stale across the focus boundary. The fix assigns = default to all three pointer states in OnFocusChanged(false), which zeroes every field including ClickCount — a full reset that .Reset() alone does not provide.

Release Notes

  • [Input System] Fixed touchscreen UI Toolkit elements (sliders, radio buttons, etc.) becoming unresponsive after Alt+Tabbing away from and back to a Windows player build.

Functional Testing status

  • Automated: new regression test AfterAltTab_FirstTouchPress_GeneratesCleanButtonPressedEvent in InputForUIFocusRegressionTests — fails on the unfixed code (clickCount=2), passes after the fix (clickCount=1).
  • Manual: not possible to automate the full OS focus cycle; verified via the test that the fix exercises the correct code path (m_Provider.OnFocusChanged is called directly since ScheduleFocusChangedEvent drives only the InputManager BackgroundBehavior path, not the IEventProviderImpl path).
  • Tested background behavior: ResetAndDisableNonBackgroundDevices.

Performance Testing Status

No performance impact. The change adds a small number of field assignments inside OnFocusChanged, which is called at most twice per Alt+Tab cycle.

Overall Product Risks

  • Technical Risk: 1 — Change is isolated to OnFocusChanged, a method that only fires on application focus transitions. No hot-path code affected.
  • Halo Effect: 1 — Resetting pointer state on focus loss could affect pen/mouse event handling across an Alt+Tab boundary, but this matches the documented intent and mirrors how InputManagerProvider already behaves (it polls fresh state every frame so stale state is never possible there).

Class diagram

Analyzed range: origin/main...HEAD

InputSystemProvider — OnFocusChanged pointer-state reset
classDiagram
    class IEventProviderImpl {
        <<interface>>
    }

    class PointerState {
    }

    class InputSystemProvider {
        -PointerState m_MouseState
        -PointerState m_TouchState
        -PointerState m_PenState
        +OnFocusChanged(bool focus)
    }

    InputSystemProvider ..|> IEventProviderImpl
    InputSystemProvider "1" *-- "3" PointerState

    note for InputSystemProvider "OnFocusChanged now resets\nm_MouseState, m_TouchState,\nand m_PenState on focus loss"
Loading

@codecov-github-com

codecov-github-com Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 97.01493% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
.../Runtime/Plugins/InputForUI/InputSystemProvider.cs 89.47% 2 Missing ⚠️
@@             Coverage Diff              @@
##           develop    #2431       +/-   ##
============================================
+ Coverage    58.58%   79.02%   +20.44%     
============================================
  Files          738      766       +28     
  Lines       135831   140408     +4577     
============================================
+ Hits         79570   110962    +31392     
+ Misses       56261    29446    -26815     
Flag Coverage Δ
inputsystem_MacOS_6000.0 5.31% <0.00%> (-0.01%) ⬇️
inputsystem_MacOS_6000.0_project 77.49% <97.01%> (+0.23%) ⬆️
inputsystem_MacOS_6000.3 5.31% <0.00%> (-0.01%) ⬇️
inputsystem_MacOS_6000.3_project 77.48% <97.01%> (+0.26%) ⬆️
inputsystem_MacOS_6000.4 5.32% <0.00%> (-0.01%) ⬇️
inputsystem_MacOS_6000.4_project 77.49% <97.01%> (+0.26%) ⬆️
inputsystem_MacOS_6000.5 5.30% <0.00%> (-0.01%) ⬇️
inputsystem_MacOS_6000.5_project 77.53% <97.01%> (+0.26%) ⬆️
inputsystem_MacOS_6000.6 5.30% <0.00%> (-0.01%) ⬇️
inputsystem_MacOS_6000.6_project 77.53% <97.01%> (+0.26%) ⬆️
inputsystem_Ubuntu_6000.0 5.32% <0.00%> (-0.01%) ⬇️
inputsystem_Ubuntu_6000.0_project 77.39% <97.01%> (+0.24%) ⬆️
inputsystem_Ubuntu_6000.3 5.32% <0.00%> (-0.01%) ⬇️
inputsystem_Ubuntu_6000.3_project 77.39% <97.01%> (+0.26%) ⬆️
inputsystem_Ubuntu_6000.4 5.32% <0.00%> (-0.01%) ⬇️
inputsystem_Ubuntu_6000.4_project 77.40% <97.01%> (+0.26%) ⬆️
inputsystem_Ubuntu_6000.5 5.31% <0.00%> (-0.01%) ⬇️
inputsystem_Ubuntu_6000.5_project 77.44% <97.01%> (+0.27%) ⬆️
inputsystem_Ubuntu_6000.6 5.31% <0.00%> (-0.01%) ⬇️
inputsystem_Ubuntu_6000.6_project 77.43% <97.01%> (+0.27%) ⬆️
inputsystem_Windows_6000.0 5.31% <0.00%> (-0.01%) ⬇️
inputsystem_Windows_6000.0_project 77.61% <97.01%> (+0.24%) ⬆️
inputsystem_Windows_6000.3 5.31% <0.00%> (-0.01%) ⬇️
inputsystem_Windows_6000.3_project 77.60% <97.01%> (+0.26%) ⬆️
inputsystem_Windows_6000.4 5.32% <0.00%> (-0.01%) ⬇️
inputsystem_Windows_6000.4_project 77.61% <97.01%> (+0.26%) ⬆️
inputsystem_Windows_6000.5 5.30% <0.00%> (-0.01%) ⬇️
inputsystem_Windows_6000.5_project 77.66% <97.01%> (+0.26%) ⬆️
inputsystem_Windows_6000.6 5.30% <0.00%> (-0.01%) ⬇️
inputsystem_Windows_6000.6_project 77.66% <97.01%> (+0.26%) ⬆️

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

Files with missing lines Coverage Δ
...utSystem/Plugins/InputForUIFocusRegressionTests.cs 100.00% <100.00%> (ø)
.../Runtime/Plugins/InputForUI/InputSystemProvider.cs 88.57% <89.47%> (+88.57%) ⬆️

... and 278 files with indirect coverage changes

ℹ️ Need help interpreting these results?

@u-pr u-pr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

May require changes

This fix for touchscreen focus loss correctly addresses the reported issue, but clearing the mouse state completely introduces a regression where initial clicks after regaining focus may default to (0,0). Additionally, resetting the repeat helper on focus loss would ensure consistent navigation state.

🤖 Helpful? 👍/👎

@Darren-Kelly-Unity

Copy link
Copy Markdown
Collaborator Author

/review

@u-pr u-pr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perfect

Focused fix for touchscreen input unresponsiveness after focus loss.

🤖 Helpful? 👍/👎

@Pauliusd01

Copy link
Copy Markdown
Collaborator

/test_plan

@u-pr

u-pr Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Test Plan

(Custom_review updated until commit 5d5c193)

Updated: 2024-06-11 - Added test cases for CI failure investigation and state restoration logic.

  • Test plan approved by PR author
  • Test execution in progress
  • Test execution complete
  • Ready for merge

Summary of Changes & Risk Assessment

Summary of Changes

This PR fixes UUM-139662 where UI elements become unresponsive to touchscreen input after an Alt+Tab cycle on Windows. The fix resets m_TouchState, m_MouseState, and m_PenState to default in InputSystemProvider.OnFocusChanged(false) at InputSystemProvider.cs:L304-306. This clears stale ClickCount values while explicitly preserving the last known positions for Mouse and Pen to ensure click-to-focus functionality remains intact.

Risk Assessment

  • High Risk Areas: None identified.
  • Medium Risk Areas: State restoration logic for Mouse/Pen positions during focus transitions.
  • Low Risk Areas: Interaction state after focus transitions; CI stability for the new regression test.

Test Scenarios

Functional Testing

  • Investigate CI Failure: Analyze why AfterAltTab_FirstTouchPress_GeneratesCleanButtonPressedEvent in InputForUIFocusRegressionTests.cs:L81 is failing on CI. Verify if the manual call to m_Provider.OnFocusChanged conflicts with the ScheduleFocusChangedEvent timing.
  • Verify Touch Reset on Focus Loss: Confirm clickCount resets to 1 after a focus cycle using the new regression test.
  • Verify Mouse Position Preservation: Perform a focus loss and gain cycle. Verify that mouseLastPos and mouseLastDisplay are correctly restored via m_MouseState.OnMove at InputSystemProvider.cs:L309.
  • Verify Pen Position Preservation: Similar to mouse, verify pen position and display index are restored using InputSystemProvider.cs:L311.

Regression Testing

  • Multi-Tap Logic: Ensure double-clicking the mouse/pen still works within a single focus session (no focus loss).
  • Event Queue Clearing: Verify m_Events.Clear() in InputSystemProvider.cs:L315 correctly flushes any pending UI events to prevent "ghost" inputs on regain.
🔍 Regression Deep Dive (additional risks identified)
  • Drag Interruption: Verify behavior when focus is lost during an active Drag-and-Drop operation. The reset should cancel the drag state.
  • State Preservation Timestamp: Ensure m_CurrentTime passed to OnMove at InputSystemProvider.cs:L309 doesn't cause unexpected event ordering if NotifyUpdate was already called.

Edge Cases

  • Focus Loss During Active Touch: Verify that if focus is lost while a finger is down, the state is zeroed and focus regain requires a new BeginTouch.
  • Rapid Focus Toggling: Quickly toggle focus state to ensure no race conditions occur within the state assignments in OnFocusChanged.

💡 This test plan updates automatically when /test_plan is run on new commits. If you have any feedback, please reach out in #ai-qa


🤖 Helpful? Please react with 👍/👎 | Questions❓Please reach out in Slack #ask-u-pr

@MorganHoarau

Copy link
Copy Markdown
Collaborator

Your new unit test is failing on CI

@MorganHoarau MorganHoarau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

PR description is different from actual diff after you applied bot suggestion. Please update accordingly.

m_SeenTouchEvents = false;
m_SeenPenEvents = false;
m_ResetSeenEventsOnUpdate = false;
m_RepeatHelper.Reset();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why do you reset navigation repeat state here? Looks unrelated to pointer clickCount bug and no mentioned in PR description. Is there a separate bug for it maybe?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It was a suggestion that seemed to make sense from u-pr below!
https://github.com/Unity-Technologies/InputSystem/pull/2431/changes#r3388665182

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If that was not a known issue, we should at least test it first before applying to know if there is an issue in the first place. It doesn't look like it could introduce any regression, but I'm just considering that any line of code not part of fixing existing a bug might be a potential liability for subtle regression.

I haven't tested locally so I'll let QA confirm

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

reverted it for now!

@Pauliusd01

This comment was marked as off-topic.

@u-pr

u-pr Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Persistent custom_review updated to latest commit 5d5c193

@Pauliusd01

This comment was marked as outdated.

@Pauliusd01 Pauliusd01 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sorry for taking a while, I was battling my cheap touchscreen monitor and finally got it to work in 480p. I've checked the /test_plan tests and the original bug repro with a windows touch screen, mouse and pen device.

Test scene can be found here if you ever want to double check:
PR_2431_V2.zip

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.

3 participants