【问题标题】:Bring up keyboard in C# for Windows 8 tablet在 C# 中为 Windows 8 平板电脑调出键盘
【发布时间】:2013-12-23 16:03:28
【问题描述】:

我正在为 Windows 8.1 平板电脑开发 UI,该平板电脑上有完整版的 Windows。 Windows 8.1 底部有一个键盘图标,它会弹出一个键盘,我希望它在单击 numericUpDown 框后自动触发。然后,我还希望它在离开或单击框后关闭。

我基本上只是在单击它时尝试聚焦它,但这似乎并没有调出键盘。另外,请注意,我正在将其他一些 numericUpDown 框设置为函数中的那个,以便我可以在外面调用它,所以我希望这不会让您很难看到发生了什么,如果您需要任何澄清,请告诉我并感谢您的帮助。这是我目前所拥有的:

copiedNUD.Click += CopiedNudPass_Focus;

//copy copied nud
CopiedNudPass = copiedNUD;

...

void CopiedNudPass_Focus(object sender, EventArgs e)
{
    CopiedNudPass.Focus();
}

我试着环顾了一下,但有些解决方案对我来说不是很清楚。我真的很感激帮助。谢谢。

【问题讨论】:

    标签: c# keyboard tablet windows-8.1


    【解决方案1】:

    我想通了。这是我专门针对具有 8 或更高窗口的平板电脑的代码:

    copiedNUD.Click += CopiedNudPass_Focus;
    
    //copy copied nud
    CopiedNudPass = copiedNUD;
    
    ...
    
    
    //Launch keyboard
    void CopiedNudPass_Focus(object sender, EventArgs e)
    {
    
        Version win8version = new Version(6, 2, 9200, 0);
    
        if (Environment.OSVersion.Version >= win8version)
        {
            string progFiles = @"C:\Program Files\Common Files\Microsoft Shared\ink";
            string keyboardPath = Path.Combine(progFiles, "TabTip.exe");
    
        Process.Start(keyboardPath);
        }
    }
    
    //Close keyboard
    void CopiedNudPass_LostFocus(object sender, EventArgs e)
    {
    
        Version win8version = new Version(6, 2, 9200, 0);
    
        if (Environment.OSVersion.Version >= win8version)
        {
            Process[] oskProcessArray = Process.GetProcessesByName("TabTip");
            foreach (Process onscreenProcess in oskProcessArray)
            {
                onscreenProcess.Kill();
            }
        Refresh();
        }
    }
    

    我现在唯一的问题是当键盘关闭时,我在后台的主窗体被切断,我尝试使用 Refresh(); 刷新它。 ,但这似乎不起作用:(。

    【讨论】:

      【解决方案2】:

      这里有一个更好的关闭函数:

      After killing the process for TabletKeyboard(TabTip.exe) application doesn't bring back to its original size in wpf

      这是我的新关闭代码:

      //Close keyboard
      void CopiedNudPass_LostFocus(object sender, EventArgs e)
      {
          Version win8version = new Version(6, 2, 9200, 0);
      
              if (Environment.OSVersion.Version >= win8version)
              {
                  uint WM_SYSCOMMAND = 274;
                  uint SC_CLOSE = 61536;
                  IntPtr KeyboardWnd = FindWindow("IPTip_Main_Window", null);
                  PostMessage(KeyboardWnd.ToInt32(), WM_SYSCOMMAND, (int)SC_CLOSE, 0);
              }
      }
      

      我还必须添加对 WindowsBase 的引用并向项目添加外部函数。步骤和附加代码在我在这篇文章中链接到的 url 中。以下是添加 WindowsBase 引用以使用 System.Windows.Interop 的方法;上班:

      1. 右键单击项目
      2. 突出显示添加并单击参考
      3. 确保您在“程序集”下选择了框架
      4. 向下滚动并检查“WindowsBase”并点击确定
      5. 使用 System.Windows.Interop 添加;在你的代码和你完成的顶部

      【讨论】:

        【解决方案3】:

        这对我有用(在 Java 和 Eclipse RCP 中)

            text.addFocusListener(new FocusListener()
            {
                @Override
                public void focusLost(FocusEvent arg0)
                {
                        LogUtil.logInfo("Closing OSK");
        
                        try
                        {
                            if(Settings.getBoolean(Settings.OSK_USETABTIP)) {
                                Runtime.getRuntime().exec("cmd /c taskkill /IM tabtip.exe");
                            } else {
                                Runtime.getRuntime().exec("cmd /c taskkill /IM osk.exe");
                            }
                        }
                        catch (IOException e)
                        {
                            LogUtil.logError(e.toString());
                        }
                }
        
                @Override
                public void focusGained(FocusEvent arg0)
                {
                    try
                    {
                        String sysroot = System.getenv("SystemRoot");
        
                        if(Settings.getBoolean(Settings.OSK_USETABTIP)) {
                            LogUtil.logInfo("Opening TabTip");
                            ProcessBuilder pb = new ProcessBuilder("C:/pathtotabtip/tabtip.exe");
                            pb.start();
                        } else {
                            LogUtil.logInfo("Opening OSK");
                            ProcessBuilder pb = new ProcessBuilder(sysroot + "/system32/osk.exe");
                            pb.start();
                        }
                    }
                    catch (Exception e)
                    {
                        LogUtil.logError(e.toString());
                    }
                }
            });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-06-24
          • 1970-01-01
          • 2014-05-21
          • 1970-01-01
          • 2016-05-10
          • 1970-01-01
          • 1970-01-01
          • 2023-03-22
          相关资源
          最近更新 更多