Skip to content
Browse files

Add note about Custom Role Preventer addon. Ignore url query and anchor

  • Loading branch information...
AlexVallat committed Mar 21, 2019
1 parent dfcc396 commit 11bfaa2a324ef40ee049cae973c298bfd9605987
Showing with 25 additions and 6 deletions.
  1. +23 −6 KeyLayoutAutoSwitch/Program.cs
  2. +2 −0
@@ -169,7 +169,24 @@ private void OnConfigure(object sender, EventArgs e)

private string mLastFocusedUrl;
private readonly Dictionary<int, IntPtr> mPreviousUrlLayouts = new Dictionary<int, IntPtr>();

private bool TryGetPreviousUrlLayout(string url, out IntPtr layout)
var result = mPreviousUrlLayouts.TryGetValue(url.GetHashCode(), out layout);
if (result)
Debug.WriteLine($"Restoring previous layout for {url}: {layout}");

return result;

private void SetPreviousUrlLayout(string url, IntPtr layout)
mPreviousUrlLayouts[url.GetHashCode()] = layout;
Debug.WriteLine($"Stored previous layout for {url}: {layout}");

private void OnFocusChanged(IntPtr hwnd, uint idObject, uint idChild)
var className = NativeMethods.GetWindowClassName(hwnd);
@@ -188,7 +205,9 @@ private void OnFocusChanged(IntPtr hwnd, uint idObject, uint idChild)
var accessibleObject = NativeMethods.GetAccessibleFromEvent(hwnd, idObject, idChild);
if (accessibleObject != null)
var focusType = browser.GetFocusType(accessibleObject, out var url);
var focusType = browser.GetFocusType(accessibleObject, out var fullUrl);
var url = new Uri(fullUrl).GetLeftPart(UriPartial.Path); // Ignore query and anchor parts of the URL

Debug.WriteLine($"Focus on {focusType} with url {url}");
//Debug.WriteLine($"Focus on accessible object: {accessibleObject.accName[0]} ({AccessibleObjectHelper.GetRole(accessibleObject)})");

@@ -206,17 +225,15 @@ private void OnFocusChanged(IntPtr hwnd, uint idObject, uint idChild)
Debug.Assert(currentLayout != IntPtr.Zero, "Unable to get current keyboard layout");
if (currentLayout != IntPtr.Zero && currentLayout != ruleLayoutForLastUrl)
Debug.WriteLine($"Stored previous layout for {mLastFocusedUrl}: {currentLayout}");
mPreviousUrlLayouts[mLastFocusedUrl.GetHashCode()] = currentLayout;
SetPreviousUrlLayout(mLastFocusedUrl, currentLayout);

// Attempt to look up the previous layout for this url
if (url != null && Rules.Instance.RestorePreviouslyVisitedPageLayouts &&
mPreviousUrlLayouts.TryGetValue(url.GetHashCode(), out var previousLayout))
TryGetPreviousUrlLayout(url, out var previousLayout))
Debug.WriteLine($"Restoring previous layout for {url}: {previousLayout}");
NativeMethods.SwitchKeyboardLayout(hwnd, previousLayout);
@@ -16,6 +16,8 @@ KeyLayoutAutoSwitch is not an Add-on. Firefox no longer supports the technical r

[Download Installer](

Some web sites, notably Google Docs, set a custom Aria document role on their pages. This prevents Firefox from reporting the URL for them through accessibility services. Most sites do not exhibit this problem, but if the ones you use do, then you can install the [Prevent Custom Document Role]( addon to work around the issue.

## Usage
Running KeyLayoutAutoSwitch will show the main configuration window. This can also be shown by double clicking on the icon in the notification area of the taskbar. In this window, the keyboard layout to use for each browser element can be set by selecting the relevant entry in the list and clicking the Edit button.

0 comments on commit 11bfaa2

Please sign in to comment.
You can’t perform that action at this time.