Search code examples
c#.netwinformsime

How to take Korean input in Winform?


I want to type Korean text in my editable area inside a WinForms application.

But Characters are repeating, I have tried to override the default WndProc, but nothing working.

switch (m.WParam.ToInt32())
                {
case Common.Interop.Window.WM_IME_CHAR:
                break;

            case Common.Interop.Window.WM_IME_ENDCOMPOSITION:
                    PassCharToScreen(m);
                break;

            case Common.Interop.Window.WM_CHAR:
                    PassCharToScreen(m);
                break;

            case Common.Interop.Window.WM_IME_NOTIFY:
                break;
            case Common.Interop.Window.WM_IME_COMPOSITION:
                PassCharToScreen(m);
                break;
            case Common.Interop.Window.WM_IME_COMPOSITIONFULL:
                break;

When I type in English, breakpoint hits WM_CHAR, But When I type in Korean it hits WM_IME_COMPOSITION on first character, and then after first character it hits WM_IME_COMPOSITION first and then hit WM_CHAR.

I have observed that it types the first character correctly. e.g. ㅁ (Korean Character) On typing second character. ㅁㅂㅁ (First char, second char, first char). I want the behaviour as it is in notepad


Solution

  • I have somehow solved the issue, I'm writing here to help others. Please let me know if there is any bug in the code.

    private bool mIsImeProcessed = true;
    private bool mIsImeContinue = false;
    
    case WM_IME_COMPOSITION:
                    {
                        if (mKoreanInput == true)
                        {
                            long lParam = m.LParam.ToInt64();
                            long wParam = m.WParam.ToInt64();
                            char c = (char)m.WParam;
                            if (lParam == 24600)
                            {
                                if (mIsImeProcessed)
                                {
                                    mIsImeProcessed = false;
                                    mIsImeContinue = false;
                                    PassCharToThirdParty(m);
                                }
                                else
                                {
                                    PassCharToThirdPartyWithBackSpace(((char)m.WParam).ToString());
                                }
                                mIsImeContinue = true;
                            }
                        }
                        else if (lParam == 2048)
                        {
                            if (mIsImeProcessed)
                            {
                            }
                            else
                            {
                                if (mIsImeContinue == true)
                                {
    
                                    PassCharToThirdPartyWithBackSpace(((char)m.WParam).ToString());
                                }
                            }
    
                            mIsImeProcessed = true;
    
                        }
                        else
                        {
                                PassBackSpaceToThirdParty();
                        }
                    }
                    break;
    case Common.Interop.Window.WM_IME_ENDCOMPOSITION:
                    if (mKoreanInput == true)
                    {
                        mIsImeProcessed = true;
                        mIsImeContinue = false;
                    }
                    break; 
    

    First, check whether the language is Korean or another language, So if it's Korean you have to handle it differently.

    You have to get the info on Start and End composition and you have to always check whether it's the continuation of character or composition. Set mIsImeProcessed to true and mIsImeContinue to false once you get End of Composition in WndProc.

    We need to handle cases for backspace too.