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);
    }
}
