Files
paoxiaoqi/Data/keyboard.cs
2026-01-16 20:55:07 +08:00

72 lines
1.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Diagnostics;
using System.IO;
using System.Security.Principal;
using System.Windows;
public class KeyboardHelper
{
public static void ShowSoftKeyboard()
{
if (!IsRunAsAdmin())
{
RestartAsAdmin();
return;
}
// 替换原有的 Process.Start 代码
string oskPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "osk.exe");
if (File.Exists(oskPath))
{
Process.Start(new ProcessStartInfo
{
FileName = oskPath,
UseShellExecute = true
});
}
else
{
//MessageBox.Show("未找到屏幕键盘程序osk.exe可能系统文件缺失或非Windows系统。", "错误");
}
}
public static void HideSoftKeyboard()
{
try
{
Process.Start("cmd.exe", "/C taskkill /IM osk.exe /F");
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
private static bool IsRunAsAdmin()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
private static void RestartAsAdmin()
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Process.GetCurrentProcess().MainModule.FileName;
startInfo.Verb = "runas";
try
{
Process.Start(startInfo);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
Environment.Exit(0);
}
}