Reputation: 374
So I am trying to make a program that disables all keyboard input but can still detect when a key is being pressed (doesn't matter which).
I tried using the BlockInput
method but it blocked ALL input (mouse and keyboard) and wouldn't allow for further detection of keyboard press.
This is my current code (function is a timer with a 1 tick interval)
private void detect_key_press_Tick(object sender, EventArgs e)
{
Process p = Process.GetCurrentProcess();
if (p != null)
{
IntPtr h = p.MainWindowHandle;
//SetForegroundWindow(h);
if (Keyboard.IsKeyDown(Key.A))
{
//SendKeys.SendWait("k");
this.BackColor = Color.Red;
}
else
{
this.BackColor = Control.DefaultBackColor;
}
}
}
How can I do this please? Thanks.
EDIT
I tried
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
e.Handled = true;
e.SuppressKeyPress = true;
}
but no success. Keys still get proccessed.
Upvotes: 1
Views: 2822
Reputation: 719
I assume you use Windows Forms ?
If you set the KeyPreview on your form to true and create an event on the KeyDown for the form, you can handle the keyboard input that way.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.S) // some accepted key
//Do something with it
else
{
//or cancel the key entry
e.Handled = true;
e.SuppressKeyPress = true;
}
}
Upvotes: 1