C#检测Ctrl + V键与RegisterHotKey而不是拦截它 [英] C# Detecting Ctrl+V with RegisterHotKey but not intercepting it

查看:1521
本文介绍了C#检测Ctrl + V键与RegisterHotKey而不是拦截它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要当用户按下Ctrl + V键(无论窗口焦点 - 我的应用程序可能会被最小化)来检测。但我不能停止的实际粘贴操作。

I need to detect when a user presses Ctrl+V (regardless of window focus - my app will likely be minimised) but I must not stop the actual paste operation.

我已经尝试了几件事情:(我成功地绑定到按键与RegisterHotKey)

I have tried a few things: (I am successfully binding to keystrokes with RegisterHotKey)

我有:

protected override void WndProc(ref Message m)
{
  if (m.Msg == 0x312)
    hotKey();
  base.WndProc(ref m);
}

和我已经试过如下:

void hotKey()
{
  SendKeys.SendWait("^v"); //just puts 'v' instead of clipboard contents
}

void hotKey()
{
  SendKeys.SendWait(ClipBoard.GetText());
  /* This works, but since Ctrl is still down, it triggers
   * all the shortcut keys for the app, e.g. if the keyboard
   * contains 's' then instead of putting 's' in the app, it
   * calls Ctrl+S which makes the app think the user wants
   * to save.
   */
}

目前唯一可行的解​​决方案我是绑定不同的东西,例如:按Ctrl + B,然后调用 SendKeys.SendWait(^ V); 然而,这是不理想的。

Currently the only working solution I have is to bind to something different, e.g. Ctrl+B and then call SendKeys.SendWait("^v"); however this isn't ideal.

一个理想的解决方案是,如果我的窗户没拦截在首位的按键,只是反应。

An ideal solution would be if my window didn't intercept the keystroke in the first place, just reacted.

推荐答案

可以通过使用SetWindowsHookEx函数利用挂钩做到这一点()。

You can do this by leveraging hooks using SetWindowsHookEx().

HHOOK WINAPI SetWindowsHookEx(
  __in  int idHook,
  __in  HOOKPROC lpfn,
  __in  HINSTANCE hMod,
  __in  DWORD dwThreadId
);



基本上,你可以设置一个低级别的键盘挂钩:

Basically, you can set up a low-level keyboard hook:

_hookHandle = SetWindowsHookEx(
    WH_KEYBOARD_LL,
    KbHookProc,                   // Your keyboard handler
    (IntPtr)0,
    0);                           // Set up system-wide hook.



捕捉到全系统的键盘事件。但它也可以让你做出这些键盘事件传递到其他应用程序。为了您的特定情况下,可以定义 KbHookProc 为:

private static int KbHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
    if (nCode >= 0) // This means we can intercept the event.
    {
        var hookStruct = (KbLLHookStruct)Marshal.PtrToStructure(
                lParam,
                typeof(KbLLHookStruct));

        // Quick check if Ctrl key is down. 
        // See GetKeyState() doco for more info about the flags.
        bool ctrlDown = 
                GetKeyState(VK_LCONTROL) != 0 ||
                GetKeyState(VK_RCONTROL) != 0;

        if (ctrlDown && hookStruct.vkCode == 0x56) // Ctrl+V
        {
            // Replace this with your custom action.
            Clipboard.SetText("Hi");
        }
    }

    // Pass to other keyboard handlers. Makes the Ctrl+V pass through.
    return CallNextHookEx(_hookHandle, nCode, wParam, lParam);
} 



我编写了一个快速和肮脏的WinForms应用程序来说明这一点。对于完整的代码列表,请参见 http://pastebin.com/uCSvqwb4

这篇关于C#检测Ctrl + V键与RegisterHotKey而不是拦截它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