当文本框获得焦点时,我正在运行虚拟键盘,但随后键盘应用程序获得焦点并且不会将键传输到文本框。如果我点击文本框来激活它,一切都很好,但我希望我的应用程序在 vKeyboard 进程运行后被激活。这是我迄今为止尝试过的:
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
……
vKeyboard = Process.Start(keyboardPath);
SetFocusToApplication(handle);
……
private static void SetFocusToApplication(IntPtr handle)
{
Process currentProcess = Process.GetCurrentProcess();
IntPtr hWnd = currentProcess.MainWindowHandle;
if (hWnd != IntPtr.Zero)
{
SetForegroundWindow(handle);
ShowWindow(hWnd,3);
}
}
我也尝试将 Alt + Tab 发送到键盘进程,但它不起作用:
private static void AltTab(IntPtr handle)
{
vKeyboard.WaitForInputIdle();
int WM_SYSCOMMAND = 0x0112;
int SC_PREVWINDOW = 0xF050;
PostMessage(vKeyboard.MainWindowHandle, WM_SYSCOMMAND, (IntPtr)SC_PREVWINDOW, (IntPtr)0);
}
PS:如果我可以从那里做任何事情来停用自己,我确实有虚拟键盘的源代码,仍然可以。让我知道。键盘最顶部的属性也设置为true,不确定这是否有任何不同。
这是我正在尝试并在按钮单击中起作用的代码:
Process vKeyboard;
string keyboardPath = Application.StartupPath + "\\vKeyboard.exe";
vKeyboard = Process.Start(keyboardPath);