10

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?

EDIT: Solution

[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));
}
Tzah Mama
  • 1,529
  • 1
  • 13
  • 25

3 Answers3

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
  • 3,586
  • 11
  • 45
  • 85
  • 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

You could try this answer, which uses windows API calls.

Community
  • 1
  • 1
John Koerner
  • 35,668
  • 7
  • 74
  • 128
  • Thanks this led me to the answer and I'll be editing my question with the answer if anyone else stumble upon this problem :D – Tzah Mama Jun 15 '12 at 21:24