Hi, I'm Robinson, and I’m happy to help you today.
I know how frustrating this may be for you but I'll do my very best to help you.
The pen works transmitting the necessary electric field to make successful contact with a capacitive touch screen, and this area above the keyboard close to the hinges has the lid sensor. Probably the pen is activating this sensor and the laptop thinks you are closing the lid.
You can test if is it the case changing the power settings when the lid is closed.
1-Click on Start then search for "edit power plan" and click on that
2-Click on Change advanced power settings
3-Seach for the option when the lid is close and changes to "do nothing"
4-After changing check if the laptop will go to sleep again