How I was trying to fix broken lid closing sensor on my laptop and what I learned

Some time back my laptop refused to switch on. It was not reacting to any attempts including battery reset. Technical support spent few months to replace practically every component and learn the problem was caused by the tiny flat cable connecting the screen to the motherboard. They’ve replaced it, PC went to life and finally I got it back. But on the very first day I found an interesting issue — looks like support guys disconnected or broke lid closing sensor. Laptop is not going into sleep when lid is closed. I’ve checked every single setting in power management, spent few hours with Google and Lenovo forums, but no luck. Returning laptop back to support was too scary after all.

Ok, I can’t fix this mechanical issue, but probably I can do a simple workaround? Somehow I got used to lock my PC with Win+L shortcut when leaving. Why not to make another shortcut to make it sleep? Digging into this I found it is not too difficult to put a PC into sleep mode, but making a new global shortcut e.g. Ctrl+Q is not that simple. There is a plenty of software that can do it for you, but usually it brings a bunch of other functions I don’t really need. So, why not to code something that would exactly resolve my issue. Co-incidentally I have few hours and strong wish practice my not so great coding skills.

In order to keep it simple I’ve decided to go with C#, Windows Form App and Visual Studio Community Edition. I’ve made a tiny project with a form and a single control notifyIcon on it. I’ve coded the form to become invisible right after application starts and added a handler for MouseDoubleClick for notifyIcon control to make the form visible again. After some attempts I’ve learned that it would be good to unregistered shortcuts I’ve created after application terminates.

Application is set to auto-start and by pressing Ctrl+Q I’m able to put my laptop into sleep mode. This is for sure not the ideal solution, but something I can live with.

Coding is fun and can give a lot of motivation if you are trying to do something that really matters to you.

If you might want to see the code, please check below. There is also a link to the Visual Studio project on git hub.