I needed to teach the product I’m working on about the ACPI power button, so I fired up dbus-monitor --system to see what events are emitted when the button is pressed. It was easy to find the device in question in d-feet afterwards. Unfortunately, the HAL device objects (/org/freedesktop/Hal/devices/computer_logicaldev_input_N) that emit the events for the various ACPI buttons change between machines and even between reboots, so it’s necessary to dynamically discover them by querying HAL for all devices with the “button” capability.
This little spike solution prints out messages to stdout whenever certain special ACPI keys on your keyboard are pressed (on my Lenovo ThinkPad T61, it shows the power button, the volume buttons, the Fn-key chords for changing backlight, etc.):
import gobject import dbus import dbus.mainloop.glib def getButtonCondition(condition, btn): print "Condition: %s, button: %s" % (condition, btn) mainloop = gobject.MainLoop() dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) bus = dbus.SystemBus() hal_manager_proxy = bus.get_object('org.freedesktop.Hal', '/org/freedesktop/Hal/Manager') hal_manager = dbus.Interface(hal_manager_proxy, dbus_interface='org.freedesktop.Hal.Manager') button_paths = hal_manager.FindDeviceByCapability('button') button_proxies = [] buttons = [] for p in button_paths: button_proxy = bus.get_object('org.freedesktop.Hal', p) button = dbus.Interface(button_proxy, dbus_interface = 'org.freedesktop.Hal.Device') button.connect_to_signal("Condition", getButtonCondition) button_proxies.append(button_proxy) buttons.append(button) mainloop.run()