当前位置:   article > 正文

C# 全局响应Ctrl+Alt+鼠标右键_c# ctrl + alt

c# ctrl + alt

一、简述

某些应用,我们希望全局自定义热键。按键少了会和别的应用程序冲突,按键多了可定用户操作不变。因此我计划左手用Ctrl+Alt,右手用鼠标右键呼出我自定义的菜单。

我使用键盘和鼠标事件进行简单测试(Ctrl+鼠标右键),发现并不能成功。

RegisterHotKey(hWnd, HOTKEY_ID, (int)Modifiers.Control, (int)Keys.RButton);

因此只好使用“钩子”。

二、添加引用

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Diagnostics;

三、定义变量

  1. private const int WH_MOUSE_LL = 14;
  2. private const int WM_RBUTTONDOWN = 0x0204;
  3. private const int VK_CONTROL = 0x11;
  4. private const int VK_MENU = 0x12;
  5. private HookProc mouseProc;
  6. private IntPtr hookId = IntPtr.Zero;
  7. public event EventHandler HotkeyPressed;

四、编写过程

  1. private IntPtr SetMouseHook(HookProc proc)
  2. {
  3. using (Process curProcess = Process.GetCurrentProcess())
  4. using (ProcessModule curModule = curProcess.MainModule)
  5. {
  6. return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
  7. }
  8. }
  9. private IntPtr MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  10. {
  11. if (nCode >= 0 && wParam == (IntPtr)WM_RBUTTONDOWN)
  12. {
  13. int ctrl_keyState = GetAsyncKeyState(VK_CONTROL);
  14. int alt_keyState = GetAsyncKeyState(VK_MENU);
  15. // 检查Ctrl键和Alt键是否同时按下
  16. if ((ctrl_keyState < 0) & (alt_keyState < 0))
  17. {
  18. HotkeyPressed?.Invoke(this, EventArgs.Empty);
  19. // 返回非零值以阻止事件传递给其他窗口
  20. return new IntPtr(1);
  21. }
  22. }
  23. return CallNextHookEx(hookId, nCode, wParam, lParam);
  24. }
  25. private delegate IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam);
  26. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  27. private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);
  28. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  29. [return: MarshalAs(UnmanagedType.Bool)]
  30. private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  31. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  32. private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
  33. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  34. private static extern IntPtr GetModuleHandle(string lpModuleName);
  35. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  36. private static extern short GetAsyncKeyState(int vKey);
  37. private void HotkeyExample_HotkeyPressed(object sender, EventArgs e)
  38. {
  39. Console.WriteLine("Ctrl+Alt+鼠标右键被按下");
  40. // 可以执行其他操作...
  41. if (this.WindowState == FormWindowState.Minimized)
  42. {
  43. POINT M_Point = GetMousePosition();
  44. this.WindowState = FormWindowState.Normal;
  45. }
  46. else
  47. {
  48. this.WindowState = FormWindowState.Minimized;
  49. }
  50. }

五、调用

  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. this.HotkeyPressed += HotkeyExample_HotkeyPressed;
  4. mouseProc = MouseHookCallback;
  5. hookId = SetMouseHook(mouseProc);
  6. }
  7. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  8. {
  9. UnhookWindowsHookEx(hookId);
  10. }

六、运行效果

 通过以上代码,自定义窗体顺利弹出,而且并不会与别的程序冲突,满足了需求。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/272527
推荐阅读
相关标签
  

闽ICP备14008679号