C# 调用系统软键盘

2 篇文章 0 订阅
using RetailApp.Core.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace RetailApp.Core.SystemUtils
{
    /// <summary>
    /// 触摸键盘工具类
    /// </summary>
    class TouchKeyBoard
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
        private const UInt32 WM_SYSCOMMAND = 0x112;
        private const UInt32 SC_RESTORE = 0xf120;
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
        private const string OnScreenKeyboadApplication = "osk.exe";


        /// <summary>
        /// 启用系统软键盘
        /// </summary>
        public static void OpenKeyBoardFun()
        {
            try
            {
                //判断软键盘是否进程是否已经存在,如果不存在进行调用
                Process[] pro = Process.GetProcessesByName("osk");
                //如果键盘已打开,则进行关闭操作
                if (pro != null && pro.Length > 0)
                {
                    CloseKeyBoardFun();
                    return;
                }

                // Get the name of the On screen keyboard
                string processName = System.IO.Path.GetFileNameWithoutExtension(OnScreenKeyboadApplication);

                // Check whether the application is not running 
                var query = from process in Process.GetProcesses()
                            where process.ProcessName == processName
                            select process;

                var keyboardProcess = query.FirstOrDefault();

                // launch it if it doesn't exist
                if (keyboardProcess == null)
                {
                    IntPtr ptr = new IntPtr(); ;
                    bool sucessfullyDisabledWow64Redirect = false;

                    // Disable x64 directory virtualization if we're on x64,
                    // otherwise keyboard launch will fail.
                    if (System.Environment.Is64BitOperatingSystem)
                    {
                        sucessfullyDisabledWow64Redirect = Wow64DisableWow64FsRedirection(ref ptr);
                    }

                    // osk.exe is in windows/system folder. So we can directky call it without path
                    using (Process osk = new Process())
                    {
                        osk.StartInfo.FileName = OnScreenKeyboadApplication;
                        osk.Start();
                        //osk.WaitForInputIdle(2000);
                    }

                    // Re-enable directory virtualisation if it was disabled.
                    if (System.Environment.Is64BitOperatingSystem)
                        if (sucessfullyDisabledWow64Redirect)
                            Wow64RevertWow64FsRedirection(ptr);
                }
                else
                {
                    // Bring keyboard to the front if it's already running
                    var windowHandle = keyboardProcess.MainWindowHandle;
                    SendMessage(windowHandle, WM_SYSCOMMAND, new IntPtr(SC_RESTORE), new IntPtr(0));
                }

            }
            catch (Exception ex)
            {
                LogUtil.WriteLog(MethodBase.GetCurrentMethod().Name, LogUtil.ERROE, ex.Message);
                LogUtil.WriteLog(MethodBase.GetCurrentMethod().Name, LogUtil.ERROE, ex.StackTrace);
            }
            
        }


        /// <summary>
        /// 关闭系统软键盘
        /// </summary>
        public static void CloseKeyBoardFun()
        {
            try
            {
                Process[] pros = Process.GetProcessesByName("osk");
                foreach (Process p in pros)
                {
                    p.Kill();
                }
            }
            catch (Exception ex)
            {
                LogUtil.WriteLog(MethodBase.GetCurrentMethod().Name, LogUtil.ERROE, ex.Message);
                LogUtil.WriteLog(MethodBase.GetCurrentMethod().Name, LogUtil.ERROE, ex.StackTrace);
            }
            
        }


    }



}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值