How can I intercept the push of the physical power button in Windows?

青春壹個敷衍的年華 提交于 2019-11-30 05:27:25

问题


Is there any way to intercept the event of the physical power button being pressed in order to react to this event? i.e.:

Push button -> My program catches the event -> My program performs action.

My goal is to write a C# program/service that will listen for the power button event and then open the "Ask me what to do" shutdown dialog (Like it did back in the days of Windows XP).


回答1:


You could use SystemEvents.SessionEnding Event

However I am not sure if this will work if the power button is pressed, this event Occurs when the user is trying to log off or shut down the system.




回答2:


I can explain it to you in theory: The button (it's a button that's tied to shutdown on most systems) has an option under "power options", where you can specificy the action to take when the power button is pressed...

It's that ACTION event you're looking for that's bound to the button by windows that THEN instantiates the shutdown event. There's programs that dissect and monitor windows variables and calls, you'd have to use one of those to figure it out and intercept/override it.



来源:https://stackoverflow.com/questions/14593834/how-can-i-intercept-the-push-of-the-physical-power-button-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!