첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형

■ RegisterHotKey/UnregisterHotKey API 함수 : 전역 핫키 사용하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainWindow.xaml

 

 

<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

 

 

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.dll")]

        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.dll")]

        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

    }

}

 

------------------------------------------------------------------------------------------------------------------------

728x90
반응형
Posted by 사용자 icodebroker

댓글을 달아 주세요