6

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).

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Xapphire13
  • 61
  • 1
  • 3
  • possible duplicate of [Detecting windows shutdown event](http://stackoverflow.com/questions/3173825/detecting-windows-shutdown-event) – Jason Jan 29 '13 at 23:16
  • 3
    @Jason: not a duplicate. The OP is asking about intercepting the push of the power button (so that it does something other than shutting down) whereas the other question is about detecting that the system is shutting down. – Harry Johnston Jan 29 '13 at 23:32
  • possible duplicate of [How to deny shutdown when pressing the power button for a while?](http://stackoverflow.com/questions/642058/how-to-deny-shutdown-when-pressing-the-power-button-for-a-while) – DaveShaw Jan 29 '13 at 23:42
  • 2
    If it's not a duplicate of those two, then you have to do something like this: http://www.sevenforums.com/customization/189160-set-custom-option-power-button-action-list-default.html – DaveShaw Jan 29 '13 at 23:43
  • 8
    I know that you can use CallNtPowerInformation with SystemPowerPolicyAc (and/or Dc) to disable the power button so that it does nothing; to do this you need to set `PowerButton.Action` to `PowerActionNone` and `PowerButton.EventCode` to `POWER_FORCE_TRIGGER_RESET`. The same API provides an option for executing a program but it isn't clear to me how to use it. You may be able to work it out by experimentation, or perhaps searching on the keyword `POWER_LEVEL_USER_NOTIFY_EXEC` will give you some clues. – Harry Johnston Jan 29 '13 at 23:44
  • @DaveShaw: I don't think that's a duplicate either; that question wants to know how to prevent the user from forcing a power-off by holding the power button down (as opposed to pressing it normally) which is of course impossible since that behaviour is built in at the hardware level. What the OP wants here is at least physically possible, it just depends whether Windows allows it or not. :-) – Harry Johnston Jan 29 '13 at 23:48
  • @DaveShaw: The link you provided seems like it's referring to the software power button in the start menu. – Xapphire13 Jan 29 '13 at 23:54
  • @HarryJohnston: I will investigate what you suggested. It seems you understand what I wish to do. – Xapphire13 Jan 29 '13 at 23:55
  • What do you mean "ask me what to do?" reality is collapsing for your app it should pray "Our father.." and check things retrospective when it comes back online. – G.Y Jan 31 '13 at 10:12
  • 1
    This is the 'ask me what to do' dialog: [link](http://i.imgur.com/FIdwRxv.png) – Xapphire13 Jan 31 '13 at 21:11
  • [Capturing Physical Power Button Event](https://www.osronline.com/showthread.cfm?link=246968) – phuclv Jan 16 '18 at 16:35

2 Answers2

0

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.

Alos
  • 2,657
  • 5
  • 35
  • 47
0

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.

RandomUs1r
  • 4,010
  • 1
  • 24
  • 44