Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Masking Keyboard and Mouse Events

Masking Keyboard and Mouse Events

Scheduled Pinned Locked Moved C#
help
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nagendra Kamath K
    wrote on last edited by
    #1

    I am developing a software where in I require to block the usage of Keyboard and Mouse and hence forth deny the usage of the system to the user. With KeyEventArgs I can block the usage of certain individual characters. But since i want to deny access of the whole system to the user, I need to mask even the Keyboard events such as "Alt+Tab" or "Ctrl+Alt+Del" etc... Any kind of help is appreciated. CHEERS - NAGU

    J D H 3 Replies Last reply
    0
    • N Nagendra Kamath K

      I am developing a software where in I require to block the usage of Keyboard and Mouse and hence forth deny the usage of the system to the user. With KeyEventArgs I can block the usage of certain individual characters. But since i want to deny access of the whole system to the user, I need to mask even the Keyboard events such as "Alt+Tab" or "Ctrl+Alt+Del" etc... Any kind of help is appreciated. CHEERS - NAGU

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      If you want to disable everything except a key combination you want to unlock the keyboard and mouse, you'll have to write a low-level keyboard hook into your app. This will let you get keyboard messages system wide. You might want to read this article on CP[^] for a little hooking in C# and follow the links in that article to some work by Dino Esposito on the subjet. Also, search through the posts here in C# and VB.NET for some discussions on hooking the keyboard. RageInTheMachine9532

      H 1 Reply Last reply
      0
      • N Nagendra Kamath K

        I am developing a software where in I require to block the usage of Keyboard and Mouse and hence forth deny the usage of the system to the user. With KeyEventArgs I can block the usage of certain individual characters. But since i want to deny access of the whole system to the user, I need to mask even the Keyboard events such as "Alt+Tab" or "Ctrl+Alt+Del" etc... Any kind of help is appreciated. CHEERS - NAGU

        J Offline
        J Offline
        Judah Gabriel Himango
        wrote on last edited by
        #3

        You can do this, or at least partially do this, utilizing Win32 keyboard and mouse hooks. Search the articles of this site, there's a few good ones covering Win32 hooks in .NET. --------------------------- He who knows that enough is enough will always have enough. -Lao Tsu

        1 Reply Last reply
        0
        • N Nagendra Kamath K

          I am developing a software where in I require to block the usage of Keyboard and Mouse and hence forth deny the usage of the system to the user. With KeyEventArgs I can block the usage of certain individual characters. But since i want to deny access of the whole system to the user, I need to mask even the Keyboard events such as "Alt+Tab" or "Ctrl+Alt+Del" etc... Any kind of help is appreciated. CHEERS - NAGU

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          A much easier way and more efficient way (without having to P/Invoke native APIs) is to simply add a IMessageFilter to pre-filter any messages sent to ONLY your application (as opposed to using a system-wide, low-level keyboard hook).

          using System;
          using System.Drawing;
          using System.Windows.Forms;

          public class Test : Form, IMessageFilter
          {
          static void Main(string[] args)
          {
          Test t = new Test();
          Application.AddMessageFilter(t);
          Application.Run(t);
          Application.RemoveMessageFilter(t);
          }

          public Test()
          {
          TextBox tb = new TextBox();
          tb.Location = new Point(8, 8);
          this.Controls.Add(tb);

          tb = new TextBox();
          tb.Location = new Point(8, 30);
          this.Controls.Add(tb);
          

          }

          private const int WM_KEYDOWN = 0x0100;
          private const int WM_LBUTTONDOWN = 0x0201;
          private const int WM_RBUTTONDOWN = 0x0204;
          private const int WM_XBUTTONDOWN = 0x020b;

          public bool PreFilterMessage(ref Message m)
          {
          if (m.Msg == WM_KEYDOWN ||
          m.Msg == WM_LBUTTONDOWN ||
          m.Msg == WM_RBUTTONDOWN ||
          m.Msg == WM_XBUTTONDOWN)
          return true;

          return false;
          

          }
          }

          Pay attention to the PreFilterMessage method, which is the implementation for IMessageFilter.PreFilterMessage. You could filter other application messages this way as well. Anything that goes through the application message pump can be filtered.

          Microsoft MVP, Visual C# My Articles

          M 1 Reply Last reply
          0
          • D Dave Kreskowiak

            If you want to disable everything except a key combination you want to unlock the keyboard and mouse, you'll have to write a low-level keyboard hook into your app. This will let you get keyboard messages system wide. You might want to read this article on CP[^] for a little hooking in C# and follow the links in that article to some work by Dino Esposito on the subjet. Also, search through the posts here in C# and VB.NET for some discussions on hooking the keyboard. RageInTheMachine9532

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            A simpler way is to add an IMessageFilter using Application.AddMessageFilter and doesn't require P/Invoking anything or using low-level hooks. It simply gives your implementation a chance to filter messages before they're dispatched to their target windows (controls).

            Microsoft MVP, Visual C# My Articles

            D 1 Reply Last reply
            0
            • H Heath Stewart

              A simpler way is to add an IMessageFilter using Application.AddMessageFilter and doesn't require P/Invoking anything or using low-level hooks. It simply gives your implementation a chance to filter messages before they're dispatched to their target windows (controls).

              Microsoft MVP, Visual C# My Articles

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Hmmm... Didn't know that. I'll look into that for future reference. Thanks Heath! RageInTheMachine9532

              1 Reply Last reply
              0
              • H Heath Stewart

                A much easier way and more efficient way (without having to P/Invoke native APIs) is to simply add a IMessageFilter to pre-filter any messages sent to ONLY your application (as opposed to using a system-wide, low-level keyboard hook).

                using System;
                using System.Drawing;
                using System.Windows.Forms;

                public class Test : Form, IMessageFilter
                {
                static void Main(string[] args)
                {
                Test t = new Test();
                Application.AddMessageFilter(t);
                Application.Run(t);
                Application.RemoveMessageFilter(t);
                }

                public Test()
                {
                TextBox tb = new TextBox();
                tb.Location = new Point(8, 8);
                this.Controls.Add(tb);

                tb = new TextBox();
                tb.Location = new Point(8, 30);
                this.Controls.Add(tb);
                

                }

                private const int WM_KEYDOWN = 0x0100;
                private const int WM_LBUTTONDOWN = 0x0201;
                private const int WM_RBUTTONDOWN = 0x0204;
                private const int WM_XBUTTONDOWN = 0x020b;

                public bool PreFilterMessage(ref Message m)
                {
                if (m.Msg == WM_KEYDOWN ||
                m.Msg == WM_LBUTTONDOWN ||
                m.Msg == WM_RBUTTONDOWN ||
                m.Msg == WM_XBUTTONDOWN)
                return true;

                return false;
                

                }
                }

                Pay attention to the PreFilterMessage method, which is the implementation for IMessageFilter.PreFilterMessage. You could filter other application messages this way as well. Anything that goes through the application message pump can be filtered.

                Microsoft MVP, Visual C# My Articles

                M Offline
                M Offline
                m_ramses
                wrote on last edited by
                #7

                I really was informed by these information, Thank you Heath. But how can one get the constant values for messages like WM_KEYDOWN...etc. Medhat Ramses

                N 1 Reply Last reply
                0
                • M m_ramses

                  I really was informed by these information, Thank you Heath. But how can one get the constant values for messages like WM_KEYDOWN...etc. Medhat Ramses

                  N Offline
                  N Offline
                  Nick Parker
                  wrote on last edited by
                  #8

                  They are defined in the header files, take a look at windows.h for example. - Nick Parker
                    My Blog

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups