可以通过以下代码打开Windows虚拟键盘
void UMouseSimulatorBPLibrary::ShowVirtualKeyboard()
{TCHAR* OskPath = L"C:\\Program Files\\Common Files\\microsoft shared\\ink\\TabTip.exe";if (!FPaths::FileExists(OskPath)){OskPath = L"C:\\windows\\system32\\osk.exe";}GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, OskPath);if (!FPaths::FileExists(OskPath)) {GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("没有找到虚拟键盘路径"));}else {FProcHandle ProcHandle = FPlatformProcess::CreateProc(OskPath, L"", true, false, false, nullptr, -1, nullptr, nullptr);if (!ProcHandle.IsValid()){// 处理错误DWORD err = GetLastError();FString errMsg = FString::Printf(TEXT("FPlatformProcess::CreateProc Fail, Error code: %d"), err);GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, errMsg);// 尝试用ShellExecuteEx替身权限打开SHELLEXECUTEINFO ShExecInfo = { 0 };ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;ShExecInfo.hwnd = NULL;ShExecInfo.lpVerb = TEXT("runas"); // 请求提升权限ShExecInfo.lpFile = OskPath; // 可执行文件路径ShExecInfo.lpParameters = TEXT(""); // 参数ShExecInfo.lpDirectory = NULL;ShExecInfo.nShow = SW_NORMAL;ShExecInfo.hInstApp = NULL;bool bSuccess = ShellExecuteEx(&ShExecInfo);if (!bSuccess){// 处理错误err = GetLastError();errMsg = FString::Printf(TEXT("ShellExecuteEx Fail, Error code: %d"), err);GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, errMsg);}}}
}
其中:TabTip.exe是带中文输入的,osk.exe只能输入英文。
通常,FPlatformProcess::CreateProc在Window 10以上系统是无法执行外部exe的。调试信息返回GetLastError() = 740。
这是一个权限不足的问题。chatGPT建议用ShellExecuteEx来提升权限运行。
用runas提升权限提示后,系统弹出以下安全确认对话框
可以在控制面板中修改通知权限,避免每次都要权限确认。