OnScreenKeyboardClass.cs 7.5 KB

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