Solved

How to do loop -> until keypressed

Posted on 2004-04-21
3
1,577 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

761 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