Fix bug where keyboard sometimes remain open when closing a window with the titlebar