Register HotKey

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace TestRegisterHotKey
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        private void btnRegister_Click(object sender, RoutedEventArgs e)
        {
            IntPtr handel = new WindowInteropHelper(this).Handle;
            NativeMethods.RegisterHotKey(handel, 1, (uint)(ModifierKeys.None|ModifierKeys.Control), (uint)Keys.A);
            NativeMethods.RegisterHotKey(handel, 2, (uint)(ModifierKeys.None), (uint)Keys.B);
            NativeMethods.RegisterHotKey(handel, 3, (uint)(ModifierKeys.Shift), (uint)Keys.C);
            NativeMethods.RegisterHotKey(handel, 4, (uint)(ModifierKeys.Windows), (uint)Keys.D);
            HwndSource.FromHwnd(handel).AddHook(new HwndSourceHook(hook));
        }


        private IntPtr hook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == NativeMethods.WM_HOTKEY) 
            {
                switch (wParam.ToInt32()) 
                {
                    case 1:
                        tbContent.Text = "Control +A";
                        break;
                    case 2:
                        tbContent.Text = "B";
                        break;
                    case 3:
                        tbContent.Text = "Shift +C";
                        break;
                    case 4:
                        tbContent.Text = "Windows +D";
                        break;
                }
                handled = true;
            }
            return IntPtr.Zero;
        }


        private void btnUnRegister_Click(object sender, RoutedEventArgs e)
        {
            IntPtr handle = new WindowInteropHelper(this).Handle;
            NativeMethods.UnregisterHotKey(handle, 1);
            NativeMethods.UnregisterHotKey(handle, 2);
            NativeMethods.UnregisterHotKey(handle, 3);
            NativeMethods.UnregisterHotKey(handle, 4);
        }
    }

}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;


namespace TestRegisterHotKey
{
    public delegate int HookProc(int code, IntPtr wparam, IntPtr lparam);
    public static class NativeMethods
    {


        /// <summary>
        /// The const WM_HOTKEY value is 0x0312
        /// </summary>
        public const uint WM_HOTKEY = 0x0312;


        /// <summary> The RegisterHotKey function defines a system-wide hot key </summary>
        /// <param name="hwnd">Handle to the window that will receive WM_HOTKEY messages  generated by the hot key.</param>
        /// <param name="id">Specifies the identifier of the hot key.</param>
        /// <param name="fsModifiers">Specifies keys that must be pressed in combination with the key  specified by the 'vk' parameter in order to generate the WM_HOTKEY message.</param>
        /// <param name="vk">Specifies the virtual-key code of the hot key</param>
        /// <returns><c>true</c> if the function succeeds, otherwise <c>false</c></returns>
        /// <seealso cref="http://msdn.microsoft.com/en-us/library/ms646309(VS.85).aspx"/>
        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);


        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值