- 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;
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
public void Start()
{
if (hHook != 0)
{
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);
}
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);
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);
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;
public IntPtr wParam;
public IntPtr lParam;
}
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 -->