C#弹出和影藏WIN10、WIN8平板模式的触摸键盘
在WIN10和WIN8的平板系统下我们的应用程序可能需要用户输入字符,这种需求有两个解决方案。
1、应用程序创建一个模拟键盘的页面,但是有一定局限性。比如你应用程序调用第三方的DLL或者exe弹出来的输入框你是没有办法跨进程录入数据的(如果有的话实现起来也绝壁吐血....);
2、使用操作系统的模拟键盘,简单高效。哪怕是你主程序在调用第三方DLL或者exe都可以,流程就是先异步开启模拟键盘,再调用第三方的弹框方法,第三方弹框方法结束后关闭模拟键盘;
使用第二种方法简直完美解决这个需求好么。好啦,需求就说到这,码上再说~~~
1 /// <summary> 2 /// 模拟键盘 3 /// 对应WIN10、WIN8下的平板系统对应的系统模拟键盘 4 /// </summary> 5 public class AnalogKeyBox 6 { 7 private const Int32 WM_SYSCOMMAND = 274; 8 private const UInt32 SC_CLOSE = 61536; 9 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 10 private static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam); 11 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 12 private static extern bool PostMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam); 13 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 14 private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); 15 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 16 private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 17 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 18 private static extern int RegisterWindowMessage(string lpString); 19 20 /// <summary> 21 /// 显示屏幕键盘 22 /// </summary> 23 /// <returns></returns> 24 public static int ShowInputPanel() 25 { 26 try 27 { 28 dynamic file = "C:\\Program Files\\Common Files\\microsoft shared\\ink\\TabTip.exe"; 29 if (!System.IO.File.Exists(file)) 30 return -1; 31 Process.Start(file); 32 //return SetUnDock(); //不知SetUnDock()是什么,所以直接注释返回1 33 return 1; 34 } 35 catch (Exception) 36 { 37 return 255; 38 } 39 } 40 41 42 /// <summary> 43 /// 异步开启模拟键盘 44 /// </summary> 45 /// <returns></returns> 46 public static async Task ShowInputPanelAsync() 47 { 48 await Task.Run(() => ShowInputPanel()); 49 } 50 /// <summary> 51 /// 隐藏屏幕键盘 52 /// </summary> 53 public static void HideInputPanel() 54 { 55 IntPtr TouchhWnd = new IntPtr(0); 56 TouchhWnd = FindWindow("IPTip_Main_Window", null); 57 if (TouchhWnd == IntPtr.Zero) 58 return; 59 PostMessage(TouchhWnd, WM_SYSCOMMAND, SC_CLOSE, 0); 60 } 61 }