Get Notified when a KeyStroke is Released ?

delphi Viet Nam
  • 15 years ago
    Hi all,
    As you may know, the GetAsyncKeyState function returns a value indicating that a specified key is pressed or not.

    I have written this code to recognize that:

    procedure TForm1.Timer1Timer(Sender: TObject);
    var KeyStateHORT;
     n1:Integer;
     nStart,nEnd:Integer;
     Ch:Char;
    const PRESSED_KEY = 128;
    begin
     nStart:=Ord('A');
     nEnd:=Ord('Z');
     for n1:=nStart to nEnd do
     begin
       KeyState:=GetAsyncKeyState(n1);
       Ch:=Chr(n1);
       if KeyState <> 0 then
       begin
         {Do the Jobs Here}
       end;
     end;
    end;

    This code works, but I want to determine when a key is released ?
    This happens both when a key is pressed and when released.
    But I want to distinguish them

    (basse dige baba mordam enghad tozih dadam ) )

    Ok thanks Guys for reply. :X
  • 15 years ago
    There are a few ways of detecting keypresses in Delphi.

    "OnKeyPress"

       FormKeyPress(Sender: TObject; var Key: Char);

    This event passes the actual ASCII code in the variable "Key".

    "OnKeyDown"

      FormKeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState);

    This event passes the windows VK keycode in the variable "Key"

    "OnKeyup"

      FormKeyUp(Sender: TObject; var Key: Word;  Shift: TShiftState);

    This event passes the windows VK keycode in the variable "Key"

    You maybe able to replace your Timer event with the "OnKeyUp" method wich also includes the state of the other keys (shift, ctrl alt) in "Shift".

    For instance

    Code:

    Procedure  Form1.FormKeyUp(Sender: TObject; var Key: Word;  Shift: TShiftState);
    begin
       Case Key of
        VK_0 .. VK_9 : ; // Number Keys
        VK_A .. VK_Z : ; // Aplha Keys
       end;
    end;


    For more information on these events see the Delphi help.

    Dave

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Nine people can't make a baby in a month.” - Fred Brooks