WPF 打开虚拟键盘

        public static void Open()
        {
            var tabTipFile = @"C:\Program Files\Common Files\microsoft shared\ink\TabTip.exe";
            if (System.IO.File.Exists(tabTipFile))
            {
                foreach (var process in Process.GetProcessesByName("TabTip"))
                {
                    process.Kill();
                }

                Process.Start(tabTipFile);
            }
        }
        public static void OpenOsk()
        {
            var oskFilePath = Environment.CurrentDirectory + @"\osk.exe";

            if (System.IO.File.Exists(oskFilePath))
            {
                var proc = new Process();
                proc.StartInfo.FileName = oskFilePath;
                proc.StartInfo.UseShellExecute = true;
                proc.StartInfo.Verb = "runas";
                proc.Start();
            }
        }
        /// <summary>
        /// 使用命令方式打开虚拟键盘
        /// </summary>
        public static void OpenOskUseCMD()
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            //是否使用操作系统shell启动
            p.StartInfo.UseShellExecute = false;
            // 接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardInput = true;
            //输出信息
            p.StartInfo.RedirectStandardOutput = true;
            // 输出错误
            p.StartInfo.RedirectStandardError = true;
            //不显示程序窗口
            p.StartInfo.CreateNoWindow = true;
            //启动程序
            p.Start();
            //向cmd窗口发送输入信息
            p.StandardInput.WriteLine("osk&exit");
            p.StandardInput.AutoFlush = true;
            //等待程序执行完退出进程
            p.WaitForExit();
            p.Close();
        }

我在采用第二种、第三种方式时,在我本机上可以,在别人电脑上报了如下错误:

但是第一种就正常,所以最后采用的是第一种方式,为了防止目标机上没有这个exe文件,还特意拷贝了一份到程序安装根目录下,最终代码如下:

        public static void Open()
        {
            var tabTipFile = @"C:\Program Files\Common Files\microsoft shared\ink\TabTip.exe";
            if (!System.IO.File.Exists(tabTipFile))
            {
                tabTipFile = Environment.CurrentDirectory + @"\TabTip.exe";
            }

            if (System.IO.File.Exists(tabTipFile))
            {
                Process.Start(tabTipFile);
            }
        }

参考文章链接如下,感谢两位大佬

https://my.oschina.net/u/4398116/blog/3225317
https://www.cnblogs.com/dotnet261010/p/7087290.html

posted @ 2020-09-07 15:26  雨也绵绵  阅读(741)  评论(0编辑  收藏  举报