首页 文章

如何将控制L和控制C发送到另一个应用程序?

提问于
浏览
3

我正在使用c#工作win应用程序,我想在firefox处于活动状态时发送Ctrl L和Ctrl C,找到地址栏并复制Url并从剪贴板获取信息 . 我想这样做 . 但我无法这样做 .

我怎样才能做到这一点?激活Firefox窗口,发送Ctrl L(激活地址栏),将Ctrl C(复制选择,即URL)发送到剪贴板并读取剪贴板 .

[return: MarshalAs(UnmanagedType.Bool)]
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool PostMessage(IntPtr hwnd, uint Msg, int wParam, int lParam);

    private const uint VK_CONTROL = 0x11;
    private const uint VK_L = 0x4C;
    private const uint VK_C = 0x43;


if (hwndfirfox == Process.GetProcessesByName("firefox")[0].MainWindowTitle)
       hwndfirfox = Process.GetProcessesByName("firefox")[0].MainWindowHandle;

if (hwndfirfox != null)
{
    PostMessage(hwndfirfox, VK_CONTROL, VK_L, 0);
    PostMessage(hwndfirfox, VK_CONTROL, VK_C, 0);
}

这段代码给出了错误,帮助我 .

2 回答

  • 4

    我看到一篇文章here,其中指出PostMessage不应该用于模拟键盘输入 . 您应该使用SendKeysSendInput .

  • 1

    您可能需要在CodeProject上考虑this article . MSDN上的SendKeys class似乎只允许您向当前活动的应用程序发送击键,这也可能对您有所帮助 .

相关问题