Unify offscreen detection in keyboard and mouse

Use the same logic to determine if the shape is off screen for both
keyboard nudging and mouse movement.