다른 프로그램의 키입력을 후킹하고 싶을 때 처리하는 코드입니다.

 

using System;

using System.Runtime.InteropServices;

using System.Windows.Forms;

 

namespace Hooking

{

    public partial class MainForm : Form

    {

        [DllImport("user32.dll")]

        static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc callback, IntPtr hInstance, uint threadId);

 

        [DllImport("user32.dll")]

        static extern bool UnhookWindowsHookEx(IntPtr hInstance);

 

        [DllImport("user32.dll")]

        static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, int wParam, IntPtr lParam);

 

        [DllImport("kernel32.dll")]

        static extern IntPtr LoadLibrary(string lpFileName);

 

        private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

 

        const int WH_KEYBOARD_LL = 13;

        const int WM_KEYDOWN = 0x100;

 

        private IntPtr hook = IntPtr.Zero;

 

 

        public MainForm()

        {

            InitializeComponent();

 

            this.Load += MainForm_Load;

            this.FormClosing += MainForm_Closing;

        }

 

        private void SetHook()

        {

            IntPtr hInstance = LoadLibrary("User32");

 

            hook = SetWindowsHookEx(WH_KEYBOARD_LL, HookProc, hInstance, 0);

        }

 

        private void UnHook()

        {

            UnhookWindowsHookEx(hook);

        }

 

        private IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam)

        {

            if (code >= 0 && wParam == (IntPtr)WM_KEYDOWN)

            {

                int vkCode = Marshal.ReadInt32(lParam);

 

                // 키 입력에 따른 처리를 추가합니다.

                if (vkCode.ToString() == "17")

                {

                    this.hookTextBox.AppendText("CTRL");

                }

                else if (vkCode.ToString() == "91")

                {

                    this.hookTextBox.AppendText("LEFT WINDOW");

                }

                else if (vkCode.ToString() == "18")

                {

                    this.hookTextBox.AppendText("ALT");

                }

                else if (vkCode.ToString() == "32")

                {

                    this.hookTextBox.AppendText("SPACE BAR");

                }

                else if (vkCode.ToString() == "92")

                {

                    this.hookTextBox.AppendText("RIGHT WINDOW");

                }

                else if (vkCode.ToString() == "93")

                {

                    this.hookTextBox.AppendText("SELECT KEY");

                }

                else if (vkCode.ToString() == "16")

                {

                    this.hookTextBox.AppendText("SHIFT");

                }

                else if (vkCode.ToString() == "20")

                {

                    this.hookTextBox.AppendText("CAPS LOCK");

                }

                else if (vkCode.ToString() == "9")

                {

                    this.hookTextBox.AppendText("TAB");

                }

                else if (vkCode.ToString() == "96")

                {

                    this.hookTextBox.AppendText("numpad 0");

                }

                else if (vkCode.ToString() == "97")

                {

                    this.hookTextBox.AppendText("numpad 1");

                }

                else if (vkCode.ToString() == "98")

                {

                    this.hookTextBox.AppendText("numpad 2");

                }

                else if (vkCode.ToString() == "99")

                {

                    this.hookTextBox.AppendText("numpad 3");

                }

                else if (vkCode.ToString() == "100")

                {

                    this.hookTextBox.AppendText("numpad 4");

                }

                else if (vkCode.ToString() == "101")

                {

                    this.hookTextBox.AppendText("numpad 5");

                }

                else if (vkCode.ToString() == "102")

                {

                    this.hookTextBox.AppendText("numpad 6");

                }

                else if (vkCode.ToString() == "103")

                {

                    this.hookTextBox.AppendText("numpad 7");

                }

                else if (vkCode.ToString() == "104")

                {

                    this.hookTextBox.AppendText("numpad 8");

                }

                else if (vkCode.ToString() == "105")

                {

                    this.hookTextBox.AppendText("numpad 9");

                }

 

                this.hookTextBox.AppendText(" | "); // 구분자

 

                return CallNextHookEx(hook, code, (int)wParam, lParam); // 키입력을 정상적으로 동작하게 합니다.

 

                //return (IntPtr)1; // 키입력을 무효화 합니다.

            }

            else

            {

                return CallNextHookEx(hook, code, (int)wParam, lParam);

            }

        }

 

        private void MainForm_Load(object sender, EventArgs e)

        {

            SetHook();

        }

 

        private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)

        {

            UnHook();

        }

    }

}

 

 

 

어떤 프로그램이든 키보드 입력한 경우 아래처럼 후킹되어 표시됩니다.

 

+ Recent posts