블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2737)
Unity3D (817)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (58)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (53)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
04-19 00:04

KeyDown이벤트시에 한글 입력은 무조건 229(?)만 나오기 때문에 어떤 글자를 쳤는지는 알 수가 없지요.

 

그래서 한글 입력은 윈도우 메시지를 가로채서 알아냅니다.

 

먼저 윈도우 메시지를 가로채는 방법은 크게 두가지정도를 생각해 볼 수 있겠는데요,

WndProc 메서드를 오버라이드 하는 방법과 IMessageFilter를 구현해서 가로채는 방법이 있겠습니다.

 

인터넷에 찾아보니 WndProc메서드 오버라이드 하는 방법이 나와있는데 전 이 방법으로는 메시지를 가로챌 수 없더군요..

(왜 안되는지 아시는 분은 답글 남겨 주시기 바랍니다..)

음.. 코딩 환경따라 다른지 모르겠습니다.

참고로 제 환경은 Windows XP Pro 이고 .NET FramWork 1.1 Ver 1.1.4322 환경입니다..

 

 

아무튼.. 메시지 필터를 이용해서 메시지를 가로채기 위해서는

윈폼에서 Application.AddMessageFilter(구현한 필터 클래스) 호출하여 가로채는데

다음과 같은 순서로 하면 되겠습니다.

 

1. 메시지를 가로채서 어떻게 처리할 것인지를 구현한다.

즉 IMessageFilter 클래스를 구현하는것을 의미합니다.

 

구체적으로 이런식으로 구현하면 되겠습니다.

 

    public class MyMessageFilter: IMessageFilter

        {

            #region IMessageFilter 멤버

     

            public const int WM_KEYFIRST = 0x100;

            public const int WM_KEYDOWN = 0x100;

            public const int WM_KEYUP = 0x101;

            public const int WM_CHAR = 0x102;

            public const int WM_IME_STARTCOMPOSITION = 0x10D;

            public const int WM_IME_COMPOSITION = 0x10F;

            public const int WM_IME_ENDCOMPOSITION = 0x10E;

            public const int WM_IME_SETCONTEXT = 0x281;

            public const int WM_IME_NOTIFY = 0x282;

            public const int WM_IME_CONTROL = 0x283;

            public const int WM_IME_COMPOSITIONFULL = 0x284;

            public const int WM_IME_SELECT = 0x285;

            public const int WM_IME_CHAR = 0x286;

            public const int WM_IME_KEYDOWN = 0x290;

            public const int WM_IME_KEYUP = 0x291;

            public const int WM_IME_REPORT = 0x0280;

            public const int WM_IME_REQUEST = 0x0288;

            

            public bool PreFilterMessage(ref Message m)

            {

                // TODO:  MyMessageFilter.PreFilterMessage 구현을 추가합니다.

                Form1 fm = (Form1)Form.ActiveForm;

                

                switch(m.Msg)

                {

                    case WM_IME_STARTCOMPOSITION:

                        fm.WriteLine("한글 조합 시작.." + "\t" + m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

     

                    case WM_IME_COMPOSITION:

                        fm.WriteLine("한글 입력 중.." + "\t"+ m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

     

                    case WM_IME_ENDCOMPOSITION:

                        fm.WriteLine("한글 조합 완료.." + "\t"+ m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

     

                    case WM_IME_SETCONTEXT:

                        fm.WriteLine("WM_IME_SETCONTEXT" + "\t"+ m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

                    case WM_IME_NOTIFY:

                        fm.WriteLine("WM_IME_NOTIFY" + "\t"+ m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

                    case WM_IME_CONTROL:

                        fm.WriteLine("WM_IME_CONTROL" + "\t"+ m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

                    case WM_IME_COMPOSITIONFULL:

                        fm.WriteLine("WM_IME_COMPOSITIONFULL" + "\t"+ m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

                    case WM_IME_SELECT:

                        fm.WriteLine("WM_IME_SELECT" + "\t"+ m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

                    case WM_IME_CHAR:

                        fm.WriteLine("WM_IME_CHAR" + "\t"+ m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

                    case WM_IME_KEYDOWN:

                        fm.WriteLine("WM_IME_KEYDOWN" + "\t"+ m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

                    case WM_IME_KEYUP:

                        fm.WriteLine("WM_IME_KEYUP" + "\t"+ m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

                    case WM_IME_REPORT:

                        fm.WriteLine("WM_IME_REPORT" + "\t"+ m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

                    case WM_IME_REQUEST:

                        fm.WriteLine("WM_IME_REQUEST" + "\t"+ m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

     

                    case WM_KEYDOWN:

                        fm.WriteLine("WM_KEYDOWN" + "\t"+ m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

                    /*

                    case WM_KEYUP:

                        fm.WriteLine("WM_KEYUP" + "\t"+ m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

                    */

                    case WM_CHAR:

                        fm.WriteLine("WM_CHAR" + "\t"+ m.WParam.ToString() + "\t" + m.LParam.ToString());

                        break;

                }

                

                return false;

            }

     

            #endregion

        }//end of class

 

 

2. 윈폼 클래스에서 구현한 필터 클래스를 선언합니다.

 

private MyMessageFilter mmf = new MyMessageFilter();

 

 

 

3. AddMessageFilter()로 메시지 필터를 윈폼클래스에 등록합니다.

 

Application.AddMessageFilter(mmf);

 

폼 생성자 메서드에서 등록하면 되겠네요..

 

 

참.. 제 소스에서는

윈폼 클래스에 다음과 같은 메서드를 추가했었죠..

 

        public void WriteLine(string str)

        {

            this.textBox1.Text += str + "\r\n";

        }

 

이로서 한글을 입력할때 어떤 메시지가 어떻게 발생하는 지 대충 알 수 있으리라고 생각됩니다.

출처 : Devpia


출처 : http://www.xdotnet.com/xdotnet/board/BoardRead.aspx?F_Code=14&B_Code=1052&B_Index=1294&B_Page=0

반응형
Posted by blueasa
, |