I have a Winforms application done in C#.Net that works dandy on Windows
7. We spawn the Win7 virtual keyboard on tablets using the
Microsoft.Ink.TextInput.TextInputPanel API. windows figures out how to
properly position the keyboard so it doesn't
cover up the control that is in focus.
However, on Windows 8 no keyboard is spawned. Not only that, if you
manually trigger the keyboard as a user by hitting the keyboard icon,
Win8 doesn't apply any intelligence to place it so no guarantee the
control in focus isn't covered. It appears to
me that maybe this API isn't supported anymore but I thought I would
ask if others have hit this and figured out any workaround or alternate
Win8 API calls.
Our code extends the .NET TextBox control and overrides the OnGotFocus
& OnLostFocus event handlers to call the keyboard API to
display/dismiss the keyboard under the desired conditions.
Below is some technical detail around we have implemented this.
Globals Class:
public static TextInputPanel tip;
public static bool InitializeTip()
{
bool result = false;
try
{
tip = new TextInputPanel();
result = true;
}
catch (Exception)
{
// Can't instantiate Tip. That's ok for non Touch input devices. We check tip for nulls everywhere this global is used.
}
return result;
}
MyTextBoxClass:
protected override void OnGotFocus(EventArgs e)
{
InitializePasteHandler();
if (!_hasFocus && Globals.tip != null && !DisableVirtualKeyboard)
{
_hasFocus = true;
try
{
Globals.tip.AttachedEditControl = TextBox;
Globals.tip.DefaultInputArea = PanelInputArea.Keyboard;
Globals.tip.DefaultInPlaceState = InPlaceState.Expanded;
Globals.tip.InPlaceVisibleOnFocus = true;
Globals.tip.SetInPlaceVisibility(true);
}
catch (Exception)
{
// Unable to access virtual keyboard – no action necessary
}
}
base.OnGotFocus(e);
if (!(this is TimeTextBox)) // Only put cursor at end of Textbox if it is not a TimeTextBox
{
SelectionStart = Text.Length;
SelectionLength = 0;
}
}
protected override void OnLostFocus(EventArgs e)
{
_hasFocus = false;
if (Globals.tip != null)
{
try
{
Globals.tip.AttachedEditControl = null;
Globals.tip.AttachedEditControl = _defaultTipWindowsPtr;
}
catch (Exception)
{
// Unable to access virtual keyboard – no action necessary
}
}
base.OnLostFocus(e);
}
Thanks,
Dan