Forums - C# Corner

Forum guidelines
jklanka

jklanka

  • 0
  • 1
  • 0

KeyBoard Hook not working

Dec 1 2007 6:14 AM


Hi

I have made a application to control the HotKeys.

The Form1 contains ::
------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace Hook
{
    public partial class Form1 : Form
    {

        private delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
        private delegate IntPtr HookHandlerDelegate(int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);

        [DllImport("user32.dll", EntryPoint = "SetWindowsHookExA", CharSet = CharSet.Ansi)]
        private static extern int SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, int hMod, int dwThreadId);
        [DllImport("user32.dll", EntryPoint = "UnHookWindowsHookEx", CharSet = CharSet.Ansi)]
        private static extern int UnHookWindowsEx(int hHook);
        [DllImport("user32.dll", EntryPoint = "CallNextHookEx", CharSet = CharSet.Ansi)]
        private static extern int CallNextHookEx(int hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);

        const int WH_KEYBOARD_LL = 13;

        public struct KBDLLHOOKSTRUCT
        {
            public int vkCode;
            int scanCode;
            public int flags;
            int time;
            int dwExtraInfo;
        }


        private int intLLKey;
        private KBDLLHOOKSTRUCT lParam;

        public Form1()
        {
            InitializeComponent();
        }

        private int LowLevelKeyboardProc(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            bool blnEat = false;
            switch (wParam)
            {
                case (256 | 257 | 260 | 261):
                    //Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key
                    if (((lParam.vkCode == 9) && (lParam.flags == 32)) || ((lParam.vkCode == 27) && (lParam.flags == 32)) || ((lParam.vkCode == 27) && (lParam.flags == 0)) || ((lParam.vkCode == 91) && (lParam.flags == 1)) || ((lParam.vkCode == 92) && (lParam.flags == 1)))
                    {
                        blnEat = true;
                    }
                    break;
            }
            if (blnEat)
                return 1;
            else return CallNextHookEx(0, nCode, wParam, ref lParam);

        }
        public void KeyboardHook(object sender, EventArgs e)
        {
            intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, new LowLevelKeyboardProcDelegate(LowLevelKeyboardProc),
                System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).ToInt32(),
                0);
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            KeyboardHook(sender, e);
        }

        public static void main()
        {           
            Application.Run(new Form1());

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //UnHookWindowsEx(intLLKey);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            KeyboardHook(sender, e);
        }


    }

}

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

But this is not wokring please help me.


Regards

jklanka


Download Free eBooks

SPONSORED BY

File APIs for .NET
Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!