Skip to content

Instantly share code, notes, and snippets.

@mozesa
Created September 5, 2023 07:47
Show Gist options
  • Save mozesa/7f203bb926dab37eb41d783933cb48d2 to your computer and use it in GitHub Desktop.
Save mozesa/7f203bb926dab37eb41d783933cb48d2 to your computer and use it in GitHub Desktop.
OSK
public static class Osk
{
public static void Show()
{
if (InputPane.IsClosed())
TabTip.Toggle();
}
public static void Hide()
{
if (!InputPane.IsClosed())
TabTip.Toggle();
}
}
// https://stackoverflow.com/a/40921638
public static class TabTip
{
public static void Toggle()
{
var uiHostNoLaunch = new UIHostNoLaunch();
var tipInvocation = (ITipInvocation)uiHostNoLaunch;
tipInvocation.Toggle(GetDesktopWindow());
Marshal.ReleaseComObject(uiHostNoLaunch);
}
[ComImport, Guid("4ce576fa-83dc-4F88-951c-9d0782b4e376")]
class UIHostNoLaunch
{
}
[ComImport, Guid("37c994e7-432b-4834-a2f7-dce1f13b834b")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface ITipInvocation
{
void Toggle(IntPtr hwnd);
}
[DllImport("user32.dll", SetLastError = false)]
static extern IntPtr GetDesktopWindow();
}
// https://stackoverflow.com/a/55513524
public static class InputPane
{
[ComImport, Guid("D5120AA3-46BA-44C5-822D-CA8092C1FC72")]
public class FrameworkInputPane
{
}
[ComImport, SuppressUnmanagedCodeSecurity,
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("5752238B-24F0-495A-82F1-2FD593056796")]
public interface IFrameworkInputPane
{
[PreserveSig]
int Advise(
[MarshalAs(UnmanagedType.IUnknown)] object pWindow,
[MarshalAs(UnmanagedType.IUnknown)] object pHandler,
out int pdwCookie
);
[PreserveSig]
int AdviseWithHWND(
IntPtr hwnd,
[MarshalAs(UnmanagedType.IUnknown)] object pHandler,
out int pdwCookie
);
[PreserveSig]
int Unadvise(
int pdwCookie
);
[PreserveSig]
int Location(
out Rectangle prcInputPaneScreenLocation
);
}
public static bool IsClosed()
{
var inputPane = (IFrameworkInputPane)new FrameworkInputPane();
inputPane.Location(out var rect);
Marshal.ReleaseComObject(inputPane);
return rect is { Width: 0, Height: 0 };
}
}
@mozesa
Copy link
Author

mozesa commented Sep 5, 2023

Enable Show touch keyboard button at system try.
When there is a device (capable of touch input), this gist works, otherwise, one has to click the systray keyboard button to create an instance, from then on, it works.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment