Grunthos
Grunthos

Reputation: 55

Win32 - Intercept keyboard sleep button

Every keyboard action can be caught with SetWindowsHookEx(), but not the sleep button. You can set it to "Do nothing" in the control panel, but I want to do something useful with it. Is this possible? (I'm using a Das Keyboard 4 Professional).

Upvotes: 1

Views: 144

Answers (1)

Anders
Anders

Reputation: 101607

This old MSDN page lists the scan codes for the power action keys:

The following defines the correct scan codes for keyboard power switches for OnNow hardware:

Power event 
 Set1:    Make = E0, 5E    Break = E0, DE
 Set2:    Make = E0, 37    Break = E0, F0, 37

Sleep event 
 Set1:    Make = E0, 5F    Break = E0, DF
 Set2:    Make = E0, 3F    Break = E0, F0, 3F

Wake event 
 Set1:    Make = E0, 63    Break = E0, E3
 Set2:    Make = E0, 5E    Break = E0, F0, 5E

The Set 2 break codes also need the 0xE0 prefix, otherwise the system will see the new key going down but see some different key coming up.

but if you look at the USB HID tables there is something called "Power Controls"

Power controls can step the system through the following states: Full Power, Low Power, and Power Down

Power control usages found in a System Control collection affect system level power.

System Sleep OSC – Asserted when the intended action is to initiate system-wide low power mode now. If the system is already in the Low Power state, there is no effect.

so it is possible that the keyboard is using the USB protocol directly to sleep.

Upvotes: 2

Related Questions