10

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

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • show the switch statement too. – Mukul Varshney Jan 30 '17 at 08:26
  • 2
    Probably this link will help you. http://stackoverflow.com/questions/3287180/putting-ime-in-a-custom-text-box-derived-from-control – Mukul Varshney Jan 30 '17 at 08:56
  • what kind of control is it? a standard `TextBox`? is it subclassed somehow? because otherwise it is supposed to work just fine. the text area in notepad is also based on a plain EDIT control, which has wired up all the require IME stuff by default. WPF would be a different story, but not WinForms. – Cee McSharpface Feb 06 '17 at 14:01
  • 1
    Control is basically a third party control, they have not handled it, I have to handle this from windows side code. I'm passing input characters from c# code to the third party dll – Vivek Nuna Feb 06 '17 at 14:45
  • @viveknuna: this may help: http://stackoverflow.com/questions/2392606/c-sharp-and-ime-getting-the-current-input-text – deblocker Feb 08 '17 at 07:46
  • @deblocker checked it, not working for me :( – Vivek Nuna Feb 08 '17 at 08:00
  • 1
    you posted below Vincent McNabb's answer (link in 3rd comment) that it helped, does it mean this question could be closed? – Cee McSharpface Feb 08 '17 at 09:43
  • @dlatikay, It a good reference, but didn't solve my problem. – Vivek Nuna Feb 08 '17 at 10:37

1 Answers1

0

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.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197