Masking Keyboard and Mouse Events
-
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
-
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
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
-
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
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
-
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
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 forIMessageFilter.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
-
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
A simpler way is to add an
IMessageFilter
usingApplication.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
-
A simpler way is to add an
IMessageFilter
usingApplication.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
Hmmm... Didn't know that. I'll look into that for future reference. Thanks Heath! RageInTheMachine9532
-
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 forIMessageFilter.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
-
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
They are defined in the header files, take a look at windows.h for example. - Nick Parker
My Blog