How to disable some C#keys?

Asked

Viewed 1,990 times

1

I would like to know how to disable/block some keyboard keys while the program is running or even cancel the lock?

For example: If I disable/block the entire keyboard or just allow x key it would not be possible to use it in any other program until close the program or cancel the key lock.

Has as ?

Thank you.

  • Good afternoon, You need to make this lock in a windows form or web application?

  • You need to make this lock in a windows form or web application?

  • Take a look at this article. http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C

1 answer

0


Answer if it is a Desktop application, in the case of Web application, I know no solution.

First of all, this is not a cool thing to do, in case you have some antivirus or something like this your application will be detected as virus, even because many keyloggers use the same technique.

For this you will need to perform some low-level Hooks in the system. There are many ways to do this, you can do 'at hand', or use some frameworks.

In the example below I will do from a framework that this in that answer here.

Github Framework Globalmousekeyhook

nuget install MouseKeyHook

My code in Windows Forms looks like this:

private IKeyboardMouseEvents m_GlobalHook;
private void Form1_Load(object sender, EventArgs e)
{
    m_GlobalHook = Hook.GlobalEvents();    //inicia instancia do hook        
    m_GlobalHook.KeyDown += MetodoKeyPressHook; //cria um hook para o metodo
}

private void MetodoKeyPressHook(object sender, KeyEventArgs e)
{
    //apenas para o botao espaço:
    if(e.KeyCode == Keys.Space)
        e.Handled = true;

    //ou simplesmente para todas as teclas:
    e.Handled = true;

    //exemplo abaixo ele deixa normal as teclas e não ignora.
    e.Handled = false;
}

PS: be careful when debugging :D.

  • Well, it worked and it didn’t work in the program I wanted. But it was almost.

  • It was just run as an administrator, now function :D, thank you all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.