I'm writing an application which I require to change system's language when the application itself is NOT FOCUSED. I'm afraid that the only way to do it is by using windows hook which I can't fully understand at the moment. any thoughts?
Asked
Active
Viewed 7,044 times
10
-
Do you mean that a particular application requires the system language to be changed from the one that the user doesn't generally use? – Eric J. Jun 15 '12 at 18:36
-
Is the problem knowing when you're not focused, or changing the system language? – Jon B Jun 15 '12 at 18:36
-
Changing the language requires a login / logout... do you mean the language input? – vcsjones Jun 15 '12 at 18:40
-
the problem is changing the current system language to a different one – Tzah Mama Jun 15 '12 at 18:40
3 Answers
2
I think that it would be much easier to map another keyboard layout and "translate" input instead of changing windows settings, especially without the contest of the user.
Before I finish the answer ahmadali shafiee posted the code I had in mind. Be elegant, go with it.
1
To change input language you can use this code:
private void ChangeKeboardLayout(System.Globalization.CultureInfo CultureInfo)
{
InputLanguage c = InputLanguage.FromCulture(CultureInfo);
InputLanguage.CurrentInputLanguage = c;
}
and you can do it whenever you application(or your thread) is open.

ahmadali shafiee
- 4,350
- 12
- 56
- 91
-
This won't help since this code changes the input language in the current thread. I'm asking for something that can change the system's input language without the application in focus (just as if the user pressed alt+shift) – Tzah Mama Jun 15 '12 at 18:59
-
@user1459484 if your thread is open you can do it. the form focus state doesn't change anything. – ahmadali shafiee Jun 15 '12 at 19:00
-
Well this might be a problem of my part where I didn't explained myself clear enough, I need the language input to change not in my application but in the current open window. let's say I'm now typing in notepad in russian then I want to application to change it to english – Tzah Mama Jun 15 '12 at 19:11
-
@user1459484 I didn't understand. I think you mean that you want to change the input language when your form `unfocused`. Do you mean that? – ahmadali shafiee Jun 16 '12 at 18:12
1
The OP's own solution, originally part of the question, but edited out:
[DllImport("user32.dll")]
private static extern bool PostMessage(int hhwnd, uint msg, IntPtr wparam, IntPtr lparam);
[DllImport("user32.dll")]
private static extern IntPtr LoadKeyboardLayout(string pwszKLID, uint Flags);
private static uint WM_INPUTLANGCHANGEREQUEST = 0x0050;
private static int HWND_BROADCAST = 0xffff;
private static string en_US = "00000409";
private static uint KLF_ACTIVATE = 1;
private static void ChangeLanguage()
{
PostMessage(HWND_BROADCAST,WM_INPUTLANGCHANGEREQUEST, IntPtr.Zero ,LoadKeyboardLayout(en_US,KLF_ACTIVATE));
}

General Grievance
- 4,555
- 31
- 31
- 45