Show & hiding the Windows 8 on screen keyboard from WPF

后端 未结 8 1737
我寻月下人不归
我寻月下人不归 2020-12-03 08:17

I\'m writing a WPF application for a Windows 8 tablet. It\'s full windows 8 and not ARM/RT.

When the user enters a textbox I show the on screen keyboard using the fo

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 08:53

    A little bit late, I'll just improve tasaki example for a complete one of what I did to enable show/hide on gotFocus/LostFocus event when user click on a textBox in my WPF application for windows 8 tablet.I hope this help people with similar headache, because disabling InkHelper, doesn't really work well if u want to scroll with touch event...

    First of all u must add these reference to your App.Xaml.cs File.

    using System.Management;
    using System.Runtime.InteropServices;
    

    The code:

        protected override void OnStartup(StartupEventArgs eventArgs)
        {
            EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotFocusEvent,
                                    new RoutedEventHandler(GotFocus_Event), true);
            EventManager.RegisterClassHandler(typeof(TextBox), UIElement.LostFocusEvent,
                                    new RoutedEventHandler(LostFocus_Event), true);
    
           MainApplication.Show();
        }
    
        private static void GotFocus_Event(object sender, RoutedEventArgs e)
        {
           // TestKeyboard();
            OpenWindows8TouchKeyboard(sender, e);
        }
        //http://www.c-sharpcorner.com/UploadFile/29d7e0/get-the-key-board-details-of-your-system-in-windows-form/
        private static bool IsSurfaceKeyboardAttached()
        {
            SelectQuery Sq = new SelectQuery("Win32_Keyboard");
            ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher(Sq);
            ManagementObjectCollection osDetailsCollection = objOSDetails.Get();
            //Windows 8 tablet are returnign 2 device when keyboard is connecto
            //My application won't be used for Desktop so this condition is fine
            //But u might want to see if keyboard is usb and == 1 so you are 
            //returning true or not on tablet.  
            return osDetailsCollection.Count <= 1 ? true : false;
        }
    
        private static void OpenWindows8TouchKeyboard(object sender, RoutedEventArgs e)
        {
            var textBox = e.OriginalSource as TextBox;        
            if (textBox != null && IsSurfaceKeyboardAttached())
            {
                var path = @"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe";
                if (!File.Exists(path))
                {
                    // older windows versions
                    path = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\osk.exe";
                }
                Process.Start(path);
                textBox.BringIntoView();//SetFocus so u dont lose focused area
            }
        }
        [DllImport("user32.dll")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);
    
        [DllImport("user32.dll")]
        public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
    
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_CLOSE = 0xF060;
        public const int SC_MINIMIZE = 0xF020;
    
        private void CloseOnscreenKeyboard()
        {
            // retrieve the handler of the window  
            int iHandle = FindWindow("IPTIP_Main_Window", "");
            if (iHandle > 0)
            {
                // close the window using API        
                SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
            }
        }
    
        private void LostFocus_Event(object sender, EventArgs e)
        {
            // It's time to close the onscreen keyboard.
            CloseOnscreenKeyboard();
        }
    

提交回复
热议问题