繁体   English   中英

屏幕键盘-Windows 8.1平板电脑

[英]On Screen keyboard - Windows 8.1 Tablet

在过去的5年中,我们一直在致力于应用程序的开发,而现在我们要让程序在Surface Pro 3上运行,我们遇到的主要问题是屏幕键盘单击文本框时无法打开。

我正在尝试找到一种方法来绑定一个应用程序范围的事件,该事件将在每次触摸文本框时打开键盘,这是否可能,还是我们需要遍历整个应用程序并在我们认为我们会在任何时候手动触发键盘需要它?

除非在WindowsStoreApplication中,否则单击或触摸文本框时似乎无法打开键盘。

我创建了一个键盘功能,大约2个月前在这里的其他地方找到了这个答案。 如果我可以再次找到它,我将使用链接对其进行更新。 我正在使用Surface 2 pro平板电脑,代码为:

 class KeyboardFunction
{
    //Used for close keyboard method
    [DllImport("user32.dll")]
    public static extern int FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll")]
    public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

    public const int WM_SYSCOMMAND = 0x0112;
    public const int SC_CLOSE = 0xF060;
    //End close keyboard inputs


    /*
   *Opens the tablet keyboard on device
   *
   */
    public void openKeyboardOnTablet()
    {
        System.Diagnostics.Process.Start(@"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe");

    }

    /*
  *Close on screen keyboard
  *
  */
    public void closeOnscreenKeyboard()
    {
        // retrieve the handler of the window  
        int iHandle = FindWindow("IPTIP_Main_Window", "");
        if (iHandle > 0)
        {
            // close the window using API        
            SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
        }
    }

}

据我所知,没有办法告诉表面只用数字打开键盘。 这只是打开默认键盘。 我试图包括Surface dll,但是在我的C#桌面应用程序中什么也没做。 希望这可以帮助!

这是在带有WPF的Windows 8.1中执行此操作的一种方法,该代码也可以在Windows 10中使用,除了您可能需要进行修改以更好地与新的Touch Panel and Handwriting Service

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Threading;

    public class KeyboardFocusBehavior : Behavior<Control>
    {
        public bool Focused
        {
            get { return (bool)GetValue(FocusedProperty); }
            set { SetValue(FocusedProperty, value); }
        }
        public static readonly DependencyProperty FocusedProperty = DependencyProperty.Register("Focused", 
            typeof(bool), 
            typeof(ControlKeyboardFocusBehavior), 
            new PropertyMetadata(false, OnFocusedPropertyChanged));

        private static void OnFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ControlKeyboardFocusBehavior b = d as ControlKeyboardFocusBehavior;

            if(b != null && b.AssociatedObject != null)
            {
                Control c = b.AssociatedObject;

                if (!c.IsFocused && (bool)e.NewValue)
                {
                    Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(delegate()
                    {
                        c.Focus();         
                        Keyboard.Focus(c);
                        TouchKeyboard.Show();
                    }));
                }
            }
        }

    public class ShowTouchKeyboardOnFocusBehavior : Behavior<Control>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            WeakEventManager<Control, KeyboardFocusChangedEventArgs>.AddHandler(AssociatedObject, "GotKeyboardFocus", OnGotKeyboardFocus);
            WeakEventManager<Control, KeyboardFocusChangedEventArgs>.AddHandler(AssociatedObject, "LostKeyboardFocus", OnLostKeyboardFocus);
        }

        private void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            // show the touch keyboard
            TouchKeyboard.Show();
            var textBox = sender as TextBox;
            if (textBox != null)
                textBox.SelectionStart = Math.Max(0, textBox.Text.Length);
        }

        private void OnLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            // hide the touch keyboard
            TouchKeyboard.Hide();
        }
    }

然后在WPF XAML文件中,对于给定的文本框:

                        <Border  CornerRadius="0,5,5,0" Style="{StaticResource TextBoxBorder}" >
                            <TextBox Style="{StaticResource TextBoxInput}" Text="{Binding MyTextProperty}" >
                                <i:Interaction.Behaviors>
                                    <behaviors:ShowTouchKeyboardOnFocusBehavior />
                                </i:Interaction.Behaviors>
                            </TextBox>
                        </Border>   

暂无
暂无

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

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