How to disable some C # keys?

1

I would like to know how to disable / lock some keys on the keyboard while the program is running or until the lock is canceled?

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

What is it like?

Thank you.

    
asked by anonymous 29.12.2015 / 19:26

1 answer

0

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

First of all, this is not something cool to do, if you have any antivirus or something, your application will be detected as a virus, even though many keyloggers use the same technique.

For this you will need to make some low level hooks on the system. There are many ways to do this, you can do 'on hand', or use some frameworks.

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

Github GlobalMouseKeyHook Framework

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: take care when debugging: D.

    
30.12.2015 / 08:53