I used KeyboardCleanTool but it didn't worked for me. What it does is to put an app window that captures keyboard events, but you can still press certain keys (like a lock screen shortcut in the touch bar) to by-pass it. Many times what I did is to open vi in the terminal... and it does the same without the extra app.