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);
}
}