This is completely untested on my part. Just a theoretical script.
You need to create a condition somewhere. Call it: "keyboard_active" & set to true
Now add this to the script section.
function keyboardHandler(eventType, character, keycode, modifiers)
if not Conditions["keyboard_active"].ConditionValue then
To toggle keyboard on / off just change the condition value.