Click here to Skip to main content
11,492,639 members (60,481 online)
Click here to Skip to main content

Tagged as

Hooking the keyboard message queue in compact framework code

, 4 Jan 2010 CPOL 26.6K 3
Rate this:
Please Sign up or sign in to vote.
Hooking the keyboard message queue in CF2
  • Download KbdHookCS - Keyboard hooking in compact framework

Here is some nice code to use a keyboard hook implemented in C# for compact framework.

You can use this to catch the funny OS assigned keys like F1, F2, F3, F4, F6 and F7 (Softkey 1 and 2, Phone, End, Volume Up, Volume Down); and last but not least catch the Win Key press.

The hooking class:

using System;
using System.Runtime.InteropServices;
/*
In order to use this class in your program, just declare the variable 
and hook up into HookEvent:
HookKeys hook = new HookKeys();
hook.HookEvent += new HookKeys.HookEventHandler(HookEvent);
hook.Start();
*/
public class HookKeys
{
#region delegates
    public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
    public delegate void HookEventHandler(HookEventArgs e, KeyBoardInfo keyBoardInfo);
    public HookEventHandler HookEvent;
#endregion
#region fields
    private HookProc hookDeleg;
    private static int hHook = 0;
#endregion
 
    public HookKeys()
    {
    }
    ~HookKeys(){
        if(hHook!=0)
            this.Stop();
    }
    #region public methods
    ///
    /// Starts the hook
    ///
    public void Start()
    {
        if (hHook != 0)
        {
            //Unhook the previous one
            this.Stop();
        }
        hookDeleg = new HookProc(HookProcedure);
        hHook = SetWindowsHookEx(WH_KEYBOARD_LL, hookDeleg, GetModuleHandle(null), 0);
        if (hHook == 0)
        {
            throw new SystemException("Failed acquiring of the hook.");
        }
        AllKeys(true);
    }
    ///
    /// Stops the hook
    ///
    public void Stop()
    {
        UnhookWindowsHookEx(hHook);
        AllKeys(false);
    }
    #endregion
    #region protected and private methods
    protected virtual void OnHookEvent(HookEventArgs hookArgs, KeyBoardInfo keyBoardInfo)
    {
        if (HookEvent != null)
        {
            HookEvent(hookArgs, keyBoardInfo);
        }
    }
 
    private int HookProcedure(int code, IntPtr wParam, IntPtr lParam)
    {
       KBDLLHOOKSTRUCT hookStruct =  
	(KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
       if (code < 0)
            return CallNextHookEx(hookDeleg, code, wParam, lParam);
       // Let clients determine what to do
       HookEventArgs e = new HookEventArgs();
       e.Code = code;
       e.wParam = wParam;
       e.lParam = lParam;
       KeyBoardInfo keyInfo = new KeyBoardInfo();
       keyInfo.vkCode = hookStruct.vkCode;
       keyInfo.scanCode = hookStruct.scanCode;
       OnHookEvent(e, keyInfo);
       // Yield to the next hook in the chain
       return CallNextHookEx(hookDeleg, code, wParam, lParam);
   }
   #endregion
   #region P/Invoke declarations
 
   [DllImport("coredll.dll")]
   private static extern int AllKeys(bool bEnable);
 
   [DllImport("coredll.dll")]
   private static extern int SetWindowsHookEx
	(int type, HookProc hookProc, IntPtr hInstance, int m);
   [DllImport("coredll.dll")]
   private static extern IntPtr GetModuleHandle(string mod);
   [DllImport("coredll.dll")]
   private static extern int CallNextHookEx(
           HookProc hhk,
           int nCode,
           IntPtr wParam,
           IntPtr lParam
           );
   [DllImport("coredll.dll")]
   private static extern int GetCurrentThreadId();
   [DllImport("coredll.dll", SetLastError = true)]
   private static extern int UnhookWindowsHookEx(int idHook);
   private struct KBDLLHOOKSTRUCT
   {
       public int vkCode;
       public int scanCode;
       public int flags;
       public int time;
       public IntPtr dwExtraInfo;
   }
   const int WH_KEYBOARD_LL = 20;
   #endregion
}
#region event arguments
 
