Click here to Skip to main content
12,889,139 members (51,901 online)
Click here to Skip to main content


48 bookmarked
Posted 26 Nov 2008

Full Screen WPF Application with a Low-Level Keyboard Hook

, 26 Nov 2008 CPOL
This application displays shapes of random sizes and colors. It's perfect for babies and toddlers who love to sit and press away at the keyboard.
using System;
using System.Runtime.InteropServices;
using System.Windows;

namespace ShapeShow.SystemConfig
    sealed class KeyboardManager
        #region Private Members

        private delegate IntPtr HookHandlerDelegate(int nCode, IntPtr wParam, ref KBHookStruct lParam);
        private static HookHandlerDelegate callbackPtr;
        private static IntPtr hookPtr = IntPtr.Zero;
        private const int LowLevelKeyboardHook = 13;

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, HookHandlerDelegate callbackPtr, IntPtr hInstance, uint dwThreadId);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, ref KBHookStruct lParam);


        public static void DisableSystemKeys()
            if (callbackPtr == null)
                callbackPtr = new HookHandlerDelegate(KeyboardHookHandler);

            if (hookPtr == IntPtr.Zero)
                // Note: This does not work in the VS host environment.  To run in debug mode:
                // Project -> Properties -> Debug -> Uncheck "Enable the Visual Studio hosting process"
                IntPtr hInstance = Marshal.GetHINSTANCE(Application.Current.GetType().Module);
                hookPtr = SetWindowsHookEx(LowLevelKeyboardHook, callbackPtr, hInstance, 0);

        public static void EnableSystemKeys()
            if (hookPtr != IntPtr.Zero)
                hookPtr = IntPtr.Zero;

        private static IntPtr KeyboardHookHandler(int nCode, IntPtr wParam, ref KBHookStruct lParam)
            if (nCode == 0)
                if (((lParam.vkCode == 0x09) && (lParam.flags == 0x20)) ||  // Alt+Tab
                ((lParam.vkCode == 0x1B) && (lParam.flags == 0x20)) ||      // Alt+Esc
                ((lParam.vkCode == 0x1B) && (lParam.flags == 0x00)) ||      // Ctrl+Esc
                ((lParam.vkCode == 0x5B) && (lParam.flags == 0x01)) ||      // Left Windows Key
                ((lParam.vkCode == 0x5C) && (lParam.flags == 0x01)) ||      // Right Windows Key
                ((lParam.vkCode == 0x73) && (lParam.flags == 0x20)) ||      // Alt+F4
                ((lParam.vkCode == 0x20) && (lParam.flags == 0x20)))        // Alt+Space
                    return new IntPtr(1);

            return CallNextHookEx(hookPtr, nCode, wParam, ref lParam);

        private struct KBHookStruct
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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


About the Author

Barry Dorman
Software Developer
United States United States
Barry Dorman is a software engineer from the Birmingham, AL area. His primary focus is ASP.NET. Barry graduated with a BS in Electrical Engineering from The University of Alabama at Birmingham in 2006. Since then, he has kept himself busy working in the healthcare industry.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 26 Nov 2008
Article Copyright 2008 by Barry Dorman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid