첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form, IMessageFilter
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            Application.AddMessageFilter(this);

            Load += Form_Load;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 사전 메시지 필터 처리하기 - PreFilterMessage(message)

        /// <summary>
        /// 사전 메시지 필터 처리하기
        /// </summary>
        /// <param name="message">메시지</param>
        /// <returns>처리 결과</returns>
        public bool PreFilterMessage(ref Message message)
        {
            if(message.Msg == WIN32Helper.WM_INPUT)
            {
                RawInput rawInput = WIN32Helper.GetDeviceID(message);

                string source = (message.WParam.ToInt32() == WIN32Helper.RIM_INPUT) ? "this" : "others";

                switch(rawInput.Header.Type)
                {
                    case RawInputType.Keyboard :

                        Console.WriteLine
                        (
                            "[{0}] [키보드] 장치 ID : {1}, 가상키 : {2}, 코드 : {3} 메시지 : {4}",
                            source,
                            rawInput.Header.Device,
                            rawInput.Keyboard.VKey,
                            rawInput.Keyboard.MakeCode,
                            rawInput.Keyboard.Message
                        );

                        break;

                    case RawInputType.Mouse :

                        Console.WriteLine
                        (
                            "[{0}] [마우스] 장치 ID : {1}, X : {2}, Y : {3}",
                            source,
                            rawInput.Header.Device,
                            rawInput.Mouse.LastX,
                            rawInput.Mouse.LastY
                        );

                        break;
                }
            }

            return false;
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 폼 닫을 경우 처리하기 - OnFormClosing(e)

        /// <summary>
        /// 폼 닫을 경우 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            Application.RemoveMessageFilter(this);

            base.OnFormClosing(e);
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

        /// <summary>
        /// 폼 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private unsafe void Form_Load(object sender, EventArgs e)
        {
            RAWINPUTDEVICE [] rawInputDevice = new RAWINPUTDEVICE[2];

            rawInputDevice[0].UsagePage    = HIDUsagePage.Generic;
            rawInputDevice[0].Usage        = HIDUsage.Mouse;
            rawInputDevice[0].Flag         = RawInputDeviceFlag.InputSink;
            rawInputDevice[0].WindowHandle = Handle;

            rawInputDevice[1].UsagePage    = HIDUsagePage.Generic;
            rawInputDevice[1].Usage        = HIDUsage.Keyboard;
            rawInputDevice[1].Flag         = RawInputDeviceFlag.InputSink;
            rawInputDevice[1].WindowHandle = Handle;

            if(WIN32Helper.RegisterRawInputDevices(rawInputDevice, 2, sizeof(RAWINPUTDEVICE)) == false)
            {
                MessageBox.Show("Failed to register");
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요