Windows下C++调用系统软键盘及其需要注意的点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/FlushHip/article/details/83008317

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了,ShellExcuteWinExec,关于这两个函数的用法可以参考MSDN。

这里说一下使用方法。

WinExec

WinExec("osk.exe", SW_SHOWNORMAL);

就这么简单,这里只给出了程序名,那么WinExec是如何找到这个程序的呢,下面是这个函数查找路径的顺序(摘自MSDN):

  1. The directory from which the application loaded.
  2. The current directory.
  3. The Windows system directory. The GetSystemDirectory function retrieves the path of this directory.
  4. The Windows directory. The GetWindowsDirectory function retrieves the path of this directory.
  5. The directories listed in the PATH environment variable.

ShellExcute

在VS中,ShellExcuteShellExcuteW的宏定义,因此,使用的时候,字符串需要用宽字符。

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,于是就会导致失败。

因此,在执行ShellExcuteWinExec的时候需要取消这个重定向,在运行完后又恢复这个重定向。

这两个操作,Windows也有对应的API可以使用,Wow64DisableWow64FsRedirectionWow64RevertWow64FsRedirection。具体使用如下:

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\,因此可以调用ShellExcuteWinExec加上绝对路径名就好了。

猜你喜欢

转载自blog.csdn.net/FlushHip/article/details/83008317