Click here to Skip to main content
12,243,058 members (39,989 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8.4K views
196 downloads
7 bookmarked
Posted

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

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
Maxim Novak28-May-14 0:28
memberMaxim Novak28-May-14 0:28 
QuestionMy vote of 1 Pin
Emre Ataseven22-May-14 21:10
professionalEmre Ataseven22-May-14 21:10 
GeneralRe: My vote of 1 Pin
Rohan Leuva22-May-14 21:56
mvpRohan Leuva22-May-14 21:56 
GeneralRe: My vote of 1 Pin
Emre Ataseven22-May-14 22:07
professionalEmre Ataseven22-May-14 22:07 
GeneralRe: My vote of 1 Pin
Rohan Leuva22-May-14 22:10
mvpRohan Leuva22-May-14 22:10 
GeneralRe: My vote of 1 Pin
Emre Ataseven22-May-14 22:15
professionalEmre Ataseven22-May-14 22:15 
AnswerRe: My vote of 1 Pin
taha kharroubi22-May-14 22:19
membertaha kharroubi22-May-14 22:19 
GeneralMy vote of 1 Pin
Rodrigo Barros Pascual22-May-14 8:38
memberRodrigo Barros Pascual22-May-14 8:38 
General[My vote of 1] very little information Pin
Vijay Gill22-May-14 3:06
memberVijay Gill22-May-14 3:06 
GeneralMessage Removed Pin
tahakhc22-May-14 4:11
membertahakhc22-May-14 4:11 
GeneralRe: [My vote of 1] very little information Pin
John Brett22-May-14 4:51
memberJohn Brett22-May-14 4:51 
GeneralRe: [My vote of 1] very little information Pin
taha kharroubi22-May-14 4:59
membertaha kharroubi22-May-14 4:59 
GeneralMy vote of 3 Pin
azweepay22-May-14 3:04
memberazweepay22-May-14 3:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web01 | 2.8.160426.1 | Last Updated 22 May 2014
Article Copyright 2014 by taha kharroubi
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid