Chcemy uzyskać efekt, aby po wciśnieciu jednej ze strzałek na klawiaturze komponent Shape został przesunienty o 10 pixeli w wybraną stronę.
Nazwa |
Klasa |
Shape1 |
TShape |
1) Wstawiamy komponenty, wypisane w powyższej tabeli i zmieniamy im właściwość name na taką jaka jest w kolumnie "Nazwa"
2) Klikamy w pustym miejscu formularza i z zakłądki Events wybieramy zdarzenie OnKeyDown. Wpisujemy poniższy kod:
if Key = VK_LEFT then Shape1.left := Shape1.left - 10;
if Key = VK_RIGHT then Shape1.left := Shape1.left + 10;
if Key = VK_UP then Shape1.top := Shape1.top - 10;
if Key = VK_DOWN then Shape1.top := Shape1.top + 10;
3) Uruchamiamy program.
Kod klawisza: | Nazwa klawisza: |
VK_RETURN | Enter |
VK_SPACE | Spacja |
VK_ESC | Esc |
VK_SHIFT | Shift |
VK_CONTROL | Ctrl |
VK_MENU | Alt |
VK_TAB | Tab |
VK_BACK | Backspace |
VK_INSERT | Insert |
VK_HOME | Home |
VK_PRIOR | Page Up |
VK_DELETE | Delete |
VK_END | End |
VK_NEXT | Page Down |
VK_0 ... VK_9 | 0 - 9 |
VK_NUMPAD0 ... VK_NUMPAD9 | Numeryczne 0 - 9 |
VK_A ... VK_Z | Litery od A do Z |
VK_F1 ... VK_F12 | F1 - F12 |
VK_DIVIDE | Dzielenie |
VK_MULTIPLY | Mnożenie |
VK_SUBTRACT | Odejmowanie |
VK_ADD | Dodawanie |
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls;
type
TForm1 = class(TForm)
Shape1: TShape;
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_LEFT then Shape1.left := Shape1.left - 10;
if Key = VK_RIGHT then Shape1.left := Shape1.left + 10;
if Key = VK_UP then Shape1.top := Shape1.top - 10;
if Key = VK_DOWN then Shape1.top := Shape1.top + 10;
end;
end.