?
Solved

How to do loop -> until keypressed

Posted on 2004-04-21
3
Medium Priority
?
1,661 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 200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

770 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