Use an API function to log the keyboard events, but only if the window you are logging has the focus.