I am making a software to switch the input language according to the active window.
I am able to get the active window title and then check it to see if i want to switch the language to English or to Arabic.
I made it that when the text of a textbox change i check the textbox text and if i get a match i set SetKeyboardLayout(GetInputLanguageByName("ar"));
public static InputLanguage GetInputLanguageByName(string inputName)
{
foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages)
{
if (lang.Culture.TwoLetterISOLanguageName == inputName)
{
return lang;
}
}
return null;
}
private void SetKeyboardLayout(InputLanguage layout)
{
InputLanguage.CurrentInputLanguage = layout;
MessageBox.Show(InputLanguage.CurrentInputLanguage.Culture.EnglishName);
}
If i check what is the current input language after it change the language i get for example Arabic but Windows keeps English as input language.
But if at IF statement i set it to this
if(true)
{
SetKeyboardLayout(GetInputLanguageByName("ar"));
}
Windows change input language to Arabic.
I can see that if the software execute SetkeyboardLayout immediately after it opened Windows change the language but if the software execute it after a while Windows doesn't change it.
Anybody knows why?
What I have tried:
Here is my class code
<pre lang="C#">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace JustTyping
{
public partial class Form1 : Form
{
WinEventDelegate dele = null;
IntPtr m_hhook = IntPtr.Zero;
delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
[DllImport("user32.dll")]
static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
[DllImport("user32.dll")]
public static extern bool UnhookWinEvent(IntPtr hWinEventHook);
private const uint WINEVENT_OUTOFCONTEXT = 0;
private const uint EVENT_SYSTEM_FOREGROUND = 3;
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
public Form1()
{
InitializeComponent();
dele = new WinEventDelegate(WinEventProc);
m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT);
}
#region methods
private string GetActiveWindowTitle()
{
const int nChars = 256;
IntPtr handle = IntPtr.Zero;
StringBuilder Buff = new StringBuilder(nChars);
handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString().Trim();
}
return null;
}
public void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
textBox2.Text = GetActiveWindowTitle();
}
public static InputLanguage GetInputLanguageByName(string inputName)
{
foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages)
{
if (lang.Culture.TwoLetterISOLanguageName == inputName)
{
return lang;
}
}
return null;
}
private void SetKeyboardLayout(InputLanguage layout)
{
InputLanguage.CurrentInputLanguage = layout;
MessageBox.Show(InputLanguage.CurrentInputLanguage.Culture.EnglishName);
}
#endregion
private void titleName_txt_TextChanged(object sender, EventArgs e)
{
bool match = Regex.IsMatch(textBox2.Text, @"Telegram");
if (match)
{
SetKeyboardLayout(GetInputLanguageByName("ar"));
}
}
bool stop = false;
private void stopB_Click(object sender, EventArgs e)
{
if (stopB.Text == "Stop")
stop = UnhookWinEvent(m_hhook);
else if (stopB.Text == "Start")
{
dele = new WinEventDelegate(WinEventProc);
m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT);
stop = false;
}
if(stop)
{
stopB.Text = "Start";
}
else
{
stopB.Text = "Stop";
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
bool match = Regex.IsMatch(textBox2.Text.ToLower(), "Telegra".ToLower());
if (textBox2.Text.ToLower().StartsWith("Viber".ToLower()))
{
SetKeyboardLayout(GetInputLanguageByName("ar"));
}
}
}
}