Grab keyboard and hide window with Esc

Pretty simple, but the whole device grabbing thing is kind of a mess so I'd appreciate someone having a look over this.