简体   繁体   English

在C#中为Windows 8平板电脑启动键盘

[英]Bring up keyboard in C# for Windows 8 tablet

I am working on a UI for a Windows 8.1 tablet, which has a full version of Windows on it. 我正在使用Windows 8.1平板电脑的UI,它上面有完整版本的Windows。 There is a keyboard icon at the bottom of windows 8.1, which brings up a keyboard, and I want that to automatically trigger after clicking a numericUpDown box. Windows 8.1底部有一个键盘图标,它会弹出一个键盘,我想在单击一个numericUpDown框后自动触发。 I then would also like it to close after leaving or clicking off of the box. 然后我还希望它在离开或点击框后关闭。

I am basically just trying to focus it when it is clicked, but this does not seem to bring up the keyboard. 我基本上只是在点击它时试图集中它,但这似乎没有调出键盘。 Also, note, I am setting some other numericUpDown box to the one in the function so I can call it outside, so I hope that doesn't make it difficult to see what's going on, let me know if you need any clarifications and thank you for the help. 另外,请注意,我正在为函数中的那个设置一些其他numericUpDown框,所以我可以在外面调用它,所以我希望这不会让你很难看到发生了什么,让我知道你是否需要任何澄清并感谢你的帮助。 Here is what I have so far: 这是我到目前为止:

copiedNUD.Click += CopiedNudPass_Focus;

//copy copied nud
CopiedNudPass = copiedNUD;

...

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

I tried looking around a bit, but some of the solutions weren't too clear to me. 我试着四处寻找,但有些解决方案对我来说并不太清楚。 I really appreciate the help. 我非常感谢你的帮助。 Thank you. 谢谢。

I figured it out. 我想到了。 Here is my code specifically for a tablet with window 8 or higher: 这是我的代码专门针对具有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();
    }
}

My only problem right now is that when the keyboard closes my main form in the background gets cut off and I tried to refresh it using Refresh(); 我现在唯一的问题是,当键盘关闭后,我的主窗体在后台被切断,我尝试使用Refresh()刷新它; , but that did not seem to work :(. ,但这似乎不起作用:(。

Here is a better closing function: 这是一个更好的关闭功能:

After killing the process for TabletKeyboard(TabTip.exe) application doesn't bring back to its original size in wpf 在杀死TabletKeyboard(TabTip.exe)应用程序的过程后,在wpf中没有恢复到原来的大小

Here is my new close code: 这是我的新密码:

//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);
        }
}

I also had to add a reference to WindowsBase and add external functions to the project. 我还必须添加对WindowsBase的引用并向项目添加外部函数。 The steps and additional code are in the url I linked to in this post. 这些步骤和其他代码都在我在这篇文章中链接的网址中。 Here's how you add a reference for WindowsBase to get using System.Windows.Interop; 以下是为WindowsBase添加引用以使用System.Windows.Interop的方法; to work: 上班:

  1. Right click on project 右键单击项目
  2. Highlight Add and click Reference 突出显示添加,然后单击参考
  3. Ensure you have Framework selected under Assemblies 确保在Assemblies下选择了Framework
  4. Scroll down and check in "WindowsBase" and hit ok 向下滚动并检入“WindowsBase”并点击“确定”
  5. Add using System.Windows.Interop; 使用System.Windows.Interop添加; at the top of your code and your done 在代码的顶部和完成

This worked for me (in Java & eclipse RCP) 这对我有用(在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());
            }
        }
    });

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

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