Click here to Skip to main content
15,896,606 members
Articles / Desktop Programming / Windows Forms

Low-level Windows API hooks from C# to stop unwanted keystrokes

Rate me:
Please Sign up or sign in to vote.
4.89/5 (88 votes)
26 Mar 200712 min read 606.8K   19.8K   231  
Babies and other animals love nothing better than to have a whack at the keyboard, with all sorts of unpredictable results. This application demonstrates how to trap keystrokes before they can do any damage.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace BabyKeyboardBash
{
    public partial class Form1 : Form
    {
        //Store an instance of DrawShapes
        DrawShapes ds;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Create an instance of DrawShapes, passing the form
            //to draw on and the settings from the config file
            string setting = Program.appSettings["DisplayConfig"];

            ds = new DrawShapes(this, setting);

            //Set up the event handler for the KeyboardHook's
            //KeyIntercepted event
            Program.kh.KeyIntercepted += new KeyboardHook.KeyboardHookEventHandler(kh_KeyIntercepted);
        }

        void kh_KeyIntercepted(KeyboardHook.KeyboardHookEventArgs e)
        {
            //Check if this key event is being passed to
            //other applications and disable TopMost in 
            //case they need to come to the front
            if (e.PassThrough)
            {
                this.TopMost = false;
            }
            
            ds.Draw(e.KeyName);
        }

        private void lblClear_Click(object sender, EventArgs e)
        {
            //Clear the form
            ds.Initialise();
        }


        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //Restore the screen from DrawShape's backup bitmap if all 
            //or part of the screen needs repainting
            using (Graphics graphics = e.Graphics)
            {
                graphics.DrawImage(ds.BackupBitmap, 0, 0);
            }

        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            //Put the form back on top if it is activated
            //(this will hide the Windows Taskbar)
            this.TopMost = true;
        }


    }
}

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
United Kingdom United Kingdom
Emma's first steps in programming took place at primary school over thirty years ago, thanks to a TI-99/4A and the LOGO language. Following a Master's degree in English Studies (obtained, strangely enough, with a paper on the birth of the microcomputer), Emma started her career in IT.

Over the last ten years, she has worked as a localiser, technical writer, editor, web designer, systems administrator, team leader and support engineer, before finally making the move into software development a few years ago. She is now thrilled on a daily basis that she is getting paid for writing code after doing it for free half her life!

Comments and Discussions