c# - 如何在非事件窗口中用代码模拟键盘事件?

标签 c# .net windows winapi

如何在不使用 SetForegroundWindow() 激活目标窗口的情况下使用 SendWait() 将击键发送到窗口?

这是 MSDN 站点上的 SendWait 示例:http://msdn.microsoft.com/en-us/library/ms171548.aspx

最佳答案

参见 this thread .基本上给窗口一些句柄,你需要使用 p/invoke 并调用 PostMessage使用 WM_KEYDOWN 消息:

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);
}

这是 the list虚拟键。

关于c# - 如何在非事件窗口中用代码模拟键盘事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3593596/

相关文章:

Windows 'dir' 命令,按名称排序和 <something>

c# - 如何通过多个接口(interface)协变 "carry"

C# 与 Python 参数传递

c# - 如何在 C# 中读取 MKV 电影文件的长度和其他信息?

c# - 如何模拟电源相关事件进行测试

.net - MVC 中的客户端证书颁发者(指纹)

Windows 程序有很大的 native 堆,比所有分配都大得多

c# - Novell LDAP C# - Novell.Directory.Ldap - 有人让它工作了吗?

c# - 抛出 Invalidcastexception 将 null 分配给可空十进制类型

node.js - 创建项目 cordova-lib 时出错 cordova-app-hello-world missing