Prevent X error

Move grabkeyboard() to when the window is visible.
Else we get the following X error:
X Error: BadWindow (invalid Window parameter) 3
  Major opcode: 31 (X_GrabKeyboard)
  Resource id:  0x0