簡體   English   中英

在Windows中使用GetKeyName()函數在C ++中即時切換鍵盤布局

[英]Keyboard layout switch on the fly in C++ using GetKeyName() function in windows

我正在嘗試通過以下方式以多種語言獲取特定掃描代碼的鍵值int
布局:英語(美國鍵盤)16-Q 17-W 18-E 19-R 20-T 21-Y布局:法語(法國鍵盤)16-A 17-Z 18-E 19-R 20-T 21 -是的,我使用以下代碼:

#include "pch.h"
#include "iostream"
#include <windows.h>

using namespace std;
int main()
{
    int scancode[6] = { 16,17,18,19,20,21};
    int bufferLength = 10;
    char buffer[10] ;
    while (1)
    {
        int i = 0;
        for (i = 0; i < 6 ; i++)
        {
            unsigned int extended = scancode[i] & 0xffff00;
            unsigned int lParam = 0;

            if (extended) 
            {

                if (extended == 0xE11D00)
                {
                    lParam = 0x45 << 16;
                }
                else
                {
                    lParam = (0x100 | (scancode[i] & 0xff)) << 16;
                }

            }
            else {

                lParam = scancode[i] << 16;

                if (scancode[i] == 0x45) 
                {
                    lParam |= (0x1 << 24);
                }
            }
            GetKeyNameTextA(lParam, buffer, bufferLength);
            printf("%s \n", buffer);

        }

    }
    return 0;
}

該代碼為我提供了本地化的鍵值,但是如果我在運行時更改布局,則鍵值不會更改。 它們保持與以前相同,要獲取更改的值,我必須再次運行它。 誰能建議我解決這個問題? 還建議是否有其他方法可以實現這一目標。

使用LoadKeyboardLayout並發送WM_INPUTLANGCHANGEREQUEST來更改鍵盤布局,如下所示:

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

int main()
{
    HKL hkl = LoadKeyboardLayout(L"0000080c", KLF_ACTIVATE);
    PostMessage(GetConsoleWindow(), WM_INPUTLANGCHANGEREQUEST, 0, (LPARAM)hkl);
    std::string str;
    while(std::cin >> str)
        if(str == "0")
            break;
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM