Lazarus

Programming => General => Topic started by: DomaZis on March 22, 2011, 06:29:07 pm

Title: Key Press
Post by: DomaZis on March 22, 2011, 06:29:07 pm
Hi, I have one question. I whant make a application, program is when I press up or W key on keyboard the image go up. Can you help me, write his program?

thnaks, and sorry for my bad english.
Title: Re: Key Press
Post by: typo on March 22, 2011, 06:47:34 pm
Code: [Select]
// KeyPreview on
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  );
begin
    if Key =  VK_UP then
      Image1.Top := Image1.Top - 1
    else if Key = VK_DOWN then
      Image1.Top := Image1.Top + 1;
end;
Title: Re: Key Press
Post by: DomaZis on March 22, 2011, 07:05:03 pm
:(

not work

my code:

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Image1: TImage;
    procedure Image1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key =  VK_UP then
      Image1.Top := Image1.Top - 1
    else if Key = VK_DOWN then
      Image1.Top := Image1.Top + 1;
end;

initialization
  {$I unit1.lrs}

end. 

but I get errors:

unit1.pas(31,29) Error: method identifier expected
unit1.pas(33,19) Error: Identifier not found "VK_UP"
unit1.pas(34,13) Error: Identifier not found "Image1"
unit1.pas(34,27) Error: Identifier not found "Image1"
unit1.pas(35,27) Error: Identifier not found "VK_DOWN"
unit1.pas(36,13) Error: Identifier not found "Image1"
unit1.pas(36,27) Error: Identifier not found "Image1"
unit1.pas(44) Fatal: There were 7 errors compiling module, stopping
Title: Re: Key Press
Post by: typo on March 22, 2011, 07:13:51 pm
Add LCLType to your uses section.
Title: Re: Key Press
Post by: DomaZis on March 22, 2011, 07:22:26 pm
Code: [Select]
unit ake;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ExtCtrls, LCLType;

type

  { TForm1 }

  TForm1 = class(TForm)
    Image1: TImage;
    procedure Image1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin

end;

initialization
  {$I ake.lrs}

end.
     

also 1 error
ake.pas(31,29) Error: method identifier expected
Title: Re: Key Press
Post by: fabienwang on March 22, 2011, 07:34:35 pm
TForm1 = class(TForm)
    Image1: TImage;
    procedure Image1Click(Sender: TObject);
  private
...

it should be added in the form declaration like Image1Click

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

Best regards,
Fabien Wang
Title: Re: Key Press
Post by: DomaZis on March 22, 2011, 07:44:24 pm
But I do this,

  { TForm1 }

  TForm1 = class(TForm)
    Image1: TImage;
    procedure Image1Click(Sender: TObject);
  private

    { private declarations }
  public
    { public declarations }
  end;   

can you write goog code and send for me..

this is my code

Code: [Select]
unit ake;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ExtCtrls, LCLType;

type

  { TForm1 }

  TForm1 = class(TForm)
    Image1: TImage;
    procedure Image1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

// KeyPreview on
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
    if Key =  VK_UP then
      Image1.Top := Image1.Top - 1
    else if Key = VK_DOWN then
      Image1.Top := Image1.Top + 1;
end;

initialization
  {$I ake.lrs}

end.   

?
Title: Re: Key Press
Post by: Avishai on March 22, 2011, 07:58:27 pm
unit ake;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ExtCtrls, LCLType;

type

  { TForm1 }

  TForm1 = class(TForm)
    Image1: TImage;
    procedure Image1Click(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

// KeyPreview on
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
    if Key =  VK_UP then
      Image1.Top := Image1.Top - 1
    else if Key = VK_DOWN then
      Image1.Top := Image1.Top + 1;
end;

initialization
  {$I ake.lrs}

end.   
Title: Re: Key Press
Post by: DomaZis on March 22, 2011, 09:30:37 pm
error :(

ake.pas(17,15) Error: Forward declaration not solved "TForm1.Image1Click(TObject);"
Title: Re: Key Press
Post by: Avishai on March 22, 2011, 09:55:17 pm
You need to do a little studying before you start writing code.

  TForm1 = class(TForm)
    Image1: TImage;
    procedure Image1Click(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { private declarations }
  public
    { public declarations }
  end;
Title: Re: Key Press
Post by: Leledumbo on March 22, 2011, 11:56:42 pm
Still, it won't assign the event handler. Don't code it by hand, use the Object Inspector->Events->OnKeyDown.
Title: Re: Key Press
Post by: DomaZis on March 23, 2011, 04:08:12 pm
thanks :) 1 question more

if I want that image to right so I have change Image1.Top := Image1.Top + 10  to?
Title: Re: Key Press
Post by: typo on March 23, 2011, 04:34:57 pm
Code: [Select]
  case Key of
    VK_UP: Image1.Top := Image1.Top - 1;
    VK_DOWN: Image1.Top := Image1.Top + 1;
    VK_LEFT: Image1.Left := Image1.Left - 1;
    VK_RIGHT: Image1.Left := Image1.Left + 1;
  end;   
Title: Re: Key Press
Post by: Weitentaaal on October 15, 2020, 09:56:11 am
How Do i Tab to Next Section after KeyPress Event
Title: Re: Key Press
Post by: jamie on October 15, 2020, 04:53:25 pm
if you are referring to normal tab movements...

form1.PreformTab(forward or reverse? use true or false);
Title: Re: Key Press
Post by: winni on October 15, 2020, 06:11:22 pm
Code: [Select]

initialization
  {$I ake.lrs}


     

also 1 error
ake.pas(31,29) Error: method identifier expected


No, wrong. Two errors.
That the way things run:

Code: Pascal  [Select][+][-]
  1. {$R *.lfm}    
  2.  


Winni
Title: Re: Key Press
Post by: Weitentaaal on October 23, 2020, 10:55:11 am
Thanks Jamie
 :)
Title: Re: Key Press
Post by: wp on October 23, 2020, 11:25:41 am
Code: [Select]

initialization
  {$I ake.lrs}


     

also 1 error
ake.pas(31,29) Error: method identifier expected


No, wrong. Two errors.
That the way things run:

Code: Pascal  [Select][+][-]
  1. {$R *.lfm}    
  2.  
Winni, this is an almost 10 years old thread started at a time when Lazarus had not yet been able to work with .res resources. The syntax to $Include the lrs file in the initialization section is absolutely correct, it still can be used today, but the preferred way, of course, is to link the .res resource by means of the {$R directive, as you show. (https://wiki.lazarus.freepascal.org/Lazarus_Resources#Lazarus_resources).

The reason why the OP of that thread had seen the "method identifer expected" error was that he had added the TForm1.FormKeyDown to the implementation part of the unit, but not to the class declaration in the interface part.

TinyPortal © 2005-2018