Z
Z
ZIK13372019-03-24 15:13:11
Delphi
ZIK1337, 2019-03-24 15:13:11

How to register user's keystrokes?

How to log user keystrokes?
The bottom line is that you need to press the key, the symbol of which falls from top to bottom in the application:

spoiler
tGsNMG2.jpg

Unit_4 code:
spoiler
unit Unit4;

interface

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

type
  TForm1 = class(TForm)
    RadioGroup1: TRadioGroup;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    RadioGroup2: TRadioGroup;
    RadioButton4: TRadioButton;
    RadioButton5: TRadioButton;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    TrackBar1: TTrackBar;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    PaintBox1: TPaintBox;
    PaintBox2: TPaintBox;
    PaintBox3: TPaintBox;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure TrackBar1Change(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);

  private
    { Private declarations }
  public
    Thread: array[1..3] of TMoveThread;
  end;

var
  Form1: TForm1;
  level: integer;
  presskey: boolean;

implementation
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  RadioButton5.Checked:=true;
  thread[1]:=TMoveThread.Create(PaintBox1);//Создается поток
  Thread[1].Priority:=tpLowest;  //Устанавливается небольшой приоритет у потока, чтобы поток был управляемым и не забирал все ресурсы.
  thread[2]:=TMoveThread.Create(PaintBox2);//Создается поток
  Thread[2].Priority:=tpLowest;  //Устанавливается небольшой приоритет у потока, чтобы поток был управляемым и не забирал все ресурсы.
  thread[3]:=TMoveThread.Create(PaintBox3);//Создается поток
  Thread[3].Priority:=tpLowest;  //Устанавливается небольшой приоритет у потока, чтобы поток был управляемым и не забирал все ресурсы.
  RadioButton2.Checked:=true;
  presskey:=false;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  x:integer;
begin
  RadioButton1.Enabled:=False;
  RadioButton2.Enabled:=False;
  RadioButton3.Enabled:=False;
  RadioButton4.Enabled:=False;
  RadioButton5.Enabled:=False;
  Button1.Enabled:=false;
  TrackBar1.enabled:=false;
  if RadioButton3.checked=true then level:=3;
  if RadioButton2.checked=true then level:=2;
  if RadioButton1.checked=true then level:=1;
  for x:=1 to level do
    begin
      thread[x].DoVisualSwap;
      Thread[x].resume;
      presskey:=true;
    end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  x:integer;
begin
  RadioButton1.Enabled:=true;
  RadioButton2.Enabled:=true;
  RadioButton3.Enabled:=true;
  RadioButton4.Enabled:=true;
  RadioButton5.Enabled:=true;
  Button1.Enabled:=true;
  TrackBar1.enabled:=true;
  presskey:=false;
  for x:=1 to level do
  begin
      PaintBox1.Refresh;
      PaintBox2.Refresh;
      PaintBox3.Refresh;
      Thread[x].er:=0;
      edit1.Text:=inttostr(0);
      thread[x].terminate;
      thread[x].DoVisualSwap;
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
   Close;
end;

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
var
  x:integer;
begin
if presskey=true then
  begin
    for x:= 1 to level do
      if thread[x].b0 = Key then
      begin
        thread[x].DoVisualSwap;
        PaintBox1.Refresh;
        PaintBox2.Refresh;
        PaintBox3.Refresh;
        exit;
      end;
    thread[1].er:=(thread[1].er)+1;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  x:integer;
begin
for x:=1 to level do
begin
  if presskey=true then
    begin
      thread[x].Execute;
      thread[x].go:=true;
      edit1.Text:=inttostr((thread[1].er)+(thread[2].er)+(thread[3].er));
    end;
end;
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
begin
  Timer1.Interval := 70 - TrackBar1.Position*20;
end;

end.

Unit4_1 code:
spoiler
unit Unit4_1;

interface

uses
  Classes, Graphics, ExtCtrls;

type
  TMoveThread = class(TThread)
  private

  protected

  public
    FBox: TPaintBox;        // Где воспроизводится картинка
    i:integer;              // Для определения координат картинки
    b0: char;
    go: boolean;
    er: integer;
    procedure Execute; override;
    procedure DoVisualSwap;
    constructor Create(c:TPaintBox);
    destructor Destroy; override;

  end;

implementation
{ TMoveThread }

uses Unit4;

constructor TMoveThread.Create(c:TPaintBox);
begin
  Fbox:=c;go:=false;er:=0;
  inherited Create(False);
end;

procedure TMoveThread.DoVisualSwap;
begin
  i:=0;
  if form1.RadioButton5.Checked=true then b0:=chr(random(26)+97)
  else b0:=chr(random(32)+224);
end;

procedure TMoveThread.Execute;
begin
    if go=true then
      begin
        FBox.Canvas.textout(20,i,b0);
        i:=i+1;
        go:=false;
      end;
    while i>(fbox.Height) do
      begin
        er:=er+1;
        synchronize(DoVisualSwap);
      end;
end;

destructor TMoveThread.Destroy;
begin
  b0:=#0;
  inherited Destroy;
end;
end.

Application screenshot:
spoiler
n5i6VcI.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2019-03-24
@tsklab

Key:
Define:Vcl.Forms.TForm.OnKeyDown

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (Key=VK_ESCAPE) and Printer.Printing then
  begin
  Printer.Abort;
  MessageDlg('Printing aborted', mtInformation, [mbOK],0);
  end;
end;

Define Vcl.Forms.TForm.OnKeyPress.
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  MessageDlg(Key + ' has been pressed', mtInformation, [mbOK], 0)
end;

Add Application.ProcessMessages, for example, before redrawing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question