有点晚了,我将改进 tasaki 示例,以便在用户单击 Windows 8 平板电脑的 WPF 应用程序中的文本框时启用显示/隐藏 gotFocus/LostFocus 事件。我希望这对人们有所帮助有类似的头痛,因为如果你想用触摸事件滚动,禁用 InkHelper 并不能很好地工作......
首先,您必须将这些引用添加到您的 App.Xaml.cs 文件中。
using System.Management;
using System.Runtime.InteropServices;
编码:
protected override void OnStartup(StartupEventArgs eventArgs)
{
EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotFocusEvent,
new RoutedEventHandler(GotFocus_Event), true);
EventManager.RegisterClassHandler(typeof(TextBox), UIElement.LostFocusEvent,
new RoutedEventHandler(LostFocus_Event), true);
MainApplication.Show();
}
private static void GotFocus_Event(object sender, RoutedEventArgs e)
{
// TestKeyboard();
OpenWindows8TouchKeyboard(sender, e);
}
//http://www.c-sharpcorner.com/UploadFile/29d7e0/get-the-key-board-details-of-your-system-in-windows-form/
private static bool IsSurfaceKeyboardAttached()
{
SelectQuery Sq = new SelectQuery("Win32_Keyboard");
ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher(Sq);
ManagementObjectCollection osDetailsCollection = objOSDetails.Get();
//Windows 8 tablet are returnign 2 device when keyboard is connecto
//My application won't be used for Desktop so this condition is fine
//But u might want to see if keyboard is usb and == 1 so you are
//returning true or not on tablet.
return osDetailsCollection.Count <= 1 ? true : false;
}
private static void OpenWindows8TouchKeyboard(object sender, RoutedEventArgs e)
{
var textBox = e.OriginalSource as TextBox;
if (textBox != null && IsSurfaceKeyboardAttached())
{
var path = @"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe";
if (!File.Exists(path))
{
// older windows versions
path = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\osk.exe";
}
Process.Start(path);
textBox.BringIntoView();//SetFocus so u dont lose focused area
}
}
[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;
public const int SC_MINIMIZE = 0xF020;
private 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);
}
}
private void LostFocus_Event(object sender, EventArgs e)
{
// It's time to close the onscreen keyboard.
CloseOnscreenKeyboard();
}