can't get current keyboard layout

前端 未结 2 807
走了就别回头了
走了就别回头了 2021-01-18 07:40

I have tried GetKeyboardLayoutName() and GetKeyboardLayout() for getting the current keyboard layout, but they both give me the default lay

2条回答
  •  温柔的废话
    2021-01-18 08:25

    The following code is simple and works fine. If you write a command line program, the GetKeyboardLayout API does't work in windows cmd or powershell, you can test it in babun(an open source windows shell).

    #include 
    int getInputMethod() {
      HWND hwnd = GetForegroundWindow();
      if (hwnd) {
        DWORD threadID = GetWindowThreadProcessId(hwnd, NULL);
        HKL currentLayout = GetKeyboardLayout(threadID);
        unsigned int x = (unsigned int)currentLayout & 0x0000FFFF;
        return ((int)x);
      }
      return 0;
    }
    

提交回复
热议问题