Recent

Author Topic: VK_SLEEP  (Read 22352 times)

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: VK_SLEEP
« Reply #15 on: November 13, 2012, 11:36:01 pm »
Also, sleep action might trigger a OS event. Those can be caught without caring if any key was pressed. I found something about PBT_APMSUSPEND:
Quote
Applications have 2 seconds to finish processing the PBT_APMSUSPEND event before Windows continues with the sleep transition.

doctorized

  • New Member
  • *
  • Posts: 10
Re: VK_SLEEP
« Reply #16 on: November 14, 2012, 11:12:31 am »
This thread is getting silly.

What do you think the "sleep" key is? You never explained this.
I am sorry but this thread is not silly, someone else might be.
You want me explain what? The obvious one? YES, this "sleep" button is the button of the keyboard that puts Windows to "sleep" (a key with a sleeping moon and two small stars on it). I have already the code you posted above but it is not catching the Sleep key. 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?

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: VK_SLEEP
« Reply #17 on: November 14, 2012, 11:43:49 am »
This thread is getting silly.

What do you think the "sleep" key is? You never explained this.

If this "sleep" button is indeed the (power-)button that puts Windows to "sleep" then see my post above: it will not show up in OnKeyDown. You'll need a keyboard hook and I provided a link to an example showing this.

If the "sleep" key issomething else, please describe it,or even better, attach a picture of it.
What do YOU think the sleep key is? Have you never used e.g. a laptop which has a key marked "sleep", or a "multimedia" keybaord that has keys for play, pause, etc and often sleep.
Agreed with doctorized that it's not the thread that is getting silly.

User137 could well be right.
I found http://stackoverflow.com/questions/1391253/intercept-media-keys-on-asus-laptop (after searching for "catching laptop keys C++"). The links posted there may be useful in FPC development as well...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Bart

  • Hero Member
  • *****
  • Posts: 5575
    • Bart en Mariska's Webstek
Re: VK_SLEEP
« Reply #18 on: November 14, 2012, 02:08:36 pm »
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.

Quote
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

doctorized

  • New Member
  • *
  • Posts: 10
Re: VK_SLEEP
« Reply #19 on: November 14, 2012, 03:07:57 pm »
Quote
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.

...
I do not want to deny/abort the suspend operation. I just want to run a small piece of code before pc falls to sleep. I have a code with a timer that can tell me when pc is coming back from sleep but I want my code to run before falling to sleep. It is a small code that wants only some milliseconds to run, nothing more.

Zoran

  • Hero Member
  • *****
  • Posts: 1949
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: VK_SLEEP
« Reply #20 on: November 14, 2012, 03:47:48 pm »
I just want to run a small piece of code before pc falls to sleep.

I believe that PC's going to sleep can be caused not only by pressing this button, but also by other events (for example it can be set to go to sleep after some time without user interaction, like screensavers).

Then perhaps preparing to go to sleep can be caught - take a look at WM_POWERBROADCAST message - http://msdn.microsoft.com/en-us/library/Aa373247
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

Zoran

  • Hero Member
  • *****
  • Posts: 1949
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: VK_SLEEP
« Reply #21 on: November 14, 2012, 03:55:33 pm »
I have also found this article about power states, maybe something helpful can be there...
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: VK_SLEEP
« Reply #22 on: November 14, 2012, 04:26:53 pm »
I do not want to deny/abort the suspend operation. I just want to run a small piece of code before pc falls to sleep.

Actually, that would have been a better question.

And like others have pointed out, in Windows the WM_POWERBROADCAST event would be the proper place to check.
http://msdn.microsoft.com/en-gb/library/windows/desktop/aa373247(v=vs.85).aspx

doctorized

  • New Member
  • *
  • Posts: 10
Re: VK_SLEEP
« Reply #23 on: November 14, 2012, 04:58:31 pm »
I have also found this article about power states, maybe something helpful can be there...
I have read  but I haven't found a way to take current power state.

Then perhaps preparing to go to sleep can be caught - take a look at WM_POWERBROADCAST message - http://msdn.microsoft.com/en-us/library/Aa373247
WM_POWERBROADCAST is not working under Windows 7. :(
« Last Edit: November 14, 2012, 05:00:39 pm by doctorized »

doctorized

  • New Member
  • *
  • Posts: 10
Re: VK_SLEEP
« Reply #24 on: November 15, 2012, 04:20:13 pm »
New data found!!
I found an old keyboard at work that has all three power buttons. By pressing the sleep key, virtual key code 95 is returned correctly. And I am wondering..... why does the new keyboard return no virtual key code and my computer takes the right signal to go to sleep? How can it be done? Is the answer hiding in the fact that the new keyboard is a USB one and the old keyboard is an OS/2 one?

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: VK_SLEEP
« Reply #25 on: November 15, 2012, 06:19:17 pm »
WM_POWERBROADCAST is not working under Windows 7. :(
MSDN doesn't say that it wouldn't work.

You have tested it so you might have source code to show? I don't know much about handling windows messages, but there was something:
http://stackoverflow.com/questions/2048983/problem-with-wm-command-on-lazarus-fpc
Also follows to Lazarus wiki in replies.

You are using normal interface right, no QT or others? Just guessing code would be different to them, again just guessing.

And i think you should focus on the events, not keyboard.
« Last Edit: November 15, 2012, 06:25:46 pm by User137 »

doctorized

  • New Member
  • *
  • Posts: 10
Re: VK_SLEEP
« Reply #26 on: November 15, 2012, 07:05:58 pm »
MSDN doesn't say that it wouldn't work.
You have tested it so you might have source code to show?
I have a source code in an other language, Pure Basic. (I use many languages, basic, c, pascal,....)
Tested there, WM_QUERYENDSESSION and WM_ENDSESSION are working fine but when falling to sleep, no WM_POWERBROADCAST message is sent. If interested, tell me to post the code, you may take some ideas from it even it is an other language, it is easy to understand what it is writen.


Now I did something else. I had an OS/2 to USB adaptor and used it to plug my USB keyboard in the OS/2 port. Then my keyboard hook program took correctly message 95 from the keyboard when I pressed the sleep key. It was just a USB matter. Wondering why....

 

TinyPortal © 2005-2018