How to disable keyboard in C# Windows Form?

Asked

Viewed 692 times

2

Good Guys, I’m facing a problem with my application when I press the ALT key, I’m not expecting any kind of interaction through the keyboard, but even so exclusively this key gives this error inserir a descrição da imagem aqui

So I don’t know why this error, I think it’s due to a library that I use to do facial recognition, or because the error form is being called by another form incorrectly, I don’t know , in order wanted to disable the keyboard , because the application in that part does not need the keyboard.

  • Summary, pressing the Alt key while running the project happens this error?

  • Yes, It’s very strange

  • Have you tried putting this line of code inside a block try { } catch { }?

  • Yes, even with Try catch gives error

  • The error may be in setting up Windows keyboard layouts, and it occurs with special keys (alt, lgr, Windows Key, etc.) Try to add and remove a Keyboard layout in Control Panel.

1 answer

1

I use a Windows function, it locks iteration with keyboard and mouse as well.

 public class Utilidades 
 {

   [DllImport("user32.dll")]
   public static extern bool BlockInput(bool Blk);

 }

I use it like this:

Utilidades.BlockInput(true); //bloqueia
// codigos
Utilidades.BlockInput(false) // desbloqueia

add using the following namespace

System.Runtime.Interopservices;

  • It didn’t work, this APP comes from anywhere ?

  • is your class name. I will edit the code

  • Even so, it was not, say I imported there was no import error or syntax , I say it had no effect, for example I have a field there and keep typing

  • I just created a windows Forms project, with a textbox and a button. I also created the class above. so on the button I called Utilities.Blockinput(true). Locked keyboard and mouse. Only release with Ctrl+Alt+Del or calling method with false

  • If you call the method with true, and with false, then it will not give the effect. needs to have other executions that take time that you can realize.

  • I don’t know seen, like the problem is that I’m making a Visible = true panel into a method that’s called by another thread, although I use invoke .... it shows there the panel all right , I can press any key except the ALT or TAB that generates exception above

Show 1 more comment

Browser other questions tagged

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