Click here to Skip to main content
15,886,077 members
Articles / Programming Languages / C#

A Simple C# Global Low Level Keyboard Hook

Rate me:
Please Sign up or sign in to vote.
4.90/5 (98 votes)
30 May 2007CPOL2 min read 973.7K   68.2K   171  
A simple description and sample of creating a global low level keyboard hook in C#
namespace key_preview {
	partial class Form1 {
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing) {
			if (disposing && (components != null)) {
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {
			this.lstLog = new System.Windows.Forms.ListBox();
			this.SuspendLayout();
			// 
			// lstLog
			// 
			this.lstLog.Dock = System.Windows.Forms.DockStyle.Fill;
			this.lstLog.FormattingEnabled = true;
			this.lstLog.Location = new System.Drawing.Point(0, 0);
			this.lstLog.Name = "lstLog";
			this.lstLog.Size = new System.Drawing.Size(292, 264);
			this.lstLog.TabIndex = 0;
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Controls.Add(this.lstLog);
			this.KeyPreview = true;
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}

		#endregion

		private System.Windows.Forms.ListBox lstLog;
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I currently work as a Software Engineer for a company in North Carolina, mainly working with C#.

Comments and Discussions