2018년 9월 26일 수요일

C# WPF Keyboard Input Language

C# 가상 키보드 개발하면서 피드백 받은 부분을 개선하기로 했다.
구현할 부분은 현재 입력값이 한글인지 영어인지 확인하여 키 캡션을 변경하는 것이다.
키 캡션을 변경하는 것은 쉽지만 현재 언어 상태를 알기가 쉽지 않았다.

먼저 Focused된 Process가 필요하다.
Focused . Not Current.
Current Process Name을 써서 2일을 날려먹었다.

포커싱된 프로세스의 현재 상태값.

 IntPtr status = SendMessage(hime, WM_IME_CONTROL, new IntPtr(0x5), new IntPtr(0));

1이면 한글이고 0이면 영어를 나타낸다.
나는 처음에 Current Process 를 써서 0고정이길래 작동이 안되는 줄 알았다.

#Currently, ime-mode does not work in Chrome. Let's give up.(18.09)

아래는 코드

        [DllImport("imm32.dll")]
        private static extern IntPtr ImmGetDefaultIMEWnd(IntPtr hWnd);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr IParam);
        private const int WM_IME_CONTROL = 643;
        public bool isEngMode,isCapsLock;
        // The GetForegroundWindow function returns a handle to the foreground window
        // (the window  with which the user is currently working).
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
   
        // The GetWindowThreadProcessId function retrieves the identifier of the thread
        // that created the specified window and, optionally, the identifier of the
        // process that created the window.
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern Int32 GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
        
        private string GetForegroundProcessName()
        {
            IntPtr hwnd = GetForegroundWindow();
            // The foreground window can be NULL in certain circumstances, 
            // such as when a window is losing activation.
            if (hwnd == null)
                return "Unknown";
            uint pid;
            GetWindowThreadProcessId(hwnd, out pid);
            foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
            {
                if (p.Id == pid)
                    return p.ProcessName;
            }
            return "Unknown";
        }
   
        //check english/korean mode
       
        public bool GetIME()
        {
            string FocusedProcess = GetForegroundProcessName();
            Process p = Process.GetProcessesByName(FocusedProcess).FirstOrDefault();
            if (p == null)
                return false;
   
            IntPtr hwnd = p.MainWindowHandle;
            IntPtr hime = ImmGetDefaultIMEWnd(hwnd);
            IntPtr status = SendMessage(hime, WM_IME_CONTROL, new IntPtr(0x5), new IntPtr(0));
            if (status.ToInt32() != 0)
                return true;  //korean
            else
                return false;
        }
cs

댓글 없음:

댓글 쓰기