Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   mover image con teclado (https://www.clubdelphi.com/foros/showthread.php?t=30542)

edusus 09-04-2006 20:59:03

mover image con teclado
 
Como puedo hacer para mover un control image con el teclado, usando las teclas de arriba, abajo, izquierda y derecha.
He probado con:
if key=vk_down then ...;, pero no funciona.
Gracias por adelantado.

Sotrono 09-04-2006 21:20:42

Hola, de que componente estas usando el evento OnKeyDown u OnKeyUp??
Tenes activada la propiedad KeyPreview del Form??

Esto funciona:
Código Delphi [-]
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = vk_right then
    Image1.Left := Image1.Left + 20;
end;

Bytes...

seoane 09-04-2006 21:31:06

Si estas programando algun tipo de juego y necesitas, ademas de los movimientos de arriba, abajo, izquierda y derecha, movimientos en diagonal puedes usar algo como esto:

Código Delphi [-]
  // Coloca esto dentro de un timer con un intervalo, por ejemplo, de 100 milisegundos
  if (GetKeyState(VK_LEFT) and $80) > 0 then
    Imagen.Left:= Imagen.Left - 10;
  if (GetKeyState(VK_RIGHT) and $80) > 0 then
    Imagen.Left:= Imagen.Left + 10;
  if (GetKeyState(VK_UP) and $80) > 0 then
    Imagen.Top:= Imagen.Top - 10;
  if (GetKeyState(VK_DOWN) and $80) > 0 then
    Imagen.Top:= Imagen.Top + 10;

dec 09-04-2006 22:55:29

Hola,

Me váis a perdonar la tontería, al Hilo de lo que habéis propuesto:

Código Delphi [-]
program PelotaLoca;

{$APPTYPE CONSOLE}

uses
  Forms,
  Windows,
  Classes,
  SysUtils,
  Controls,
  ExtCtrls,
  Graphics;

const
  MOVIMIENTO = 15; { Cantidad de movimiento }

var
  FForm  : TForm;
  FShape : TShape;

type
  TAuxiliar = class
  private
    FTimer: TTimer;
    procedure TimerTick(Sender: TObject);
    procedure KeyDownEvent(Sender: TObject;
               var Key: Word; Shift: TShiftState);
  public
    constructor Create;
    destructor Destroy; override;
  end;

{ TAuxiliar }

constructor TAuxiliar.Create;
begin
  FTimer := TTimer.Create(nil);
  FTimer.Enabled  := true;
  FTimer.Interval := 100;
  FTimer.OnTimer  := TimerTick;
end;

destructor TAuxiliar.Destroy;
begin
  FTimer.Free;
  inherited Destroy;
end;

procedure TAuxiliar.KeyDownEvent(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  with FShape do case Key of
    VK_UP:    Top  := Top  - MOVIMIENTO;
    VK_RIGHT: Left := Left + MOVIMIENTO;
    VK_DOWN:  Top  := Top  + MOVIMIENTO;
    VK_LEFT:  Left := Left - MOVIMIENTO;
  end;
end;

procedure TAuxiliar.TimerTick(Sender: TObject);
resourcestring
  rsCaption = 'Top: %d - Left: %d';
begin
  with FShape do
  begin
    FForm.Caption := Format(rsCaption, [Top, Left]);

    if (Left > FForm.Width)  then Left := 2;
    if (Top  > FForm.Height) then Top  := 2;

    if (Left < 0) then Left := (FForm.Width  - Width);
    if (Top  < 0) then Top  := (FForm.Height - Height);

    if (GetKeyState(VK_UP)    and $4000) > 0 then Top  := Top  - MOVIMIENTO;
    if (GetKeyState(VK_DOWN)  and $4000) > 0 then Top  := Top  + MOVIMIENTO;
    if (GetKeyState(VK_LEFT)  and $4000) > 0 then Left := Left - MOVIMIENTO;
    if (GetKeyState(VK_RIGHT) and $4000) > 0 then Left := Left + MOVIMIENTO;
  end;
end;

procedure Inicializar();
var
  FAuxilar: TAuxiliar;
begin
  FAuxilar := TAuxiliar.Create;
  FForm    := TForm.Create(nil);
  FShape   := TShape.Create(FForm);

  with FForm do
  begin
    Width       := 400;
    Height      := 300;
    Color       := clWhite;
    KeyPreview  := true;
    BorderStyle := bsDialog;
    Position    := poDesktopCenter;
    OnKeyDown   := FAuxilar.KeyDownEvent;
  end;

  with FShape do
  begin
    Width       := 20;
    Height      := 20;
    Parent      := FForm;
    Brush.Color := clRed;
    Pen.Color   := clWhite;
    Shape       := stCircle;
    Top         := MOVIMIENTO;
    Left        := MOVIMIENTO;
  end;

  try
    FForm.ShowModal;
  finally
    FForm.Free;
    FAuxilar.Free;
  end;
end;

begin { application }

  Inicializar();

end.

edusus 09-04-2006 23:05:19

Funciona !!!
 
Muchas gracias por la explicación. Ha funcionado.


La franja horaria es GMT +2. Ahora son las 16:40:01.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi