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.