简体   繁体   中英

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. 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. 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. 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:

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(); , 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

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. 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; to work:

  1. Right click on project
  2. Highlight Add and click Reference
  3. Ensure you have Framework selected under Assemblies
  4. Scroll down and check in "WindowsBase" and hit ok
  5. Add using System.Windows.Interop; at the top of your code and your done

This worked for me (in 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());
            }
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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