Search code examples
c++cwinapikeyboard

How can change language keyboard layout in Windows (c++)


I need change keyboard language. with this code i can change the language if in focus app:

#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <string>

using namespace std;

#pragma comment(lib, "User32.lib")


int main(int argc, char *argv[])
{
   //0409 - Ingles, 0c0a - Espanhol, 0416 - Portugues
   char *str = "0c0a";//argv[1];
   int i;

    if(EOF != sscanf(str, "%x", &i))
   {
      SendMessage(GetActiveWindow(), WM_INPUTLANGCHANGEREQUEST, 0, i);
   }
   system("PAUSE");
   return EXIT_SUCCESS;
}

but i need change the system keyboard language definitely, after app is close or if focus not set in app.

any can help me?


Solution

  • If you really need to change user's keyboard layout you can use following functions: GetKeyboardLayoutName() and LoadKeyboardLayout().