I am sorry but this thread is not silly, someone else might be.
Yes I am silly, I apologize...
I have already the code you posted above but it is not catching the Sleep key.
I missed that. (Or you did not mention this before?)
(I suppose you mean the code in the link I posted.)
All other keys are catched, except power keys (power on, sleep, wake) and laptops' Fn key. That's why I still insist. Am I clear now?
Perfectly clear, and now I know we are talking about the same thing.
(I was beginning to doubt that, since my previous posts seemed not to be read by the others, so I asked myself if I was talking about something else completely.)
a "multimedia" keybaord that has keys for play, pause, etc and often sleep.
Now that you mention it: my old PC has a button like that (my new laptop doesn't). I once pressed it (never read the manual of course) and it immediately crashed my PC (mind you, it runs WinMe, so not surprising)

The "silliness" is more in the perseveration of using the OnKeyDown method (or even crt.readkey) in several posts.
It simply will not work, as clearly demonstrated (and it won't work in Delphi either).
Googling for vk_sleep, most hits indicate faliure in trying to capture this, even with a keyboard hook.
Most hits are about mentioning this, I did not actually find a thread that proofed it could be done.
As for the PBT_APMSUSPEND message.
If I understand the
MS site about it correctly, you cannot cancel this?
You could try installing a driver that returns FALSE upon a PBT_APMQUERYSUSPEND message, but this is not guaranteed to work, since SetSystemPowerState can be invoked by a driver or program without a prior PBT_APMQUERYSUSPEND request message.
Queted from Handling OS Events in Windows XP and Vista
In Windows XP and earlier, the PBT_APMQUERYSUSPEND event allowed applications to deny/abort the suspend operation from completing by returning BROADCAST_QUERY_DENY.
...
In Windows Vista, applications can no longer block in-progress sleep transitions. The PBT_APMQUERYSUSPEND event is not delivered to applications in Vista and hence applications cannot deny/abort the suspend operation. This is as per Microsoft's recommendation that applications should not block in-progress sleep transitions on any version of Windows.
...
Windows applications can prevent the Display Idle Timeout and the Sleep Idle Timeout by using the API Call SetThreadExecutionState function. SetThreadExecutionState takes a single parameter that indicates the resources to remain available. The Syntax of SetThreadExecutionState is as follows:
Collapse | Copy Code
EXECUTION_STATE WINAPI SetThreadExecutionState( __in EXECUTION_STATE esFlags)
The value of esFlags can be any one or combination of the following:
- ES_AWAYMODE_REQUIRED - Forces away Mode. Away mode should be used only by media-recording and media-distribution applications that must perform critical background processing on desktop computers while the computer appears to be sleeping.
- ES_CONTINUOUS - Tells System that the state being set should remain in effect until the next call to SetThreadExecutionState that uses ES_CONTINUOUS and one of the other state flags is cleared.
- ES_DISPLAY_REQUIRED - Forces the display to be on (display idle timeout is prevented).
- ES_SYSTEM_REQUIRED - Forces the system to be in the working state (sleep idle timeout is prevented).
Maybe this is of any help...
Bart