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.