Recent

Author Topic: VK_SLEEP  (Read 22351 times)

doctorized

  • New Member
  • *
  • Posts: 10
VK_SLEEP
« on: November 10, 2012, 03:05:34 pm »
I try to make an app that will detect Sleep key press without any success. Can anyone provide me a small code and an exe demonstrating me how to do it?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: VK_SLEEP
« Reply #1 on: November 10, 2012, 03:26:52 pm »
Can you handle other VK_*? VK_SLEEP should not be different. Show your current code.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: VK_SLEEP
« Reply #2 on: November 10, 2012, 05:56:18 pm »
My keyboard doesn't even have sleep key. Anyway this can show them all:
Code: [Select]
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  caption:=inttostr(key);
end;

Possible reason your VK_SLEEP doesn't work, is if your keyboard implementation differs from standard.

doctorized

  • New Member
  • *
  • Posts: 10
Re: VK_SLEEP
« Reply #3 on: November 11, 2012, 06:19:15 pm »
Can you handle other VK_*? VK_SLEEP should not be different. Show your current code.
Program test;
uses Crt;

var
  ch : char;
begin
  writeln('Press any key');
  repeat
    ch:=ReadKey;
    writeln(ch)
  until ch=#27 {Esc}
end.


By Pressing the Sleep key no WriteLn is executed.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: VK_SLEEP
« Reply #4 on: November 12, 2012, 12:15:59 am »
Oh my... I thought you were using LCL. In the case of Crt, I have no idea.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: VK_SLEEP
« Reply #5 on: November 12, 2012, 12:29:01 am »
Add LCLType to your uses list. You are also using #27 instead of VK_ESCAPE.

Does this work?
Code: [Select]
  ch:=ReadKey;
  if ch=VK_SLEEP then writeln('sleep pressed');
If doesn't, is it a compiler error (which error?) or nothing happens when you press sleep?
« Last Edit: November 12, 2012, 12:30:59 am by User137 »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8819
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: VK_SLEEP
« Reply #6 on: November 12, 2012, 09:30:43 am »
Quote
Add LCLType to your uses list. You are also using #27 instead of VK_ESCAPE.

Does this work?
It won't work, ReadKey returns Char while VK_* constants are integers. Furthermore, for function/special keys (ESC not included), ReadKey must be called twice in a row, the first one return #0 while the second gives the extended key char.

Bart

  • Hero Member
  • *****
  • Posts: 5575
    • Bart en Mariska's Webstek
Re: VK_SLEEP
« Reply #7 on: November 12, 2012, 04:01:21 pm »
I'll bet you this is not going to work in a console application.
My suspicion is that you need a keyboard hook to intercept the VK_SLEEP (which AFAIK is the power button on the PC).

Did you find any working Delphi examples on the net?

Bart

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: VK_SLEEP
« Reply #8 on: November 12, 2012, 05:57:18 pm »
ReadKey returns Char while VK_* constants are integers.

Next test app:
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
var c: char;
begin
  caption:=inttostr(VK_SLEEP)+' '+inttostr(sizeof(VK_SLEEP));
  // prints 95 1

  c:=char(VK_SLEEP);
end;
VK_SLEEP is typed shortint by compiler messages, hence you can typecast it. Or you can accept that it's #95.

If readkey returns #0, read another character maybe? I don't know about these special keys. As always you can see everything that those functions return. If your test tries to print it out as character, you're propably not going to know what code it is.
writeln(byte(ch));

Again if there was anyone else here that had sleep key (to test how it actually works), this topic might clear out.

doctorized

  • New Member
  • *
  • Posts: 10
Re: VK_SLEEP
« Reply #9 on: November 12, 2012, 09:39:12 pm »
Oh my... I thought you were using LCL. In the case of Crt, I have no idea.
Can anyone help me use LCL?


Add LCLType to your uses list. You are also using #27 instead of VK_ESCAPE.

Does this work?
Code: [Select]
  ch:=ReadKey;
  if ch=VK_SLEEP then writeln('sleep pressed');
If doesn't, is it a compiler error (which error?) or nothing happens when you press sleep?
Yes, it is working fine. I can compile the code and the exe runs as it sould be. The only problem is the Sleep key that is not detected. I press the key, the system reacts, but the console shows nothing.


Next test app:
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
var c: char;
begin
  caption:=inttostr(VK_SLEEP)+' '+inttostr(sizeof(VK_SLEEP));
  // prints 95 1

  c:=char(VK_SLEEP);
end;
I run your code but the caption is 'Form1'.
« Last Edit: November 12, 2012, 09:54:30 pm by doctorized »

doctorized

  • New Member
  • *
  • Posts: 10
Re: VK_SLEEP
« Reply #10 on: November 12, 2012, 09:57:01 pm »
Code: [Select]
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  caption:=inttostr(key);
end;
This code does nothing to me. I press keys but caption is always 'Form1'.

Bart

  • Hero Member
  • *****
  • Posts: 5575
    • Bart en Mariska's Webstek
Re: VK_SLEEP
« Reply #11 on: November 12, 2012, 11:34:54 pm »
Here you can see some example of low level keyboard interaction (the example uses it to block vk_sleep).
As I expected it uses a keyboardhook to get access to it.

ReadKey, OnKeyPress, OnKeyDown will not recieve this key.

Bart

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: VK_SLEEP
« Reply #12 on: November 13, 2012, 07:49:23 am »
This code does nothing to me. I press keys but caption is always 'Form1'.
You need to add the code to TForm's onFormCreate and the other onKeyDown event. When you select form, they are in Events tab in Object Inspector window of Lazarus.

You could have just doubleclicked these events in the list, and then only copy-paste insides of functions i gave. The doubleclick makes empty functions (and function headers which you also need!) ready to add code in.

I should have told it this way:
Code: [Select]
onFormCreate:
var c: char;
begin
  caption:=inttostr(VK_SLEEP)+' '+inttostr(sizeof(VK_SLEEP));
  // prints 95 1

  c:=char(VK_SLEEP);

Code: [Select]
onFormKeyDown:
  caption:=inttostr(key);
« Last Edit: November 13, 2012, 07:53:50 am by User137 »

doctorized

  • New Member
  • *
  • Posts: 10
Re: VK_SLEEP
« Reply #13 on: November 13, 2012, 10:51:24 am »
Code: [Select]
onFormCreate:
var c: char;
begin
  caption:=inttostr(VK_SLEEP)+' '+inttostr(sizeof(VK_SLEEP));
  // prints 95 1

  c:=char(VK_SLEEP);

Code: [Select]
onFormKeyDown:
  caption:=inttostr(key);

I did all these and my code is this:
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, LCLtype;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
var c: char;
begin
  caption:=inttostr(VK_SLEEP)+' '+inttostr(sizeof(VK_SLEEP));
  // prints 95 1

 // c:=char(VK_SLEEP);
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  caption:=inttostr(key);
end;

end.
and I still cannot get VK_SLEEP. I press it but the caption is not changing, the last pressed key is remaining like Sleep is not pressed.

Bart

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

Bart

 

TinyPortal © 2005-2018