public static class InkInputHelper
{
public static void DisableWPFTabletSupport()
{
TabletDeviceCollection devices = System.Windows.Input.Tablet.TabletDevices;
if (devices.Count > 0)
{
Type inputManagerType = typeof(System.Windows.Input.InputManager);
object stylusLogic = inputManagerType.InvokeMember("StylusLogic",
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, InputManager.Current, null);
if (stylusLogic != null)
{
Type stylusLogicType = stylusLogic.GetType();
while (devices.Count > 0)
{
stylusLogicType.InvokeMember("OnTabletRemoved",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
null, stylusLogic, new object[] { (uint)0 });
}
}
}
}
}
使用该类来确定是否存在物理键盘,或者有其他更适合您需求的方式。
我使用这个类在任何我想要的地方打开和关闭键盘。
class KeyboardManager
{
public static void LaunchOnScreenKeyboard()
{
var processes = Process.GetProcessesByName("osk").ToArray();
if (processes.Any())
return;
string keyboardManagerPath = "KeyboardExecuter.exe";
Process.Start(keyboardManagerPath);
}
public static void KillOnScreenKeyboard()
{
var processes = Process.GetProcessesByName("osk").ToArray();
foreach (var proc in processes)
{
proc.Kill();
}
}
public static void killTabTip()
{
var processes = Process.GetProcessesByName("TabTip").ToArray();
foreach (var proc in processes)
{
proc.Kill();
}
}
public static void LaunchTabTip()
{
Process.Start("TabTip.exe");
}
}
请记住以下内容:
我添加了osk.exe和tabtip.exe的副本。
将其添加到我的程序中解决了在32/64位上无法使用tabtip或osk的问题。
osk是键盘,tabtip是其停靠版本。
keyboardexecuter是我制作的一个程序,用作备用方法。
注意*我目前无法在触摸屏设备上测试此功能。您必须自行尝试。
为了使所有内容正常工作,我在我的mainwindow中使用了以下代码:
public int selectedTableNum;
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
InkInputHelper.DisableWPFTabletSupport();
Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() =>
{
var navWindow = Window.GetWindow(this) as NavigationWindow;
if (navWindow != null) navWindow.ShowsNavigationUI = false;
}));
KeyboardManager.LaunchTabTip();
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
InputPanelConfiguration cp = new InputPanelConfiguration();
IInputPanelConfiguration icp = cp as IInputPanelConfiguration;
if (icp != null)
icp.EnableFocusTracking();
mainFrame.Content = new LoginPage();
}
我包含了注释,因为如果有错误的话,它可能对某些人有用。
输入面板配置:
[Guid("41C81592-514C-48BD-A22E-E6AF638521A6")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IInputPanelConfiguration
{
int EnableFocusTracking();
}
[ComImport, Guid("2853ADD3-F096-4C63-A78F-7FA3EA837FB7")]
class InputPanelConfiguration
{
}
我希望这能帮助到未来访问此问题的访客。
TextBox
时,内置的虚拟键盘可以显示出来,并且应该是用户友好的,例如键盘永远不会覆盖输入元素。 - user1031200