Check when phone is no longer idle, i.e. keypress, etc. RRS feed

  • Question

  • Hi

    I have an application that runs in the background (SW_HIDE style) and I'd like for it to know when the phone is no longer idle.

    Basically if I can check whenever a key is pressed outside the application (or inside the application, or anywhere on the device for that matter).

    I guess what I want to do is similar to the trigger for the backlight, if you press a key, the backlight comes on. What I want to do, if user presses a key, my application should call a function and stop everything it's doing.

    I'm looking for features in the native APIs, I'm not using .NET.


    "The moon could not continue shining if it had to pay attention to all the dogs barking at it."
    Monday, July 13, 2009 4:00 AM


  • Hi Lionel,

    You can request power notifications from the system with the RequestPowerNotifications function, in which way your application is notified when a power state transition occurs. Please run the PowerManager sample in Windows Mobile SDKs:
      Windows Mobile 6 SDK Samples\Common\CPP\Win32\PowerManager

    Note the messages from the Output window when the backlight of a device is turned on/off (which usually means user is active/inactive):
      Power Notification Message: PBT_TRANSITION
      Flags: 10010000Length: 26SystemPowerState: backlightoff
      Power Notification Message: PBT_TRANSITION
      Flags: 12010000Length: 6SystemPowerState: on
      Power Notification Message: PBT_TRANSITION
      Flags: 10010000Length: 26SystemPowerState: backlightoff

    Another way is using Activity Timers (which in fact controls backlight's on/off under the hood). Specifically, you monitor the following named events:
      HANDLE hEventUserActive = OpenEvent(EVENT_ALL_ACCESS, FALSE, TEXT("PowerManager/UserActivity_Active"));
      HANDLE hEventUserInactive = OpenEvent(EVENT_ALL_ACCESS, FALSE, TEXT("PowerManager/UserActivity_Inactive"));

    As the names indicate, the former event is signaled when the user is active (touching touch screen, keyboard), while the later is signaled periodically when there is no user activity. You may need to build an sample application to monitor the behaviors of the two events before you can use them properly. For a sample usage, you can create a timer due in N seconds; when you receive an Active event, reset the timer; when timeout, the user is approximately idle for N seconds.

    Chunsheng Tang
    Please mark the post that helps you, and unmark that does not. This benefits our community members.
    • Marked as answer by Y-Lionel Thursday, July 16, 2009 12:24 PM
    Thursday, July 16, 2009 8:26 AM