0

我已经尝试对此进行了一段时间的研究,但未能找到有效的答案。我正在尝试注册我的 WPF C# 应用程序,以便在应用程序运行时从任何地方监听 Ctrl+C。下面的代码是我的应用程序的一部分,当用户按下 Ctrl+C 时会触发,但是,剪贴板的内容是陈旧的(内容来自应用程序运行之前的前一个副本)。正在调用 AnimateWindow 函数,因此我知道我的热键注册正在工作。

所以,我的问题是,注册下面的热键似乎覆盖了复制功能。我想保持这个功能完好无损,因为它是我的应用程序工作方式的一部分。我需要获取复制的内容并将该数据用于数据库查询并填充文本框。

这只是整个项目的一个片段,如果需要额外的示例代码来帮助回答,请告诉我。谢谢!

    [DllImport("user32.dll")]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

    [DllImport("user32.dll")]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    // CTRL + C Hotkey
    private const int COPY_ID = 9001;
    private const uint MOD_CONTROL = 0x0002; // Modifier: Control
    private const uint VK_C = 0x43; // 'C' key
    /* Registering the hotkeys to be monitored */

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);

        _windowHandle = new WindowInteropHelper(this).Handle;
        _source = HwndSource.FromHwnd(_windowHandle);
        _source.AddHook(HwndHook);

        RegisterHotKey(_windowHandle, COPY_ID, MOD_CONTROL, VK_C); // 'C' key

    }

    /* The hook for pressing a hotkey */
    private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        const int WM_HOTKEY = 0x0312;
        switch (msg)
        {
            case WM_HOTKEY:
                switch (wParam.ToInt32())
                {
                    case COPY_ID:

                        AnimateWindow();

                        break;
                }
         }
    }
4

0 回答 0