I found some code that use libhal to do detection, I extract them and wrote the 
following code which works well. 
*I'm wondering if there's any similar part in libudev?*

#include <hal/libhal.h>
#include <dbus/dbus.h>
#include <glib.h>
#include <glib-object.h>

#define BUTTON_POWER_UDI  
"/org/freedesktop/Hal/devices/computer_logicaldev_input"

static void condition_cb(LibHalContext *ctx, const char *udi, const char *name, 
const char *detail);

int main()
{
    LibHalContext *ctx = NULL;
    DBusConnection *conn = NULL;
    GMainLoop *main_loop = NULL;
    DBusError err;

    dbus_error_init(&err);
    conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
    if (conn == NULL) {
        g_print("dbus get failed %s\n", err.message);
    }
    dbus_connection_setup_with_g_main(conn, NULL);

    if ((ctx = libhal_ctx_new()) == NULL) {
        g_print("libhal_ctx_new failed\n");
        return -1;
    }

    if(!libhal_ctx_set_dbus_connection(ctx, conn)) 
    {
        g_print("set dbus connection failed\n");
        return -1;
    }

    if (!libhal_ctx_set_device_condition(ctx, condition_cb)) {
        g_print("set device condition failed\n");
        return -1;
    }

    dbus_error_init(&err);
    if (!libhal_ctx_init(ctx, &err)) {
       g_print("Hal init error %s \n", err.message);
       return -1;
    }

    dbus_error_init(&err);
    if (!libhal_device_add_property_watch(ctx, BUTTON_POWER_UDI, &err)) {
        g_print("Failed to add property watch to power button %s\n", 
err.message);
        return -1;
    }

    main_loop = g_main_loop_new(NULL, TRUE);
    g_main_loop_run(main_loop);
}

static void condition_cb(LibHalContext *ctx, const char *udi, const char *name, 
const char *detail)
{
    g_print("udi is %s, name is %s, detail is %s\n", udi, name, detail);
    if (!strcmp(name, "ButtonPressed") && !strcmp(detail, "power"))
        g_print("power button pressed\n");
}

Makefile :  gcc -o hal_detect_power_button main.c `pkg-config --cflags --libs 
hal glib-2.0 dbus-glib-1`

-----Original Message-----
From: [email protected] [mailto:[email protected]] On 
Behalf Of Rusty Lynch
Sent: Tuesday, January 25, 2011 10:26 AM
To: [email protected]
Subject: Re: [MeeGo-dev] How can i detect power button press

First a warning.... not all buttons are connected to the system in the 
same manner.  If you lucky the platform exposes the button as an input 
device mapped to a key.

Ok... now if you are lucky enough to have the power button for your 
specific device mapped to some key, you can use XGrabKey from a process 
inside the user session but without active keyboard focus (like a 
homescreen running in the desktop.)  This isn't a Qt mechanism but a low 
level XLib call.

On a linux workstation run 'man XGrabKey' to learn all about it.

     --rusty

On 01/24/2011 05:51 PM, zhu wrote:
> I think the question is
> " How to get the keypressevent when the application is not
> active(don't have the focus widget) "
>
>
> On Tue, Jan 25, 2011 at 9:41 AM, Zhang, Zheng<[email protected]>  wrote:
>    
>> Write a Qt application, get keyPressEvent(QKeyEvent* event). event->key().
>>
>>
>>
>> From: [email protected] [mailto:[email protected]] On
>> Behalf Of Zheng, Huan
>> Sent: Monday, January 24, 2011 4:44 PM
>> To: [email protected]; [email protected] community
>> Subject: [MeeGo-dev] How can i detect power button press
>>
>>
>>
>> Hi, dear developers
>>
>> How can I detect power button press?
>>
>> And further more, how can I detect the button press that I'm interested in?
>>
>> Thanks!
>>
>> _______________________________________________
>> MeeGo-dev mailing list
>> [email protected]
>> http://lists.meego.com/listinfo/meego-dev
>>
>>
>>      
> _______________________________________________
> MeeGo-dev mailing list
> [email protected]
> http://lists.meego.com/listinfo/meego-dev
>    

_______________________________________________
MeeGo-dev mailing list
[email protected]
http://lists.meego.com/listinfo/meego-dev
_______________________________________________
MeeGo-dev mailing list
[email protected]
http://lists.meego.com/listinfo/meego-dev

Reply via email to