Detecting input

To detect if a button is pressed we have to check if the input state is set in the States property of the input.

procedure TGame.Update;begin// Update the device
Device.Update;// Update the timer
Timer.Update;// Update the input
Input.Update;if isLeft in Input.Statesthenbegin// Move the player to the left with a velocity of 200 pixels per second
PlayerPosition.X:= PlayerPosition.X-200* Timer.FrameTime;end;if isRight in Input.Statesthenbegin// Move the player to the left with a velocity of 200 pixels per second
PlayerPosition.X:= PlayerPosition.X+200* Timer.FrameTime;end;end;

It is also possible to check for a specific key even if its not bound to a state.

This code moves the player to the position of the mouse while TAB is pressed.

Modifying the render method

Modify the rendering method to only draw the player.

procedure TGame.Render;begin// Clear the back buffer
Device.Clear;// Draw the image using the image index and pattern index
Images[0].Draw(PlayerPosition.X, PlayerPosition.Y,0);// Flush the canvas
Canvas.Flush;// Flip the front and back buffers to show the scene
Device.Flip;end;

When running the application you should be able to move the player left and right

Detecting input from a single device

Normally when checking if the input has a state we are testing all devices that has that input binding, to check if a single device has a state we have to check that device's state instead

// Detect if the isButton1 state is set for the mouse deviceif isButton1 in Input.Mouse.Statesthenbegin// Do something when the left mouse button is pressedend;

Trigger once

It is also possible to only trigger a action once every time a button is pressed, to do this we just have to remove the wanted input state from the States property of the input or device.

// Only perform a action when a button is pressedif isButton1 in Input.Statesthenbegin// TODO: Fire bullet// Remove the state
Input.States:= Input.States-[isButton1];end;

You can use the same method to remove a input state from a single device

Rebinding keys

To rebind a key we first have to remove all bound keys from the bindings and then add the new ones. The code snippet below shows how that's done

// Remove all bindings from a state
Input.Bindings.Remove(isButton3);// Bind a key to a state
Input.Bindings.Add(isButton3, VK_F2);

It is possible to load and save the input bindings to a text file

// Save the bindings to a file
Input.SaveBindings('input.txt');// Load the bindings from a file
Input.LoadBindings('input.txt');

The file looks like the following, first is the input state then the device and shortcut.