Global Keyboard Hook in C#

Page 1 of 1

2 Replies - 265 Views - Last Post: 29 July 2011 - 10:21 AM Rate Topic: -----

#1 flawa  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 07-May 11

Global Keyboard Hook in C#

Posted 29 July 2011 - 09:23 AM

I want to detect all the keys like when I press "shift" and 'a' it will convert the character 'a' into 'A' or when I press the capslock then it will show all the characters into upper case characters, and it will also detect combination of keys like ctrl+shift+y, shift+ctrl+c or alt+ctrl+b etc.......I have hooked the keyboard but I have not succeeded in getting the above mentioned functionality all I am getting is whenever I press 'a' it shows me 'aa' means duplicate/double the character, so what should I do......

Here is my call back function of global keyboard hook


private  int callBack(int nCode, int wParam, ref hookStruct lParam)
        {
           char temp;
           
                if (nCode >= 0)
                {
                    Keys number = (Keys)Marshal.ReadInt32(lParam.vkCode);
                    switch (wParam)
                    { 
                        case WM_KEYDOWN : 
                        case WM_SYSKEYDOWN :
                            if (lParam.vkCode>=65 && lParam.vkCode<=90)
                            {
                               temp = (Char)lParam.vkCode;
                               temp = Char.ToLower(temp);
                              data = data + temp;
                            }

                         else if (lParam.vkCode >= 65 && lParam.vkCode <=90)
                           {
                               temp = (Char)lParam.vkCode;
                               temp = char.ToUpper(temp);  
                             data = data + temp;
                            }
                         else if (lParam.vkCode == 32)
                           {
                               temp = (Char)lParam.vkCode;
                               data = data + temp;
                           }

                            break;

                            
                    }


                }            
            
            return CallNextHookEx(hookID, nCode, wParam, ref lParam);
        }




I don't know if this function good enough to detect the keys that I want to detect..........

Here is my whole class of Keyboard hook

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;

namespace Low_Level_Keyboard_Hook
{
    class KeyboardHook
    {
        private string data;

        private const int VK_ALT = 0x12;
        private const int VK_CONTROL = 0x11;
        private const int VK_SHIFT = 16;


        public string getMyData()
        {
            return data;
        }

        private  delegate int  keyboardHookProc(int nCode, int wParam, ref hookStruct lParam);

        private keyboardHookProc kph;

        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private const int WM_KEYUP = 0x0101;
        private const int WM_SYSKEYUP = 0x0105;
        private const int WM_SYSKEYDOWN = 0x0104;
        
        //handle of the functions
        private IntPtr hookID = IntPtr.Zero;


        private struct hookStruct
        {
            public int vkCode;
            public  int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }


       public  KeyboardHook()
        {
            kph = new keyboardHookProc(callBack);
            hook();
        }

        ~KeyboardHook()
        {
            unHook();
        }

        
        //Installs the global hook
        public void hook()
        {
             IntPtr hInstance = LoadLibrary("USER32");
             hookID = SetWindowsHookEx(WH_KEYBOARD_LL, kph, hInstance, 0); //kph is pointing to KeyboardHookProc
        }
        
        //Unhooks the global hook
        public void unHook()
        {
            UnhookWindowsHookEx(hookID);
        }


        //This function will be called everytime a key is pressed
        private  int callBack(int nCode, int wParam, ref hookStruct lParam)
        {
           char temp;
           
                if (nCode >= 0)
                {
                    Keys number = (Keys)Marshal.ReadInt32(lParam.vkCode);
                    switch (wParam)
                    { 
                        case WM_KEYDOWN : 
                        case WM_SYSKEYDOWN :
                            if (lParam.vkCode>=65 && lParam.vkCode<=90)
                            {
                               temp = (Char)lParam.vkCode;
                               temp = Char.ToLower(temp);
                              data = data + temp;
                            }

                         else if (lParam.vkCode >= 65 && lParam.vkCode <=90)
                           {
                               temp = (Char)lParam.vkCode;
                               temp = char.ToUpper(temp);  
                             data = data + temp;
                            }
                         else if (lParam.vkCode == 32)
                           {
                               temp = (Char)lParam.vkCode;
                               data = data + temp;
                           }

                            break;

                            
                    }


                }            
            
            return CallNextHookEx(hookID, nCode, wParam, ref lParam);
        }

       
        [DllImport("user32.dll")]
        private static extern IntPtr SetWindowsHookEx(int idHook, keyboardHookProc callback, IntPtr hInstance, uint threadId);

        [DllImport("user32.dll")]
        private static extern bool UnhookWindowsHookEx(IntPtr hInstance);

        [DllImport("user32.dll")]
        private static extern int CallNextHookEx(IntPtr idHook, int nCode, int wParam, ref hookStruct lParam);

        [DllImport("kernel32.dll")]
        private static extern IntPtr LoadLibrary(string lpFileName);

        [DllImport("USER32.dll")]
        private static extern int GetAsyncKeyState(int vk);

        [DllImport("USER32.dll")]
        private static extern int GetKeyState(int nvk);

}

}


This post has been edited by flawa: 29 July 2011 - 09:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Global Keyboard Hook in C#

#2 Milto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 139
  • Joined: 10-February 11

Re: Global Keyboard Hook in C#

Posted 29 July 2011 - 10:06 AM

Key-logger like behavior?
It seems you have had other topics relating to such activity.

This post has been edited by Milto: 29 July 2011 - 10:19 AM

Was This Post Helpful? 0
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Global Keyboard Hook in C#

Posted 29 July 2011 - 10:21 AM

We generally don't help with these kinds of subjects because some people may have malicious intent. Sorry. :(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1