How to get power key current state pressed or not in C#

Sachi 221 Reputation points
2023-03-06T13:31:28.7866667+00:00

I am creating WinUI3 app i want to handle some events.

how to know whether power key status of pressed or not

I want to avoid the condition to go inside if the power key pressed.

please suggest.

Windows development | Windows App SDK
Windows development | Windows API - Win32
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 12,731 Reputation points Microsoft External Staff
    2023-03-29T07:47:10.5466667+00:00

    Hello @Sachi , according to Shutdown Notifications, which concentrates on shutting down, you can use the WM_QUERYENDSESSION and WM_ENDSESSION messages to receive shutdown notifications instead of checking the state of power key. I have tested that the messages work fine.

    For how to use the messages with WinUI3, see @Castorix31 's example.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.