-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Vallat
committed
Dec 26, 2020
1 parent
fc7c32b
commit 14c8f3c
Showing
6 changed files
with
96 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace KeyLayoutAutoSwitch | ||
{ | ||
/// <summary> | ||
/// Handles accessibility events as if a screen reader were running. Chrome detects this to automatically enable accessibility features. | ||
/// </summary> | ||
internal class ChromeAccessibilityWinEventHook : IDisposable | ||
{ | ||
private delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime); | ||
|
||
[DllImport("user32.dll")] | ||
private static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags); | ||
|
||
[DllImport("user32.dll")] | ||
private static extern bool UnhookWinEvent(IntPtr hWinEventHook); | ||
|
||
[DllImport("User32.dll")] | ||
private static extern IntPtr SendMessage(IntPtr hWnd, uint nMsg, int wParam, int lParam); | ||
|
||
private const uint WINEVENT_OUTOFCONTEXT = 0; | ||
private const uint EVENT_SYSTEM_ALERT = 0x0002; | ||
|
||
private const uint WM_GETOBJECT = 0x3D; | ||
|
||
// ref: http://www.chromium.org/developers/design-documents/accessibility | ||
private const int GOOGLE_CHROME_ACCESSIBILITY_OBJECT_ID = 1; | ||
|
||
private readonly IntPtr mWinEventHook; | ||
private readonly WinEventDelegate mEventDelegate; | ||
|
||
public ChromeAccessibilityWinEventHook() | ||
{ | ||
mEventDelegate = OnEventReceived; | ||
mWinEventHook = SetWinEventHook(EVENT_SYSTEM_ALERT, EVENT_SYSTEM_ALERT, IntPtr.Zero, mEventDelegate, 0, 0, WINEVENT_OUTOFCONTEXT); | ||
} | ||
|
||
private void OnEventReceived(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) | ||
{ | ||
if (idObject == GOOGLE_CHROME_ACCESSIBILITY_OBJECT_ID) | ||
{ | ||
EventReceived = true; | ||
SendMessage(hwnd, WM_GETOBJECT, 0, idObject); | ||
|
||
// Chrome only enables accessibility if it gets a top-level IAccessible request, so let's make one first | ||
var _ = AccessibleObjectHelper.GetAccessibleObjectFromWindow(hwnd).accName; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
UnhookWinEvent(mWinEventHook); | ||
} | ||
|
||
/// <summary> | ||
/// This flag is set true whenever an event is received. | ||
/// Set it false before the period of interest. | ||
/// </summary> | ||
public bool EventReceived { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters