How to make the Image1 object move through the form to the right by pressing the D button. Also to keep this button the object moved, releasing stopped. Please give the answer normal, with the code. I am a beginner and do not understand something like "use that property."

    4 answers 4

    1. Make sure that the form properties are displayed in the Object Inspector.
    2. In the Property inspector, click the Events tab and find the OnKeyDown event OnKeyDown .
    3. Double click on the text field in this line. Delphi will generate an event handler code.
    4. Paste this fragment as a handler:

       procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = Ord('D') then Shape1.Left := Shape1.Left + 1; end; 
    • one
      That's just, it seems to me, that KeyPreview will have to: = true to make the form the first to receive a key-press signal, and not some component on the form. - pincher1519
    • If I am not mistaken (too lazy to check), then it will only work the first time, for further movement you need to release the button. That does not meet the condition that the object was moving while you hold the button - vdk company
    • > (too lazy to check) Before posting, I certainly check the code. Everything's great moving. - Nofate
    • Yes you are right, and simultaneous keystroke handles? - vdk company

    My version is the following: A timer was thrown on the form and image (the picture of which you need to manage), double-click on the timer to open the code to write the event.

    we insert into it

      procedure TForm1.Timer1Timer(Sender: TObject); begin if (GetKeyState (ord('D')) <0) or (GetKeyState (ord('d')) <0) then Image1.left:=Image1.left+1; if (GetKeyState (ord('S')) <0) or (GetKeyState (ord('s')) <0) then Image1.left:=Image1.left-1; if (GetKeyState (ord('E')) <0) or (GetKeyState (ord('e')) <0) then Image1.Top:=Image1.Top-1; if (GetKeyState (ord('X')) <0) or (GetKeyState (ord('x')) <0) then Image1.Top:=Image1.Top+1; image1.Repaint; end; 

    We get control of the picture with the SDEX keys. Moreover, you can clamp 2 keys at the same time and we will move diagonally.

      We throw the ApplicationEvents component on the form (the Additional tab), in the OnMessage event handler we write:

       if Msg.message = WM_KEYDOWN then case Msg.wParam of 65: Image1.Left:=Image1.Left -1; 68: Image1.Left:=Image1.Left +1; 87: -1; 83: +1; end; 

        From myself I will add only that in the very same events you need to find Form.Create , and inside to write

         Form1.DoubleBuffered := True; 

        Then the picture also will not flash at offset.