Skip to content
This repository has been archived by the owner on May 21, 2021. It is now read-only.

Commit

Permalink
Updated Keyboard layout routine for win 7 - no more lagging
Browse files Browse the repository at this point in the history
  • Loading branch information
pawy committed Nov 22, 2015
1 parent eb8d04b commit e797211
Showing 1 changed file with 27 additions and 5 deletions.
Expand Up @@ -12,13 +12,21 @@ public class SEBInputLanguageToolStripButton : SEBToolStripButton
{
private Timer timer;
private int currentIndex;
private IntPtr[] languages;

[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] uint Msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
static extern IntPtr LoadKeyboardLayout(string pwszKLID, uint Flags);

[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr id);


[DllImport("user32.dll")]
static extern uint GetKeyboardLayout(uint idThread);

private const int WM_INPUTLANGCHANGEREQUEST = 0x0050;


Expand All @@ -39,6 +47,14 @@ public SEBInputLanguageToolStripButton()

SEBWindowHandler.ForegroundWatchDog.OnForegroundWindowChanged += handle => SetKeyboardLayoutAccordingToIndex();

languages = new IntPtr[InputLanguage.InstalledInputLanguages.Count];
for (int i = 0; i < InputLanguage.InstalledInputLanguages.Count; i++)
{
languages[i] =
LoadKeyboardLayout(
InputLanguage.InstalledInputLanguages[i].Culture.KeyboardLayoutId.ToString("X8"), 1);
}

//Start the update timer
timer = new Timer();
timer.Tick += (x, y) => UpdateDisplayText();
Expand All @@ -58,12 +74,18 @@ private void SetKeyboardLayoutAccordingToIndex()
{
InputLanguage.CurrentInputLanguage = InputLanguage.InstalledInputLanguages[currentIndex];

var threadIdOfCurrentForegroundWindow = GetWindowThreadProcessId(SEBWindowHandler.GetForegroundWindow(), IntPtr.Zero);
var currentKeyboardLayout = GetKeyboardLayout(threadIdOfCurrentForegroundWindow);

//This is for Windows 7
PostMessage(SEBWindowHandler.GetForegroundWindow(),
WM_INPUTLANGCHANGEREQUEST,
IntPtr.Zero,
LoadKeyboardLayout(InputLanguage.CurrentInputLanguage.Culture.KeyboardLayoutId.ToString("X8"), 1)
);
if (languages[currentIndex].ToInt32() != currentKeyboardLayout)
{
PostMessage(SEBWindowHandler.GetForegroundWindow(),
WM_INPUTLANGCHANGEREQUEST,
IntPtr.Zero,
languages[currentIndex]
);
}
}
catch (Exception ex)
{
Expand Down

0 comments on commit e797211

Please sign in to comment.