■ RegisterHotKey/UnregisterHotKey API 함수를 사용해 전역 핫키를 사용하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="RegisterHotKey/UnregisterHotKey API 함수를 사용해 전역 핫키 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <TextBlock Name="textBlock" /> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
using System; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 핫키 등록하기 - RegisterHotKey(windowHandle, id, modifiers, virtualKey) /// <summary> /// 핫키 등록하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="id">ID</param> /// <param name="modifiers">수정자</param> /// <param name="virtualKey">가상 키</param> /// <returns>처리 결과</returns> [DllImport("user32")] private static extern bool RegisterHotKey(IntPtr windowHandle, int id, uint modifiers, uint virtualKey); #endregion #region 핫키 등록 취소하기 - UnregisterHotKey(windowHandle, id) /// <summary> /// 핫키 등록 취소하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="id">ID</param> /// <returns>처리 결과</returns> [DllImport("user32")] private static extern bool UnregisterHotKey(IntPtr windowHandle, int id); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// HOTKEY_ID /// </summary> private const int HOTKEY_ID = 9000; /// <summary> /// MODIFIER_NONE /// </summary> private const uint MODIFIER_NONE = 0x0000; /// <summary> /// MODIFIER_ALT /// </summary> private const uint MODIFIER_ALT = 0x0001; /// <summary> /// MODIFIER_CONTROL /// </summary> private const uint MODIFIER_CONTROL = 0x0002; /// <summary> /// MODIFIER_SHIFT /// </summary> private const uint MODIFIER_SHIFT = 0x0004; /// <summary> /// MODIFIER_WINDOWS /// </summary> private const uint MODIFIER_WINDOWS = 0x0008; /// <summary> /// VK_CAPITAL /// </summary> /// <remarks>CAPS LOCK</remarks> private const uint VK_CAPITAL = 0x14; /// <summary> /// WM_HOTKEY /// </summary> private const int WM_HOTKEY = 0x0312; /// <summary> /// 윈도우 핸들 /// </summary> private IntPtr windowHandle; /// <summary> /// 윈도우 핸들 소스 /// </summary> private HwndSource windowHandleSource; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 소스 초기화시 처리하기 - OnSourceInitialized(e) /// <summary> /// 소스 초기화시 처리하기 /// </summary> /// <param name="e">이벤트 발생자</param> protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); this.windowHandle = new WindowInteropHelper(this).Handle; this.windowHandleSource = HwndSource.FromHwnd(this.windowHandle); this.windowHandleSource.AddHook(ProcessWindowHandleHooking); RegisterHotKey(this.windowHandle, HOTKEY_ID, MODIFIER_CONTROL, VK_CAPITAL); } #endregion #region 종료시 처리하기 - OnClosed(e) /// <summary> /// 종료시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnClosed(EventArgs e) { this.windowHandleSource.RemoveHook(ProcessWindowHandleHooking); UnregisterHotKey(this.windowHandle, HOTKEY_ID); base.OnClosed(e); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Public #region 윈도우 핸들 후킹 처리하기 - ProcessWindowHandleHooking(windowHandle, message, wordParameter, longParameter, handled) /// <summary> /// 윈도우 핸들 후킹 처리하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="message">메시지</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <param name="handled">처리 여부</param> /// <returns>처리 결과</returns> private IntPtr ProcessWindowHandleHooking(IntPtr windowHandle, int message, IntPtr wordParameter, IntPtr longParameter, ref bool handled) { switch(message) { case WM_HOTKEY : switch(wordParameter.ToInt32()) { case HOTKEY_ID : int virtualKey = (((int)longParameter >> 16) & 0xffff); if(virtualKey == VK_CAPITAL) { this.textBlock.Text += "CTRL+CAPS 키가 눌렸습니다." + Environment.NewLine; } handled = true; break; } break; } return IntPtr.Zero; } #endregion } } |