1

The korean layout has alphabet mode and hangul mode.

enter image description here enter image description here

I only need hangul mode in the korean layout. To suppress alphabet mode I wrote this code but I could not find the way to get current IME mode. Which Windows API can I use to implement checkKoreanInputMode() function?

#include <Windows.h>
#include <iostream>
#include <thread>

bool checkKoreanInputMode() {
    // check current IME layout and mode
}

void setKoreanInputMode() {
    keybd_event(VK_HANGUL, 0, 0, 0);
    keybd_event(VK_HANGUL, 0, KEYEVENTF_KEYUP, 0);
}

int main() {
    while (true) {
        if (!checkKoreanInputMode()) {
            std::cout << "Switching to Korean input mode." << std::endl;
            setKoreanInputMode();
        }
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    return 0;
}

I tried to use ImmGetConversionStatus(), but ImmGetContext() returns always NULL even if I changed IME.

bool checkKoreanInputMode() {
    // Get the current IME context
    HIMC hIMC = ImmGetContext(GetForegroundWindow());
    if (hIMC == NULL){
        return false;
    }

    // Get the current IME mode
    DWORD dwConversion = 0, dwSentence = 0;
    bool isKoreanInputMode = false;

    if (ImmGetConversionStatus(hIMC, &dwConversion, &dwSentence)) {
        // Check if the current mode is Korean IME with English input mode
        isKoreanInputMode = (dwConversion & IME_CMODE_NATIVE) && !(dwConversion & IME_CMODE_ALPHANUMERIC);
    }

    // Release the IME context
    ImmReleaseContext(GetForegroundWindow(), hIMC);

    return isKoreanInputMode;
}

What am I doing wrong?

6
  • As @AndrásVarró indicated, I remember the imeapps sample which can get notified with WM_IME_NOTIFY and ImmGetConversionStatus. Commented May 9, 2024 at 3:20
  • Thanks for your comment and answer. But I could not use ImmGetConversionStatus() function and WM_IME_NOTIFY. Can you show me how to use it? Commented May 10, 2024 at 18:41
  • Try this answer stackoverflow.com/a/64285429/15511041, ImmGetDefaultIMEWnd and WM_IME_CONTROL. Commented May 11, 2024 at 0:29
  • It returns keyboard layout that is korean or english. I need to know mode of korean keyboard layout. The mode can be latin or hangul. Commented May 11, 2024 at 6:23
  • Although these values are not explained, you can test and utilize these values for your requirement. Commented May 13, 2024 at 2:16

2 Answers 2

1

I found a function called ImmGetConversionStatus(), which

Retrieves the current conversion status.

Unfortunately, I could not find any parameter named hangul, but there is one called hanja. I do not speak korean thus I do not know if it is the same or not. Here are the sources:

Link to ImmGetContext()

Link to ImmGetConversionStatus()

Update:

I think the problem is with GetForeGroundWindow() function.

HIMC hIMC = ImmGetContext(GetForegroundWindow());

I suppose that you should use the ImmGetDefaultIMEWnd instead:

HIMC hIMC = ImmGetContext(ImmGetDefaultIMEWnd(GetForegroundWindow()));

According to this question - where ImmGetDefaultIMEWnd was used - it could work.

Sign up to request clarification or add additional context in comments.

Comments

0

As @AndrásVarró indicated, I found the imeapps sample which can get notified with WM_IME_NOTIFY and ImmGetConversionStatus.

The following code is adapted from the imeapps sample.

   case WM_IME_NOTIFY:
    {
        switch (wParam)
        {
        case IMN_SETCONVERSIONMODE:
        {
            HIMC hIMC = ImmGetContext(hwnd);
            BOOL fOpen = ImmGetOpenStatus(hIMC);
            DWORD dwConvMode = 0, dwSentMode = 0;
            ImmGetConversionStatus(hIMC, &dwConvMode, &dwSentMode);
            if (fOpen)
            {
                switch (dwConvMode & IME_CMODE_LANGUAGE)
                {
                case IME_CMODE_ALPHANUMERIC:
                    //IME_CMODE_ALPHANUMERIC
                    break;   
                case IME_CMODE_NATIVE:
                    //IME_CMODE_NATIVE
                    break;  
                case (IME_CMODE_NATIVE | IME_CMODE_KATAKANA):
                    //IME_CMODE_KATAKANA
                    break;    
                default:
                    //NULL
                    break;
                }
            }
            break;
        }
        }
        break;
    }

Update @ironsand :

I tested these undocumented values with WM_IME_CONTROL. Maybe you can use 0x01.

#include <iostream>
#include <Windows.h>
#pragma comment (lib, "imm32.lib")
#define IMC_GETOPENSTATUS 0x0005
using namespace std;

int main() {
    while (true) {
        HWND hIME = ImmGetDefaultIMEWnd(GetForegroundWindow());
        LRESULT status = SendMessage(hIME, WM_IME_CONTROL, IMC_GETOPENSTATUS, 0);
        cout << (status ? "Korean" : "English") << endl;

        status = SendMessage(hIME, WM_IME_CONTROL, 0x01, 0);
        cout << status << "-0x01" << endl;
        /*status = SendMessage(hIME, WM_IME_CONTROL, 0x02, 0);
        cout << status << "-0x02" << endl;
        status = SendMessage(hIME, WM_IME_CONTROL, 0x03, 0);
        cout << status << "-0x03" << endl;
        status = SendMessage(hIME, WM_IME_CONTROL, 0x04, 0);
        cout << status << "-0x04" << endl;
        status = SendMessage(hIME, WM_IME_CONTROL, 0x06, 0);
        cout << status << "-0x06" << endl;
        status = SendMessage(hIME, WM_IME_CONTROL, 0x0D, 0);
        cout << status << "-0x0D" << endl;
        status = SendMessage(hIME, WM_IME_CONTROL, 0x0E, 0);
        cout << status << "-0x0E" << endl;
        status = SendMessage(hIME, WM_IME_CONTROL, 0x11, 0);
        cout << status << "-0x11" << endl;
        status = SendMessage(hIME, WM_IME_CONTROL, 0x12, 0);
        cout << status << "-0x12" << endl;
        status = SendMessage(hIME, WM_IME_CONTROL, 0x13, 0);
        cout << status << "-0x13" << endl;*/

        Sleep(10000);
    }
    return 0;
}

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.