Solved

How to do loop -> until keypressed

Posted on 2004-04-21
3
1,512 Views
Last Modified: 2008-01-16
How to do loop until keypressed ?

repeat
memo1.lines.add('Hello!') ;
until keypresses ; // What should I put to here, I would like to use ESC key.



0
Comment
Question by:hsimola
3 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 50 total points
ID: 10885919
a sample

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    Procedure DoRepeatLoop;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Procedure TForm1.DoRepeatLoop;
var
  keyWasEscape : Boolean;
  KeyBoardState : TKeyBoardState;
begin
  keyWasEscape := false;
  repeat
    //other ops
    memo1.lines.add('In Loop');
    //check
    keyWasEscape := GetAsyncKeyState(VK_ESCAPE) < 0;
  until keyWasEscape;
  memo1.lines.add('Escape-Key was pressed');
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
  DoRepeatLoop;
end;

end.

meikl ;-)
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10885927
Set the Form's property KeyPreview as True (It will allow it to receive Pressed Keys before controlls placed on it):

unit Unit_Q_20963045;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    SpeedButton1: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure SpeedButton1Click(Sender: TObject);
  private{ Private declarations }
    PressedKey: Char;
  public { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  PressedKey := #0;
end;

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  PressedKey := Key;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  try
    repeat
      Memo1.SetFocus;
      Memo1.Lines.add('Hello!') ;
      Application.ProcessMessages;
    until (PressedKey=#27);
  finally
    PressedKey := #0;
  end;
end;

end.

//.............................

object Form1: TForm1
  Left = 216
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  KeyPreview = True
  OldCreateOrder = False
  OnCreate = FormCreate
  OnKeyPress = FormKeyPress
  PixelsPerInch = 96
  TextHeight = 13
  object SpeedButton1: TSpeedButton
    Left = 52
    Top = 76
    Width = 135
    Height = 22
    Caption = 'Check Key Press'
    OnClick = SpeedButton1Click
  end
  object Memo1: TMemo
    Left = 286
    Top = 42
    Width = 255
    Height = 317
    TabOrder = 0
  end
end
0
 
LVL 3

Expert Comment

by:Aliev
ID: 10897320
Most easy and primitive variant:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Timer1: TTimer;
    Button1: TButton;
    procedure Timer1Timer(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure Memo1KeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
Memo1.Lines.Add('Hello!')
end;

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
Timer1.Enabled:=false;
 Button1.Enabled:=true;
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
Timer1.Enabled:=false;
 Button1.Enabled:=true;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled :=true;
 Button1.Enabled:=false;
end;


end.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now