You can use KeyboardCleanTool (made by the developer of BetterTouchTool).

It opens the application window with a button you have to press in order to lock/unlock the keyboard.

Download it from [BetterTouchTool.net][1]

[![Screenshot of KeyboardCleanTool on macOS 10.12][2]][2]


  [1]: https://bettertouchtool.net/releases/KeyboardCleanTool.zip
  [2]: https://i.sstatic.net/MXBwn.png