Windows下C++呼叫系統軟鍵盤及其需要注意的點
Windows下系統軟鍵盤的程式名是osk.exe
,系統軟鍵盤在有鍵盤的時候一點用都沒有,但是沒有鍵盤的時候想要輸入點東西,系統軟鍵盤就至關重要了。
osk.exe
為微軟系統自帶的虛擬鍵盤程式,功能與真的鍵盤差不多.只需要在執行中輸入"osk"即可啟動虛擬鍵盤。
Window8開始,Windows開始採用扁平化設計,還有一個系統軟鍵盤叫TabTip.exe
,這個待會再說。先來看看這兩個軟鍵盤在Window7和Windows10下顯示的差異。上面是osk.exe
,下面是TabTip.exe
Windows7:
Windows10:
osk.exe
osk.exe
%SYSTEMROOT%\System32\
下,無論是32位或64位的系統都是位於這個路徑下。
64位的系統下的系統命令為什麼會在System32
中呢,這個請參考64位windows為什麼不把system32改成system64 ?。
那麼如何在你的C++程式中呼叫系統軟鍵盤osk.exe
呢?這裡就要提到兩個Windows系統API了,ShellExcute
和WinExec
,關於這兩個函式的用法可以參考MSDN。
這裡說一下使用方法。
WinExec
WinExec("osk.exe", SW_SHOWNORMAL);
就這麼簡單,這裡只給出了程式名,那麼WinExec
- The directory from which the application loaded.
- The current directory.
- The Windows system directory. The
GetSystemDirectory
function retrieves the path of this directory.- The Windows directory. The
GetWindowsDirectory
function retrieves the path of this directory.- The directories listed in the
PATH
environment variable.
ShellExcute
在VS中,ShellExcute
是ShellExcuteW
的巨集定義,因此,使用的時候,字串需要用寬字元。
ShellExecute(NULL, L"open", L"osk.exe", NULL, NULL, SW_SHOWNORMAL);
需要注意的點
從64位windows為什麼不把system32改成system64 ?這個連結中,你應該知道了有SysWOW64這個資料夾,這個資料夾的作用如下:
WoW64 (Windows On Windows64)是一個Windows作業系統的子系統,被設計用來處理許多在32-bit Windows和64-bit Windows之間的不同的問題,使得可以在64-bit Windows中執行32-bit程式。
也就是說32位程式在64位作業系統下執行System32
下的程式,會被自動重定向到%SYSTEMROOT%\SysWOW64
下,在這個目錄下搜尋程式,而SysWOW
下沒有osk.exe
,於是就會導致失敗。
因此,在執行ShellExcute
或WinExec
的時候需要取消這個重定向,在執行完後又恢復這個重定向。
這兩個操作,Windows也有對應的API可以使用,Wow64DisableWow64FsRedirection
和Wow64RevertWow64FsRedirection
。具體使用如下:
PVOID OldValue = NULL;
BOOL f = Wow64DisableWow64FsRedirection(&OldValue);
WinExec("osk.exe", SW_SHOWNORMAL);
if (f)
Wow64RevertWow64FsRedirection(OldValue);
TabTip.exe
剛沒說TabTip.exe
位於哪個路徑下,TabTip.exe
位於C:\Program Files\Common Files\microsoft shared\ink\
,因此可以呼叫ShellExcute
或WinExec
加上絕對路徑名就好了。