Adding keyboard commands
Once you’ve worked with mouse events, keyboard events are a breeze. In fact, key presses have fewer events associated with them than a mouse, though you can use multiple keys at a time for complex commands. But basically, a key possesses the down and up events. With respect to graphics environments, keys are used to influence what is going on in the scene. The most common keys in games, for example, are the arrow keys or WASD for moving an object and the spacebar for jump or fire. An example of combination keys would be holding down the Shift key while using the arrows to make the object move faster.
In the following exercise, you will program keys to move an object in the 3D environment.
Let’s do it…
To move a 3D object, we can use the Transform
class and modify the position of the model when a key is pressed. To achieve this, follow these steps:
- In the
Transform
class, we will be adding a new function that will move an...