Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
The GetKey function returns an object which can be used to query information about keys. GetKey can be used in situations where the code is written against a general key, or even a general input object, as the return value from GetKey is of type FlatRedBall.Input.KeyReference, which implements FlatRedBall.Input.IPressableInput.
The Keyboard class can be used for text input. Unfortunately, at the time of this writing the keyboard only understands the English keyboard. The GetStringTyped method returns the string that was typed during the last frame. It is possible for multiple keys to be pressed during a frame and the GetStringTyped method also considers whether the Shift key is pressed.
The following code creates a Text object which displays the string typed.
Add the following using statement:
in your Game class scope:
Replace Initialize with the following:
GetStringTyped supports ctrl+c and ctrl+v for copy and paste; however, your game must be set to use [STAThread]. For more information, see this page.
Get2DInput returns an I2DInput instance using the argument keys. This function can be used to abstract input, allowing the same input code to use keyboards and Xbox360GamePad instances. For more information see the page.
The following code can be used to create an I2DInput instance:
The Keyboard class provides functionality for getting input data from the physical keyboard. This class is automatically instantiated by and accessible through the InputManager. The Keyboard class is accessible both on a PC as well as on the Xbox 360.
There are many ways to get data from the keyboard. The following section of code is a series of if-statements which could be used to detect input from the keyboard. Add the following using statements:
In your CustomActivity method:
The Keyboard is commonly used to control objects like in-game characters and the Camera. The following code can be used to quickly control any PositionedObject Add the following using statement:
Assuming positionedObject is a valid PositionedObject (or object inheriting from the PositionedObject class like a Sprite or Camera) add the following code in Update:
For more control over input controlling movement, see the following entry on controlling the camera with the keyboard.
The ControlPositionedObject method is a method which can be used to move a PositionedObject with the arrow keys. this code is rarely used in final games, but can be very useful for learning how to use FlatRedBall and for quick prototypes.
For an example, see this example on the main Keyboard page.
The IgnoreKeyForOneFrame method marks the argument key as being ignored for the rest of the current frame. This is useful if your project has two live objects which are checking for a particular key, but you only want one of them to receive the key press.
In this example we will consider a game screen which uses the Escape key to show a pause menu. Once the pause menu is visible, the Escape key will close it. We will assume that the Pause menu is an instance inside the Screen which has a Visible property (ImplementsIVisible in Glue). Code may look like this:
The code should consume the Escape key by calling IgnoreKeyForOneFrame to prevent both pieces of code from executing in the same frame.
The KeyDown method is a method which returns whether a particular key is being held down. This method will return true if a key is pressed, and will continue to return true until the key has been released. This method can be used to perform every-frame actions such as setting the velocity of a character if the key is pressed.
The following code will move a Sprite to the right if the Right arrow key is down:
The Clear method clears out input from the Keyboard. After Clear is called, all Keyboard methods and properties will behave as if there was no keyboard activity this frame. This function is useful when implementing functionality that should consume input, and is commonly used with the IInputReceiver interface.