Click here to Skip to main content
Click here to Skip to main content

Hooking Keys in .NET

, 22 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
This application is used to demonstrate how to hook memory and block keyboard keys

Introduction

This application is used to demonstrate how to hook memory and block keyboard keys. In this example, we blocked F2; this application is useful when we use virtual keyboard and also a device keyboard.

Background

I made this small application when I found a problem with blocking Key press from virtual keyboard.

Run Single Instance of an Application using Mutex

In the main method, we implement Mutex to check that we are running a single instance at a time.

private static string appGuid = "c0a76b5a-12ab-45c5-b9d9-d693faa6e7b9";

        [STAThread]
        static void Main()
        {
           using (Mutex mutex = new Mutex(false, @"Global\" + appGuid))
            {
                if (!mutex.WaitOne(0, false))
                {
                    MessageBox.Show("Instance déjà en cours d'exécution",
                          "F2Lock",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    return;
                }
                ProcessIcon pi = new ProcessIcon();
                pi.Display();
               Application.Run();
            }
            }

// 

Display Method

This method is used to display app in the systemTray and start Hooking Keys Press.

public  void Display()
        {
            // Put the icon in the system tray and allow it react to mouse clicks.            
            ni.MouseClick += new MouseEventHandler(ni_MouseClick);
            ni.Icon = Resources.Cadenas__2_;
            ni.Text = "F2Locker";
            ni.Visible = true;
// Start Hooking memory 
            globalKeyboardHook gkh = new globalKeyboardHook();
          globalKeyboardHook.HookedKeys.Add(Keys.F2);
          globalKeyboardHook.KeyDown += new KeyEventHandler(gkh_KeyDown);
          globalKeyboardHook.KeyUp += new KeyEventHandler(gkh_KeyUp);
        }

Reference

History

  • 21-05-2014 Second release
  • 20-03-2014 First release

License

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

Share

About the Author

taha kharroubi

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberMaxim Novak27-May-14 23:28 
QuestionMy vote of 1 [modified] PinprofessionalEmre Ataseven22-May-14 20:10 
GeneralRe: My vote of 1 PinmvpRohan Leuva22-May-14 20:56 
GeneralRe: My vote of 1 PinprofessionalEmre Ataseven22-May-14 21:07 
GeneralRe: My vote of 1 PinmvpRohan Leuva22-May-14 21:10 
GeneralRe: My vote of 1 PinprofessionalEmre Ataseven22-May-14 21:15 
AnswerRe: My vote of 1 Pinmembertaha kharroubi22-May-14 21:19 
GeneralMy vote of 1 PinmemberRodrigo Barros Pascual22-May-14 7:38 
General[My vote of 1] very little information PinmemberVijay Gill22-May-14 2:06 
GeneralMessage Removed Pinmembertahakhc22-May-14 3:11 
GeneralRe: [My vote of 1] very little information PinmemberJohn Brett22-May-14 3:51 
GeneralRe: [My vote of 1] very little information Pinmembertaha kharroubi22-May-14 3:59 
GeneralMy vote of 3 Pinmemberazweepay22-May-14 2:04 

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 | Mobile
Web01 | 2.8.141022.2 | Last Updated 22 May 2014
Article Copyright 2014 by taha kharroubi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid