locked
Catch power button events RRS feed

  • Question

  • Hi,
    I implemented a key hook using the SDK which provides a class called KeyboardHook. With this class I can divert keyboard events which works fine except for the power key which actually is the key I want to catch.

    When the power key is pressed, the mobile phone changes to the suspended mode but actually I just want to go to the user idle mode so I thought I could catch the power button event since it is defined in the msdn library. However, my application does not react on the power button press although the device is running in unattended mode. Is there a way of changing the assigned action from going to suspended mode to user idle mode?

    x81

    VK_F18

    VK_TPOWER

    Power.

    Wednesday, July 1, 2009 7:52 AM

Answers

  • Power Button is managed by GWES and power management. It's something usually implemented by OEMs and can't be changed. So no keyboard hooks can do the trick, guys.
    If You'll find my answer satisfactory or helpful - mark it as answered! Thank You. PS. Votes also doesn't hurt :).
    • Proposed as answer by Mal Loth Friday, July 3, 2009 10:24 AM
    • Marked as answer by Ltty Wednesday, July 8, 2009 10:39 AM
    Friday, July 3, 2009 10:21 AM
  • Actually this would be a solution or at least a workaround that can be excepted in my case. I'd have to change the registry entry at application startup and restore it once the application is disposed.

    For the moment I used a bit more "dirty" workaround. I set the device in unattended mode and prevent the device from going to standby by resetting the systemidletimer. At the same time I check the system state. If the system state is unattended, I switch to user idle, so the device is for about 30 seconds in unattended mode. However, I think your solution is the better one.

    Can I assume that this registry entry is the same for every manufacturer?
    • Marked as answer by Ltty Monday, July 6, 2009 9:33 PM
    Monday, July 6, 2009 9:33 PM

All replies

  • Here is a couple of articles that might help you:

    Windows Mobile Power Management
    Windows Mobile Interprocess Communication with Message Queues in .NET - see towards the end how to intercept the power button press.
    João Paulo Figueira (Device Application Development MVP)
    Wednesday, July 1, 2009 8:39 AM
  • Well, thank you but actually I am developing .NET C# stuff. OpenNETCF provides also a powermanagement class with an event handler that should fire an event when the device is powered down or goes to suspend mode. However this works great for OnPowerUp and OnIdle but not if I press the power key...
    Wednesday, July 1, 2009 9:17 AM
  • First please read this article:

    Keyboard hook in the CF v2
    http://blog.opennetcf.com/ayakhnin/PermaLink,guid,84b95802-76d7-499c-b266-e2251ab15706.aspx 

    In the hook procedure LowLevelKeyboardProc, if you do not call CallNextHookEx, you can prevent the message from reaching the rest of the hook chain, and possibly stop the PPC from suspending itself.


    Please mark the post that helps you, and unmark that does not. This benefits our community members.
    Friday, July 3, 2009 9:13 AM
  • Hi,

    Chunsheng Tang, I am not sure whether this approach will allow for catching of Power Button events as the Power Key does not raise an event the device I have tested on. Is this OEM specific?

    Thanks

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    Friday, July 3, 2009 9:37 AM
  • Have you tried it? What I was trying to say is that possibly a keyboard hook can intercept and suppress the power key press before the device can be suspended. If a keyboard hook cannot do it, I don't think something else can. As you know when the device is suspended, applicaitons are not notified but "paused" where they are.
    Please mark the post that helps you, and unmark that does not. This benefits our community members.
    • Edited by warrentang Friday, July 3, 2009 10:19 AM
    Friday, July 3, 2009 10:17 AM
  • Hi,

    Yes i did give it a quick run through and was unable to hook into the Power Button.

    Thanks

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    Friday, July 3, 2009 10:18 AM
  • Power Button is managed by GWES and power management. It's something usually implemented by OEMs and can't be changed. So no keyboard hooks can do the trick, guys.
    If You'll find my answer satisfactory or helpful - mark it as answered! Thank You. PS. Votes also doesn't hurt :).
    • Proposed as answer by Mal Loth Friday, July 3, 2009 10:24 AM
    • Marked as answer by Ltty Wednesday, July 8, 2009 10:39 AM
    Friday, July 3, 2009 10:21 AM
  • Hi,
    I implemented a key hook using the SDK which provides a class called KeyboardHook. With this class I can divert keyboard events which works fine except for the power key which actually is the key I want to catch.

    When the power key is pressed, the mobile phone changes to the suspended mode but actually I just want to go to the user idle mode so I thought I could catch the power button event since it is defined in the msdn library. However, my application does not react on the power button press although the device is running in unattended mode. Is there a way of changing the assigned action from going to suspended mode to user idle mode?

    x81

    VK_F18

    VK_TPOWER

    Power.



    You can do this in the registry. Go to [HKLM\System\CurrentControlSet\Control\Power\State\Suspend] and change Flags to the one in [HKLM\System\CurrentControlSet\Control\Power\State\UserIdle]. It's the only way it can be done.

    If You'll find my answer satisfactory or helpful - mark it as answered! Thank You. PS. Votes also doesn't hurt :).
    • Proposed as answer by Mal Loth Friday, July 3, 2009 10:24 AM
    • Marked as answer by Ltty Monday, July 6, 2009 9:33 PM
    • Unmarked as answer by Ltty Wednesday, July 8, 2009 10:24 AM
    Friday, July 3, 2009 10:24 AM
  • Actually this would be a solution or at least a workaround that can be excepted in my case. I'd have to change the registry entry at application startup and restore it once the application is disposed.

    For the moment I used a bit more "dirty" workaround. I set the device in unattended mode and prevent the device from going to standby by resetting the systemidletimer. At the same time I check the system state. If the system state is unattended, I switch to user idle, so the device is for about 30 seconds in unattended mode. However, I think your solution is the better one.

    Can I assume that this registry entry is the same for every manufacturer?
    • Marked as answer by Ltty Monday, July 6, 2009 9:33 PM
    Monday, July 6, 2009 9:33 PM
  • I'm sorry but changing the flags in the registry does not work.
    Wednesday, July 8, 2009 10:24 AM
  • You can as well change the suspend guid (but first write it down for backup) for that of useridle.
    Or even easier - swipe Suspend key name with UserIdle and vice versa.
    Should do the work if OEM implemented suspend button action via power name not via power flag.

    Those registries are in all PPC devices (not sure about SP).
    Play with it - I did it successfuly in XDA Flame device to only get Screen Off instead of Suspend.


    If You'll find my answer satisfactory or helpful - mark it as answered! Thank You. PS. Votes also doesn't hurt :).
    • Edited by Mal Loth Wednesday, July 8, 2009 10:41 PM
    Wednesday, July 8, 2009 10:40 PM