using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Management; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace CleanMachineHMI { public class KeyboardAutoPopUp { private const Int32 WM_SYSCOMMAND = 274; private const UInt32 SC_CLOSE = 61536; [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern bool PostMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern int RegisterWindowMessage(string lpString); private static bool hasKeyboard = false; private static readonly string USB = "USB"; static KeyboardAutoPopUp() { // hasKeyboard = GetKeyboardPresent(); } private static bool GetKeyboardPresent() { bool keyboardPresent = false; ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from Win32_Keyboard"); foreach (ManagementObject keyboard in searcher.Get()) { foreach (PropertyData prop in keyboard.Properties) { if (Convert.ToString(prop.Value).Contains(USB)) { keyboardPresent = true; break; } } } return keyboardPresent; } //显示屏幕键盘 public static int ShowInputPanel() { if (hasKeyboard) return 0; try { dynamic file = "C:\\Program Files\\Common Files\\microsoft shared\\ink\\TabTip.exe"; if (!System.IO.File.Exists(file)) return -1; Process.Start(file); return 1; } catch (Exception) { return 255; } } //隐藏屏幕键盘 public static void HideInputPanel() { try { IntPtr TouchhWnd = new IntPtr(0); TouchhWnd = FindWindow("IPTip_Main_Window", null); if (TouchhWnd == IntPtr.Zero) return; PostMessage(TouchhWnd, WM_SYSCOMMAND, SC_CLOSE, 0); } catch { } } } }