Kaynağa Gözat

调用系统的虚拟键盘

张洋 1 hafta önce
ebeveyn
işleme
71a582902d

BIN
bin/Win32/Debug/zhipuzi_pos_windows/DuiLib_d.dll


BIN
bin/Win32/Release/zhipuzi_pos_windows/DuiLib.dll


+ 47 - 0
zhipuzi_pos_windows/helper/CSystem.cpp

@@ -314,3 +314,50 @@ void CSystem::RunHotKey(int mod, int key)
 	keybd_event((BYTE)key, (BYTE)0, KEYEVENTF_KEYUP, 0);
 	keybd_event(mod_key, (BYTE)0, KEYEVENTF_KEYUP, 0);
 }
+
+DWORD CSystem::GetWindowsMajorVersion()
+{
+	using RtlGetVersionFn = LONG(WINAPI*)(OSVERSIONINFOW*);
+
+	const HMODULE ntdll = ::GetModuleHandleW(L"ntdll.dll");
+	const auto rtlGetVersion = reinterpret_cast<RtlGetVersionFn>(
+		::GetProcAddress(ntdll, "RtlGetVersion"));
+
+	if (rtlGetVersion == nullptr)
+	{
+		return 0;
+	}
+
+	OSVERSIONINFOW versionInfo = {};
+	versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
+
+	return rtlGetVersion(&versionInfo) == 0
+		? versionInfo.dwMajorVersion
+		: 0;
+}
+
+std::wstring CSystem::GetTouchKeyboardPath()
+{
+	wchar_t programFiles[MAX_PATH] = {};
+
+	// Win32 程序在 64 位系统中应使用 64 位 Program Files 目录。
+	if (::ExpandEnvironmentStringsW(
+		L"%ProgramW6432%",
+		programFiles,
+		_countof(programFiles)) > 1)
+	{
+		return std::wstring(programFiles) +
+			L"\\Common Files\\microsoft shared\\ink\\TabTip.exe";
+	}
+
+	if (::ExpandEnvironmentStringsW(
+		L"%CommonProgramFiles%",
+		programFiles,
+		_countof(programFiles)) > 1)
+	{
+		return std::wstring(programFiles) +
+			L"\\microsoft shared\\ink\\TabTip.exe";
+	}
+
+	return L"";
+}

+ 4 - 0
zhipuzi_pos_windows/helper/CSystem.h

@@ -47,4 +47,8 @@ public:
 	static BOOL IsAppRunning();
 
 	static void RunHotKey(int mod, int key);
+
+	static DWORD GetWindowsMajorVersion();
+
+	static std::wstring GetTouchKeyboardPath();
 };

+ 54 - 0
zhipuzi_pos_windows/wnd/CMainWnd.cpp

@@ -274,6 +274,16 @@ void CMainWnd::Notify(TNotifyUI& msg)
 			m_IsKuaijiejian = false;
 		}
 	}
+	else if (msg.sType == _T("setfocus"))
+	{
+		CDuiString name = msg.pSender->GetName();
+		std::wstring senderName = name;
+
+		if (senderName == L"diandan_food_search_edit")
+		{
+			this->ShowSystemKeyboard();
+		}
+	}
 }
 
 void CMainWnd::HandleClickMsg(TNotifyUI& msg)
@@ -2013,4 +2023,48 @@ void CMainWnd::UpdateRegisterHotKey()
 	UnregisterHotKey(m_hWnd, WM_MYHOTKEY_SHOUKUAN);
 
 	RegisterHotKey(m_hWnd, WM_MYHOTKEY_SHOUKUAN, shoukuan_mod, shoukuan_vk);
+}
+
+void CMainWnd::ShowSystemKeyboard()
+{
+	const DWORD windowsMajorVersion = CSystem::GetWindowsMajorVersion();
+
+	if (windowsMajorVersion >= 10)
+	{
+		const std::wstring touchKeyboardPath = CSystem::GetTouchKeyboardPath();
+
+		if (!touchKeyboardPath.empty() &&
+			CSystem::IsFileExist(touchKeyboardPath))
+		{
+			const HINSTANCE result = ::ShellExecuteW(
+				m_hWnd,
+				L"open",
+				touchKeyboardPath.c_str(),
+				nullptr,
+				nullptr,
+				SW_SHOWNORMAL);
+
+			if (reinterpret_cast<INT_PTR>(result) > 32)
+			{
+				return;
+			}
+
+			LOG_ERROR("TabTip.exe 启动失败,错误码: "
+				<< reinterpret_cast<INT_PTR>(result));
+		}
+	}
+
+	const HINSTANCE result = ::ShellExecuteW(
+		m_hWnd,
+		L"open",
+		L"C:\\Windows\\System32\\osk.exe",
+		nullptr,
+		nullptr,
+		SW_SHOWNORMAL);
+
+	if (reinterpret_cast<INT_PTR>(result) <= 32)
+	{
+		LOG_ERROR("osk.exe 启动失败,错误码: "
+			<< reinterpret_cast<INT_PTR>(result));
+	}
 }

+ 2 - 0
zhipuzi_pos_windows/wnd/CMainWnd.h

@@ -153,6 +153,8 @@ public:
 	//注册热键
 	void UpdateRegisterHotKey();
 
+	void ShowSystemKeyboard();
+
 private:
 	void UpdateFoodImage();