    public class HookEventArgs : EventArgs
    {
        public int Code;    // Hook code
        public IntPtr wParam;   // WPARAM argument
        public IntPtr lParam;   // LPARAM argument
    }
    public class KeyBoardInfo
    {
        public int vkCode;
        public int scanCode;
        public int flags;
        public int time;
    }
#endregion

Here is the download of the Visual Studio 2005 Windows Mobile 6 SDK targeting source code.

<!-- Social Bookmarks BEGIN -->

<!-- Social Bookmarks END -->

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

hjgode

Germany Germany
No Biography provided

Comments and Discussions

 
QuestionWon't work if the application loses focus (WM6.5) Pin
tgz833-Jan-12 6:30
membertgz833-Jan-12 6:30 
AnswerRe: Won't work if the application loses focus (WM6.5) Pin
hjgode3-Jan-12 7:46
memberhjgode3-Jan-12 7:46 
QuestionRe: Won't work if the application loses focus (WM6.5) Pin
tgz833-Jan-12 23:33
membertgz833-Jan-12 23:33 
AnswerRe: Won't work if the application loses focus (WM6.5) Pin
hjgode4-Jan-12 6:37
memberhjgode4-Jan-12 6:37 
GeneralRe: Won't work if the application loses focus (WM6.5) Pin
tgz835-Jan-12 3:55
membertgz835-Jan-12 3:55 
GeneralUsing without form Pin
Campese28-Mar-11 6:01
memberCampese28-Mar-11 6:01 
GeneralRe: Using without form Pin
hjgode28-Mar-11 19:31
memberhjgode28-Mar-11 19:31 
GeneralAfter a messagebox hangs Pin
mamm98998-Mar-11 23:49
membermamm98998-Mar-11 23:49 
GeneralRe: After a messagebox hangs Pin
hjgode9-Mar-11 6:43
memberhjgode9-Mar-11 6:43 
GeneralRe: After a messagebox hangs Pin
mamm98999-Mar-11 22:16
membermamm98999-Mar-11 22:16 
GeneralRe: After a messagebox hangs Pin
Adam Cooke15-Jan-13 22:41
memberAdam Cooke15-Jan-13 22:41 
GeneralSetWindowsHookEx returns 87 Pin
Member 769177121-Feb-11 2:58
memberMember 769177121-Feb-11 2:58 
GeneralRe: SetWindowsHookEx returns 87 Pin
hjgode21-Feb-11 3:23
memberhjgode21-Feb-11 3:23 
GeneralRe: SetWindowsHookEx returns 87 Pin
Member 769177121-Feb-11 3:29
memberMember 769177121-Feb-11 3:29 
GeneralRe: SetWindowsHookEx returns 87 Pin
Member 769177121-Feb-11 3:36
memberMember 769177121-Feb-11 3:36 
QuestionHTC HD2 incoming call don´t lock Green Button/Answer Key? [modified] Pin
ths18015-Dec-10 9:44
memberths18015-Dec-10 9:44 
AnswerRe: HTC HD2 incoming call don´t lock Green Button/Answer Key? Pin
hjgode15-Dec-10 22:20
memberhjgode15-Dec-10 22:20 
AnswerRe: HTC HD2 incoming call don´t lock Green Button/Answer Key? Pin
ths18016-Dec-10 2:38
memberths18016-Dec-10 2:38 
GeneralGreat code Pin
James P Grady13-Nov-10 11:57
memberJames P Grady13-Nov-10 11:57 
QuestionAllKeys? Pin
Steven Richardson28-Sep-10 2:03
memberSteven Richardson28-Sep-10 2:03 
AnswerRe: AllKeys? Pin
hjgode28-Sep-10 18:51
memberhjgode28-Sep-10 18:51 
GeneralGreat code!! Pin
DizZ31-May-10 6:00
memberDizZ31-May-10 6:00 
GeneralRe: Great code!! Pin
DizZ31-May-10 6:37
memberDizZ31-May-10 6:37 
GeneralRe: Great code!! Pin
hjgode31-May-10 8:08
memberhjgode31-May-10 8:08 
GeneralRe: Great code!! Pin
Member 769177121-Feb-11 20:32
memberMember 769177121-Feb-11 20:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 4 Jan 2010
Article Copyright 2010 by hjgode
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid