Skip to content

Commit 3aa49cc

Browse files
Remove IsScreenReaderActive() check from ConsoleHost (#26118)
1 parent 3cfa4b2 commit 3aa49cc

File tree

3 files changed

+12
-117
lines changed

3 files changed

+12
-117
lines changed

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,11 @@ internal sealed partial class ConsoleHost
5454
internal const int ExitCodeCtrlBreak = 128 + 21; // SIGBREAK
5555
internal const int ExitCodeInitFailure = 70; // Internal Software Error
5656
internal const int ExitCodeBadCommandLineParameter = 64; // Command Line Usage Error
57-
private const uint SPI_GETSCREENREADER = 0x0046;
5857
#if UNIX
5958
internal const string DECCKM_ON = "\x1b[?1h";
6059
internal const string DECCKM_OFF = "\x1b[?1l";
6160
#endif
6261

63-
[DllImport("user32.dll", SetLastError = true)]
64-
[return: MarshalAs(UnmanagedType.Bool)]
65-
private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref bool pvParam, uint fWinIni);
66-
6762
/// <summary>
6863
/// Internal Entry point in msh console host implementation.
6964
/// </summary>
@@ -1680,35 +1675,6 @@ private void CreateRunspace(RunspaceCreationEventArgs runspaceCreationArgs)
16801675
}
16811676
}
16821677

1683-
/// <summary>
1684-
/// Check if a screen reviewer utility is running.
1685-
/// When a screen reader is running, we don't auto-load the PSReadLine module at startup,
1686-
/// since PSReadLine is not accessibility-friendly enough as of today.
1687-
/// </summary>
1688-
private bool IsScreenReaderActive()
1689-
{
1690-
if (_screenReaderActive.HasValue)
1691-
{
1692-
return _screenReaderActive.Value;
1693-
}
1694-
1695-
_screenReaderActive = false;
1696-
if (Platform.IsWindowsDesktop)
1697-
{
1698-
// Note: this API can detect if a third-party screen reader is active, such as NVDA, but not the in-box Windows Narrator.
1699-
// Quoted from https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-systemparametersinfoa about the
1700-
// accessibility parameter 'SPI_GETSCREENREADER':
1701-
// "Narrator, the screen reader that is included with Windows, does not set the SPI_SETSCREENREADER or SPI_GETSCREENREADER flags."
1702-
bool enabled = false;
1703-
if (SystemParametersInfo(SPI_GETSCREENREADER, 0, ref enabled, 0))
1704-
{
1705-
_screenReaderActive = enabled;
1706-
}
1707-
}
1708-
1709-
return _screenReaderActive.Value;
1710-
}
1711-
17121678
private static bool LoadPSReadline()
17131679
{
17141680
// Don't load PSReadline if:
@@ -1759,7 +1725,6 @@ private void DoCreateRunspace(RunspaceCreationEventArgs args)
17591725
bool psReadlineFailed = false;
17601726

17611727
// Load PSReadline by default unless there is no use:
1762-
// - screen reader is active, such as NVDA, indicating non-visual access
17631728
// - we're running a command/file and just exiting
17641729
// - stdin is redirected by a parent process
17651730
// - we're not interactive
@@ -1770,26 +1735,18 @@ private void DoCreateRunspace(RunspaceCreationEventArgs args)
17701735
ReadOnlyCollection<ModuleSpecification> defaultImportModulesList = null;
17711736
if (!customConfigurationProvided && LoadPSReadline())
17721737
{
1773-
if (IsScreenReaderActive())
1738+
// Create and open Runspace with PSReadline.
1739+
defaultImportModulesList = DefaultInitialSessionState.Modules;
1740+
DefaultInitialSessionState.ImportPSModule(new[] { "PSReadLine" });
1741+
consoleRunspace = RunspaceFactory.CreateRunspace(this, DefaultInitialSessionState);
1742+
try
17741743
{
1775-
s_theConsoleHost.UI.WriteLine(ManagedEntranceStrings.PSReadLineDisabledWhenScreenReaderIsActive);
1776-
s_theConsoleHost.UI.WriteLine();
1744+
OpenConsoleRunspace(consoleRunspace, args.StaMode);
17771745
}
1778-
else
1746+
catch (Exception)
17791747
{
1780-
// Create and open Runspace with PSReadline.
1781-
defaultImportModulesList = DefaultInitialSessionState.Modules;
1782-
DefaultInitialSessionState.ImportPSModule(new[] { "PSReadLine" });
1783-
consoleRunspace = RunspaceFactory.CreateRunspace(this, DefaultInitialSessionState);
1784-
try
1785-
{
1786-
OpenConsoleRunspace(consoleRunspace, args.StaMode);
1787-
}
1788-
catch (Exception)
1789-
{
1790-
consoleRunspace = null;
1791-
psReadlineFailed = true;
1792-
}
1748+
consoleRunspace = null;
1749+
psReadlineFailed = true;
17931750
}
17941751
}
17951752

@@ -3102,7 +3059,6 @@ private sealed class ConsoleHostStartupException : Exception
31023059
private bool _setShouldExitCalled;
31033060
private bool _isRunningPromptLoop;
31043061
private bool _wasInitialCommandEncoded;
3105-
private bool? _screenReaderActive;
31063062

31073063
// hostGlobalLock is used to sync public method calls (in case multiple threads call into the host) and access to
31083064
// state that persists across method calls, like progress data. It's internal because the ui object also

src/Microsoft.PowerShell.ConsoleHost/resources/ManagedEntranceStrings.resx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,6 @@
132132
<data name="ShellBannerRLMode" xml:space="preserve">
133133
<value>[Restricted Language Mode]</value>
134134
</data>
135-
<data name="PSReadLineDisabledWhenScreenReaderIsActive" xml:space="preserve">
136-
<value>Warning: PowerShell detected that you might be using a screen reader and has disabled PSReadLine for compatibility purposes. If you want to re-enable it, run 'Import-Module PSReadLine'.</value>
137-
</data>
138135
<data name="PreviewUpdateNotificationMessage" xml:space="preserve">
139136
<value> {1} A new PowerShell preview release is available: v{0} {2}
140137
{1} Upgrade now, or check out the release page at:{3}{2}
@@ -162,9 +159,9 @@
162159
[-CustomPipeName &lt;string&gt;] [-EncodedCommand &lt;Base64EncodedCommand&gt;]
163160
[-ExecutionPolicy &lt;ExecutionPolicy&gt;] [-InputFormat {Text | XML}]
164161
[-Interactive] [-MTA] [-NoExit] [-NoLogo] [-NonInteractive] [-NoProfile]
165-
[-NoProfileLoadTime] [-OutputFormat {Text | XML}]
166-
[-SettingsFile &lt;filePath&gt;] [-SSHServerMode] [-STA]
167-
[-Version] [-WindowStyle &lt;style&gt;]
162+
[-NoProfileLoadTime] [-OutputFormat {Text | XML}]
163+
[-SettingsFile &lt;filePath&gt;] [-SSHServerMode] [-STA]
164+
[-Version] [-WindowStyle &lt;style&gt;]
168165
[-WorkingDirectory &lt;directoryPath&gt;]
169166

170167
pwsh[.exe] -h | -Help | -? | /?

test/powershell/Host/ScreenReader.Tests.ps1

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)