繁体   English   中英

C#Windows 10虚拟键盘问题

[英]c# Windows 10 virtual keyboard issues

我已经开发了ac#代码段,以确定是否显示了虚拟(屏幕上)键盘。

下面的代码在Windows 7、8和8.1中工作正常,但是在Windows 10中, IsKeyboardVisible始终返回true ...

public static bool IsKeyboardVisible() {
    Process keyboardProc;
    UInt32 WS_DISABLED = 0x8000000;
    UInt32 WS_VISIBLE = 0X94000000;
    int GWL_STYLE = -16;
    IntPtr keyboardHandle = GetKeyboardWindowHandle();

    bool visible = false;

    if (keyboardHandle != IntPtr.Zero) {
        UInt32 style = GetWindowLong(keyboardHandle, GWL_STYLE);
        // in Win10, this always returns "true", i.e. WS_DISABLED is
        // 
        //visible = ((style & WS_DISABLED) != WS_DISABLED);
        // UPDATE: I found this code helping here
        visible = (style == WS_VISIBLE);
    }
    return visible;
}

我使用了关于SO的教程,但是前一阵子很抱歉,我不相信作者。

有谁知道所有最新Windows版本的工作代码段,因此我不必检查实际的操作系统即可打开该版本...?

更新

我在这里找到了原始帖子 ,这使我可以更正代码。 所以现在我的问题是相同的旧Win10问题-我无法使用

string progFiles = @"C:\Program Files\Common Files\Microsoft Shared\ink";
string keyboardPath = Path.Combine(progFiles, "TabTip.exe");
keyboardProc = Process.Start(keyboardPath);

...同样,我可以使用任何“全平台”代码,或者Win10的建议方法是什么?

更新2我发现有关在64位操作系统上运行32位应用程序的问题。 话虽如此,无论我尝试在System32还是sysWOW64`文件夹中运行osk.exe ,都会发生错误...除了制作64位版本之外,还有其他方法吗???

在深入研究TabTip.exeosk.exe以及x86和x64兼容性问题之后,我找到了一个解决方案,方法是在系统上搜索osk.exe并尝试运行它们。 我在以下文件夹中找到了4版本:

  • C:\\Windows\\System32
  • C:\\Windows\\SysWOW64
  • C:\\Windows\\WinSxS\\amd64_microsoft...
  • C:\\Windows\\WinSxS\\wow64_microsoft...

似乎在C:\\Windows\\WinSxS\\amd64_microsoft...工作正常(尽管不是其他三个)...

鉴于“ amd64 _....”文件夹在不同的计算机上可能不相同(我实际上检查了它们,但它们不匹配,因此我没有搜索这是否取决于计算机,Windows构建或其他任何内容... )。

因此,基本上,我做了一个小例程来查看WinSxS文件夹并返回osk.exe出现,效果很好。 我还使用简单的OS体系结构测试使代码在32位OS上工作:

 string OSKpath64 = getOskPath(@"C:\Windows\WinSxS");
 if (string.IsNullOrWhiteSpace(OSKpath64)) {
     OSKpath64 = "osk.exe";
  }
  string OSKpath32 = @"C:\Windows\System32\osk.exe";
  if (!File.Exists(OSKpath32)) {
      OSKpath32 = @"osk.exe";
  }
  System.Diagnostics.Process.Start((Environment.Is64BitOperatingSystem) ? OSKpath64 : OSKpath32);

更新: WinSxS文件夹中对一个可用版本和一个不可用版本的困惑使我感到紧张。 由于amd_..文件夹按字母顺序在wow64_...之前, wow64_...它可以正常工作。

因此,我建议在getOskPath方法中添加一个测试以返回第一个本机64位osk.exe (而不是模拟的)。

使用此处找到IsWin64Emulator 方法 ,该方法如下所示:

static string getOskPath(string dir) {
    string path = Path.Combine(dir, "osk.exe");
    if (File.Exists(path)) {
        Process p = System.Diagnostics.Process.Start(path);
        if (p.IsWin64Emulator()) {
            path = string.Empty;
        }
        p.Kill();
        return path;
    }
    DirectoryInfo di = new DirectoryInfo(dir);
    foreach (DirectoryInfo subDir in di.GetDirectories().Reverse()) {
        path = getOskPath(Path.Combine(dir, subDir.Name));
        if (!string.IsNullOrWhiteSpace(path)) {
            return path;
        }
    }
    return string.Empty;
}

我遇到了同样的问题,我在这里尝试所有答案,但是没有用。 用Google找到解决方案后,就可以了。

// Step 1: For Load On-Screen Keyboard
    const string Kernel32dll = "Kernel32.Dll";
    [DllImport(Kernel32dll, EntryPoint = "Wow64DisableWow64FsRedirection")]
    public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);

    [DllImport(Kernel32dll, EntryPoint = "Wow64EnableWow64FsRedirection")]
    public static extern bool Wow64EnableWow64FsRedirection(IntPtr ptr);

    IntPtr wow64Value;
    //---------------------------------------

   // Step 2: Function-----
   if (Environment.Is64BitOperatingSystem)
            {
                if (Wow64DisableWow64FsRedirection(ref wow64Value))
                {
                    System.Diagnostics.Process.Start("osk.exe");
                    Wow64EnableWow64FsRedirection(wow64Value);
                }
            }
            else
            {
                System.Diagnostics.Process.Start("osk.exe");
            }
   //----------------

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM