Problems with restricting input to external window

When I click and activate another application and start typing, my program detects it as input. For whatever reason, my program is retaining the keyboard focus. I have HWND_NOTOPMOST set in setwindowpos.

Please help me out with this.