Lazarus
Programming => General => Topic started 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.
-
// 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;
-
:(
not work
my code:
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
-
Add LCLType to your uses section.
-
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
-
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
-
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
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.
?
-
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.
-
error :(
ake.pas(17,15) Error: Forward declaration not solved "TForm1.Image1Click(TObject);"
-
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;
-
Still, it won't assign the event handler. Don't code it by hand, use the Object Inspector->Events->OnKeyDown.
-
thanks :) 1 question more
if I want that image to right so I have change Image1.Top := Image1.Top + 10 to?
-
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;
-
How Do i Tab to Next Section after KeyPress Event
-
if you are referring to normal tab movements...
form1.PreformTab(forward or reverse? use true or false);
-
initialization
{$I ake.lrs}
also 1 error
ake.pas(31,29) Error: method identifier expected
No, wrong. Two errors.
That the way things run:
Winni
-
Thanks Jamie
:)
-
initialization
{$I ake.lrs}
also 1 error
ake.pas(31,29) Error: method identifier expected
No, wrong. Two errors.
That the way things run:
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.