Wpf 虚拟键盘对话框窃取文本框键盘焦点
Wpf Virtual Keyboard dialog steals textbox keyboard focus
我们在项目中使用 Mindfusion wpf 键盘。
每次文本框获得焦点时,我们都会为用户打开键盘对话框。
如果它是一个没有任何行为的常规文本框,一切正常,但是当我们将此键盘用于具有 "select all" 行为的文本框时,我们只能输入一个字符,因为它会在键盘上按键后选择文本。
我们检查过,这不是 mindfusion 键盘的问题,因为当我们将它用作用户控件时,它可以正常工作。当我们从他们的应用程序中打开此键盘时,它可以正常工作。当我们打开 windows 键盘时,它可以工作。
我认为最主要的是对话框 window。
我们尝试将其设置为 focusable=false
和 showactivated=false
但它不起作用。
我们还尝试使用 focusmanger 和 win32.showunactivated
这里是行为代码:
public class SelectAllTextOnFocusBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.GotKeyboardFocus += AssociatedObjectGotKeyboardFocus;
AssociatedObject.GotMouseCapture += AssociatedObjectGotMouseCapture;
AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObjectPreviewMouseLeftButtonDown;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.GotKeyboardFocus -= AssociatedObjectGotKeyboardFocus;
AssociatedObject.GotMouseCapture -= AssociatedObjectGotMouseCapture;
AssociatedObject.PreviewMouseLeftButtonDown -= AssociatedObjectPreviewMouseLeftButtonDown;
}
private void AssociatedObjectGotKeyboardFocus(object sender,
System.Windows.Input.KeyboardFocusChangedEventArgs e)
{
AssociatedObject.SelectAll();
Console.WriteLine("AssociatedObjectGotKeyboardFocus");
}
private void AssociatedObjectGotMouseCapture(object sender,
System.Windows.Input.MouseEventArgs e)
{
AssociatedObject.SelectAll();
Console.WriteLine("AssociatedObjectGotMouseCapture");
}
private void AssociatedObjectPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!AssociatedObject.IsKeyboardFocusWithin)
{
AssociatedObject.Focus();
Console.WriteLine("AssociatedObjectPreviewMouseLeftButtonDown");
e.Handled = true;
}
}
}
你知道如何防止它失去键盘焦点吗?
解决方法是在另一个线程中打开window...
我们浪费了12个小时来解决它..
var thread = new Thread(() =>
{
fk = new FullKeyboard();
fk.Show();
App.Current.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
我们在项目中使用 Mindfusion wpf 键盘。
每次文本框获得焦点时,我们都会为用户打开键盘对话框。
如果它是一个没有任何行为的常规文本框,一切正常,但是当我们将此键盘用于具有 "select all" 行为的文本框时,我们只能输入一个字符,因为它会在键盘上按键后选择文本。
我们检查过,这不是 mindfusion 键盘的问题,因为当我们将它用作用户控件时,它可以正常工作。当我们从他们的应用程序中打开此键盘时,它可以正常工作。当我们打开 windows 键盘时,它可以工作。
我认为最主要的是对话框 window。
我们尝试将其设置为 focusable=false
和 showactivated=false
但它不起作用。
我们还尝试使用 focusmanger 和 win32.showunactivated
这里是行为代码:
public class SelectAllTextOnFocusBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.GotKeyboardFocus += AssociatedObjectGotKeyboardFocus;
AssociatedObject.GotMouseCapture += AssociatedObjectGotMouseCapture;
AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObjectPreviewMouseLeftButtonDown;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.GotKeyboardFocus -= AssociatedObjectGotKeyboardFocus;
AssociatedObject.GotMouseCapture -= AssociatedObjectGotMouseCapture;
AssociatedObject.PreviewMouseLeftButtonDown -= AssociatedObjectPreviewMouseLeftButtonDown;
}
private void AssociatedObjectGotKeyboardFocus(object sender,
System.Windows.Input.KeyboardFocusChangedEventArgs e)
{
AssociatedObject.SelectAll();
Console.WriteLine("AssociatedObjectGotKeyboardFocus");
}
private void AssociatedObjectGotMouseCapture(object sender,
System.Windows.Input.MouseEventArgs e)
{
AssociatedObject.SelectAll();
Console.WriteLine("AssociatedObjectGotMouseCapture");
}
private void AssociatedObjectPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!AssociatedObject.IsKeyboardFocusWithin)
{
AssociatedObject.Focus();
Console.WriteLine("AssociatedObjectPreviewMouseLeftButtonDown");
e.Handled = true;
}
}
}
你知道如何防止它失去键盘焦点吗?
解决方法是在另一个线程中打开window... 我们浪费了12个小时来解决它..
var thread = new Thread(() =>
{
fk = new FullKeyboard();
fk.Show();
App.Current.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();