How to reproduce:
1. Open the attached “AltTab” project
2. Build and Run the project with the “SampleScene” Scene
3. Alt+Tab out of the Build and back
Expected result: The Scene is colored blue (anyKey.isPressed is no longer running)
Actual result: The Scene is colored red (anyKey.isPressed is still running)
Reproducible with: 1.4.4 (2021.3.15f1)
Reproduced on: Windows 10
Note:
UPDATE:
This behavior occurs because the InputSystem's Background Behavior setting is set to "Ignore Focus", which is by design.
When "Ignore Focus" setting is used, InputSystem does nothing to reset or re-sync devices when focus is regained, meaning the keyboard state will not automatically update when switching back to the app. In this scenario, since Windows only send key events to the focused app, the "KeyUp" message for the Alt key is missed by InputSystem causing isPressed to report true when focus is regained.
When using the "Ignore Focus" setting, the app must manually issue a sync request to ensure the keyboard (and possibly other devices) are updated.
For example:
Application.focusChanged += (bool value) =>
{
if (!value) return;
foreach (var device in InputSystem.devices)
{
InputSystem.TrySyncDevice(device);
}
};
Admittedly the documentation for this setting isn't very clear on this (at least to me), and so we'll update it to be more explicit.
In general, the Background Behavior settings is intended for very specific scenarios and shouldn't be changed unless necessary. For details on this setting, it's intended purpose, and expected functionality please see this PR: https://github.com/Unity-Technologies/InputSystem/pull/1324