WindowsHotKeyAPI.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. namespace Azylee.Core.WindowsUtils.APIUtils
  8. {
  9. public class WindowsHotKeyAPI
  10. {
  11. //======================================
  12. //如果函数执行成功,返回值不为0。
  13. //如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
  14. /// <summary>
  15. /// 注册热键
  16. /// </summary>
  17. /// <param name="hWnd">要定义热键的窗口的句柄</param>
  18. /// <param name="id">定义热键ID(不能与其它ID重复) </param>
  19. /// <param name="fsModifiers">标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效</param>
  20. /// <param name="vk">定义热键的内容</param>
  21. /// <returns></returns>
  22. [DllImport("user32.dll", SetLastError = true)]
  23. public static extern bool RegisterHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk);
  24. /// <summary>
  25. /// 删除热键
  26. /// </summary>
  27. /// <param name="hWnd">要取消热键的窗口的句柄</param>
  28. /// <param name="id">要取消热键的ID</param>
  29. /// <returns></returns>
  30. [DllImport("user32.dll", SetLastError = true)]
  31. public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
  32.         //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
  33.         [Flags()]
  34. public enum KeyModifiers
  35. {
  36. None = 0,
  37. Alt = 1,
  38. Ctrl = 2,
  39. Shift = 4,
  40. WindowsKey = 8
  41. }
  42. }
  43. }