OnScreenKeyboardClass.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Threading.Tasks;
  7. namespace CCDCount.DLL.Tools
  8. {
  9. public static class OnScreenKeyboard
  10. {
  11. [DllImport("kernel32.dll")]
  12. private static extern IntPtr GetModuleHandle(string lpModuleName);
  13. [DllImport("user32.dll")]
  14. private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  15. [DllImport("user32.dll")]
  16. private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  17. private const int SW_SHOW = 5;
  18. private const int SW_HIDE = 0;
  19. /// <summary>
  20. /// 启动软键盘(兼容Windows 10及以上版本)
  21. /// </summary>
  22. public static void KeyBoardShow()
  23. {
  24. //// 检查是否为Windows 10或更高版本
  25. //if (IsWindows10OrHigher())
  26. //{
  27. // // 尝试启动TabTip
  28. // if (TryStartTabTip())
  29. // return;
  30. //}
  31. // 回退到传统osk
  32. StartOnScreenKeyboard();
  33. }
  34. /// <summary>
  35. /// 隐藏软键盘
  36. /// </summary>
  37. public static void KeyBoardHide()
  38. {
  39. try
  40. {
  41. // 隐藏TabTip窗口
  42. IntPtr tabTipHwnd = FindWindow("IPTip_Main_Window", null);
  43. if (tabTipHwnd != IntPtr.Zero)
  44. {
  45. ShowWindow(tabTipHwnd, SW_HIDE);
  46. }
  47. // 隐藏OSK窗口
  48. IntPtr oskHwnd = FindWindow("OSKMainClass", "屏幕键盘");
  49. if (oskHwnd != IntPtr.Zero)
  50. {
  51. ShowWindow(oskHwnd, SW_HIDE);
  52. }
  53. }
  54. catch
  55. {
  56. // 忽略异常
  57. }
  58. }
  59. private static void StartOnScreenKeyboard()
  60. {
  61. try
  62. {
  63. // 检查是否已经运行
  64. Process[] oskProcesses = Process.GetProcessesByName("osk");
  65. if (oskProcesses.Length > 0)
  66. {
  67. Console.WriteLine("进入了分支");
  68. // 如果已经运行,尝试显示窗口
  69. IntPtr oskHwnd = FindWindow("OSKMainClass", "屏幕键盘");
  70. if (oskHwnd != IntPtr.Zero)
  71. {
  72. ShowWindow(oskHwnd, SW_SHOW);
  73. return;
  74. }
  75. else
  76. {
  77. // 如果找不到窗口,杀死进程重新启动
  78. foreach (var process in oskProcesses)
  79. {
  80. try { process.Kill(); } catch { }
  81. }
  82. }
  83. }
  84. Console.WriteLine("执行开启软键盘");
  85. Process.Start(Path.Combine(Environment.SystemDirectory, "osk.exe"));
  86. }
  87. catch (Exception ex)
  88. {
  89. throw new InvalidOperationException("无法启动屏幕键盘", ex);
  90. }
  91. }
  92. private static bool IsWindows10OrHigher()
  93. {
  94. var os = Environment.OSVersion;
  95. if (os.Version.Major >= 10)
  96. return true;
  97. try
  98. {
  99. using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"))
  100. {
  101. var currentBuild = key?.GetValue("CurrentBuild")?.ToString();
  102. if (int.TryParse(currentBuild, out int buildNumber))
  103. {
  104. // Windows 10 build 10240及以上
  105. return buildNumber >= 10240;
  106. }
  107. }
  108. }
  109. catch
  110. {
  111. // 忽略异常
  112. }
  113. // 兼容性检查:Windows 8.1及以后版本的Major是6,但Minor版本不同
  114. return os.Version.Major == 10 || (os.Version.Major == 6 && os.Version.Minor >= 2);
  115. }
  116. private static bool TryStartTabTip()
  117. {
  118. try
  119. {
  120. // 检查是否已经运行
  121. Process[] tabTipProcesses = Process.GetProcessesByName("TabTip");
  122. if (tabTipProcesses.Length > 0)
  123. {
  124. // 如果找不到窗口,杀死进程重新启动
  125. foreach (var process in tabTipProcesses)
  126. {
  127. try { process.Kill(); } catch { }
  128. }
  129. }
  130. // 方法1:通过注册表查找路径
  131. string tabTipPath = GetTabTipPathFromRegistry();
  132. if (!string.IsNullOrEmpty(tabTipPath) && File.Exists(tabTipPath))
  133. {
  134. Process.Start(tabTipPath);
  135. return true;
  136. }
  137. // 方法2:通过系统路径查找
  138. string systemPath = Path.Combine(Environment.SystemDirectory, "TabTip.exe");
  139. if (File.Exists(systemPath))
  140. {
  141. Process.Start(systemPath);
  142. return true;
  143. }
  144. // 方法3:使用modern Windows 10路径
  145. string modernPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
  146. @"TabletTip\1.7\TabTip.exe");
  147. if (File.Exists(modernPath))
  148. {
  149. Process.Start(modernPath);
  150. return true;
  151. }
  152. // 方法4:使用默认路径
  153. string defaultPath = @"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe";
  154. if (File.Exists(defaultPath))
  155. {
  156. Process.Start(defaultPath);
  157. return true;
  158. }
  159. }
  160. catch (Exception ex)
  161. {
  162. // 记录异常但不中断
  163. System.Diagnostics.Debug.WriteLine($"启动TabTip失败: {ex.Message}");
  164. }
  165. return false;
  166. }
  167. private static string GetTabTipPathFromRegistry()
  168. {
  169. try
  170. {
  171. using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore\InboxApplications"))
  172. {
  173. if (key != null)
  174. {
  175. foreach (var subKeyName in key.GetSubKeyNames())
  176. {
  177. if (subKeyName.Contains("Microsoft.Windows.Keyboard"))
  178. {
  179. using (var subKey = key.OpenSubKey(subKeyName))
  180. {
  181. var installPath = subKey?.GetValue("Path")?.ToString();
  182. if (!string.IsNullOrEmpty(installPath))
  183. {
  184. string tabTipPath = Path.Combine(installPath, "TabTip.exe");
  185. if (File.Exists(tabTipPath))
  186. return tabTipPath;
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. catch
  195. {
  196. // 忽略注册表访问异常
  197. }
  198. return null;
  199. }
  200. }
  201. }