Files
CleanMachine/CleanMachineHMI/Utils/KeyboardAutoPopUp.cs

99 lines
3.0 KiB
C#
Raw Normal View History

2021-06-05 14:17:30 +08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
2022-01-16 15:35:09 +08:00
using System.Management;
2021-06-05 14:17:30 +08:00
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);
2022-05-05 20:42:02 +08:00
private static bool hasKeyboard = false;
2022-01-16 15:35:09 +08:00
private static readonly string USB = "USB";
2022-05-05 20:42:02 +08:00
static KeyboardAutoPopUp()
2022-01-16 15:35:09 +08:00
{
2022-05-05 20:42:02 +08:00
hasKeyboard = GetKeyboardPresent();
2022-01-16 15:35:09 +08:00
}
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;
}
2021-06-05 14:17:30 +08:00
//显示屏幕键盘
public static int ShowInputPanel()
{
2022-01-16 15:35:09 +08:00
if (hasKeyboard) return 0;
2021-06-05 14:17:30 +08:00
try
{
2022-05-05 20:42:02 +08:00
2021-06-05 14:17:30 +08:00
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
{
}
}
}
}