Recent

Author Topic: Key Press  (Read 25695 times)

DomaZis

  • New member
  • *
  • Posts: 7
Key Press
« 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.
« Last Edit: March 22, 2011, 06:33:55 pm by DomaZis »

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Key Press
« Reply #1 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;

DomaZis

  • New member
  • *
  • Posts: 7
Re: Key Press
« Reply #2 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

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Key Press
« Reply #3 on: March 22, 2011, 07:13:51 pm »
Add LCLType to your uses section.

DomaZis

  • New member
  • *
  • Posts: 7
Re: Key Press
« Reply #4 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

fabienwang

  • Sr. Member
  • ****
  • Posts: 449
  • Lazarus is the best
    • My blog
Re: Key Press
« Reply #5 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
I'm using Arch Linux.
Known for: CPickSniff, OpenGrabby
Contributed to: LazPaint

DomaZis

  • New member
  • *
  • Posts: 7
Re: Key Press
« Reply #6 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.   

?

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Key Press
« Reply #7 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.   
Lazarus Trunk / fpc 2.6.2 / Win32

DomaZis

  • New member
  • *
  • Posts: 7
Re: Key Press
« Reply #8 on: March 22, 2011, 09:30:37 pm »
error :(

ake.pas(17,15) Error: Forward declaration not solved "TForm1.Image1Click(TObject);"

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Key Press
« Reply #9 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;
Lazarus Trunk / fpc 2.6.2 / Win32

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Key Press
« Reply #10 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.

DomaZis

  • New member
  • *
  • Posts: 7
Re: Key Press
« Reply #11 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?
« Last Edit: March 23, 2011, 04:10:28 pm by DomaZis »

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Key Press
« Reply #12 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;   
« Last Edit: March 23, 2011, 04:40:28 pm by typo »

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: Key Press
« Reply #13 on: October 15, 2020, 09:56:11 am »
How Do i Tab to Next Section after KeyPress Event
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Key Press
« Reply #14 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);
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018