我们过去5年一直致力于应用程序,我们刚刚到达我们希望程序在表面3上运行的地方,这是我们要解决的主要问题遇到的是单击文本框时屏幕键盘无法打开。
我正试图找到一种方法,绑定一个应用程序范围的事件,可以在触摸文本框的任何时候打开键盘,这是可能的,还是我们需要通过整个应用程序并在我们想到的任何时候手动触发键盘我们需要它吗?
答案 0 :(得分:0)
我创建了一个键盘功能,并在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);
}
}
}
据我所知,没有办法告诉表面只用数字打开键盘。这只会打开默认键盘。我试图包含表面dll,但在我的c#桌面应用程序中没有做任何事情。希望这有帮助!
答案 1 :(得分:0)
以下是使用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>