首页 > 编程语言 >C# 调用系统软键盘帮助类(兼容.netframework2.0)

C# 调用系统软键盘帮助类(兼容.netframework2.0)

时间:2022-12-02 10:00:34浏览次数:44  
标签:IntPtr netframework2.0 C# osk Process 软键盘 ex new LogUtil


前言

最近再做触屏系统的时候需要手动调用打开系统软键盘的需求,网上查找到的资料很多,在高版本的fx上也能用,但是刚好我这个现场程序是基于fx2.0开发的,只能在之前的基础上改造支持。目前已经调整到可以支持使用,供大家享用。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Management;
using System.Runtime.InteropServices;

namespace Common
{
/// <summary>
/// 触摸键盘工具类
/// </summary>
public class TouchKeyBoardHelper
{
[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);

var processList = new List<Process>(Process.GetProcesses());
// Check whether the application is not running
var query = processList.FindAll((u) => u.ProcessName== processName);

Process keyboardProcess=null;

if (query!=null&&query.Count>0)
{
keyboardProcess = query[0];
}

// 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 (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 (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)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
//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)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
//LogUtil.WriteLog(MethodBase.GetCurrentMethod().Name, LogUtil.ERROE, ex.Message);
//LogUtil.WriteLog(MethodBase.GetCurrentMethod().Name, LogUtil.ERROE, ex.StackTrace);
}

}


private static bool Is64BitOperatingSystem
{
get
{
try
{
string addressWidth = String.Empty;
ConnectionOptions mConnOption = new ConnectionOptions();
ManagementScope mMs = new ManagementScope("//localhost", mConnOption);
ObjectQuery mQuery = new ObjectQuery("select AddressWidth from Win32_Processor");
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(mMs, mQuery);
ManagementObjectCollection mObjectCollection = mSearcher.Get();
foreach (ManagementObject mObject in mObjectCollection)
{
addressWidth = mObject["AddressWidth"].ToString();
}
return addressWidth == "64";
}
catch (Exception ex)
{

return false;
}
}
}
}
}


标签:IntPtr,netframework2.0,C#,osk,Process,软键盘,ex,new,LogUtil
From: https://blog.51cto.com/u_11295556/5905025

相关文章