简体   繁体   English

如何在不活动的窗口中用代码模拟键盘事件?

[英]How to simulate Keyboard Events in Code in not active window?

How do I use SendWait() to send key strokes to a window without using SetForegroundWindow() to make the target window active? 如何在不使用SetForegroundWindow()使目标窗口处于活动状态的情况下使用SendWait()将击键发送到窗口?

Here is the SendWait example on the MSDN site: http://msdn.microsoft.com/en-us/library/ms171548.aspx 这是MSDN站点上的SendWait示例: http : //msdn.microsoft.com/zh-cn/library/ms171548.aspx

See this thread . 看到这个线程 Basically given some handle to a window, you need to use p/invoke and call PostMessage with the WM_KEYDOWN message: 基本上为窗口提供了一些句柄,您需要使用p / invoke并使用WM_KEYDOWN消息调用PostMessage

private const int VK_RETURN = 0x0D;
private const uint WM_KEYDOWN = 0x0100;

[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);    

public void SendKeysToWindow(IntPtr hWnd)
{
    PostMessage(hWnd, WM_KEYDOWN, new IntPtr(VK_RETURN), IntPtr.Zero);
}

Here's the list of Virtual Keys. 这是虚拟密钥的列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM