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

Sachi 221 Reputation points

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 App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
719 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,410 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,166 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 11,246 Reputation points Microsoft Vendor

    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