21
1 Using the Keyboard Session 3.2.1

11 Using the Keyboard Session 3.2.1. Session Overview Introduce the keyboard device Show how keys on a keyboard can be represented by enumerated types

Embed Size (px)

Citation preview

Page 1: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

11

Using the KeyboardSession 3.2.1

Page 2: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

Session Overview

Introduce the keyboard device

Show how keys on a keyboard can be represented by enumerated types

Show how an XNA program can use both the keyboard and the gamepad to control game programs

Chapter 3.2.1: Using the Keyboard 2

Page 3: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

XNA and the Keyboard

The Xbox 360 is not supplied with a keyboard

You can connect a keyboard to any of the USB ports on the Xbox itself

A Windows PC already has a keyboard available

The keyboard is used in exactly the same way in an XNA game program for Xbox or Windows PC

It is not possible to use the keyboard on a Zune device

Chapter 3.2.1: Using the Keyboard 3

Page 4: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

Keyboards in XNA Game Programs

The keyboard is used in a very similar way to the gamepad

A game can tell whether or not a particular key is pressed or released

Keys do not generate a character as such, instead a game can get the keyboard status and check the status of particular keys

All the keys on a keyboard can be tested

Shift and control keys can be tested as well

Chapter 3.2.1: Using the Keyboard 4

Page 5: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

The KeyboardState type

The state of the keyboard is represented in XNA by a variable of type KeyboardState

This type provides methods that can be used to find out if particular keys have been pressed

They are used in a slightly different way to the properties of GamePadState

But the underlying principle is the same

Chapter 3.2.1: Using the Keyboard 5

Page 6: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

Creating a KeyboardState Variable

The game will use a variable to hold the state of the keyboard

It will then test the values of the keys in this variable so that the keyboard can be used to control the game

The variable must be declared like any other variable in the game program

It has been given the identifier Keys

Chapter 3.2.1: Using the Keyboard 6

KeyboardState keys;KeyboardState keys;

Page 7: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

Setting the Keys Variable

The Keyboard class is part of XNA and provides a method called getState to read the keyboard state

You do not need to tell getState which keyboard to read as the XNA Framework only supports one keyboard

The getState method delivers a KeyboardState object that holds the state of the keyboard at that instant

Chapter 3.2.1: Using the Keyboard 7

KeyboardState keys = Keyboard.GetState();KeyboardState keys = Keyboard.GetState();

Page 8: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

Testing for a Key Press

The method isKeyDown is told which key is to be tested

The method returns true if the key is pressed

The above code would increase the red intensity if the R key is pressed down

The programmer can identify the key to be tested by using a value from the Keys enumeration

Chapter 3.2.1: Using the Keyboard 8

if (keys.IsKeyDown(Keys.R)) redIntensity++;if (keys.IsKeyDown(Keys.R)) redIntensity++;

Page 9: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

The Keys enumeration

The designers of XNA have created an enumeration which holds values that represent the keys on the keyboard

An enumeration is a type which can hold a particular set of values

They are created for use in specific situations where you only want a variable to hold particular values

Later you will create your own enumerations to manage the state of a game

Chapter 3.2.1: Using the Keyboard 9

Page 10: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

Enumerations and Microsoft Visual Studio

A Visual Studio feature called “Intellisense” can suggest values from an enumeration that can be used

This makes it much easier for the programmer to create correct code

Chapter 3.2.1: Using the Keyboard 10

Page 11: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

Program Exit Using the Escape key

The empty project that XNA creates can only be stopped by pressing the Back button

XNA provides a method called exit to stop the game

The program above calls the exit method on the running game when the Escape key is pressed

This allows the program to be keyboard controlled

Chapter 3.2.1: Using the Keyboard 11

if (keys.IsKeyDown(Keys.Escape)){ this.Exit();}

if (keys.IsKeyDown(Keys.Escape)){ this.Exit();}

Page 12: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

Handling the Keyboard and Gamepad

This code can use either gamepad, Zune, or keyboard input to control the red intensity

Keyboard.GetState() method always returns

If there is no keyboard present it returns a KeyboardState value with no keys pressed

Chapter 3.2.1: Using the Keyboard 12

if (keys.IsKeyDown(Keys.R) || pad1.DPad.Right == ButtonState.Pressed || pad1.Buttons.B == ButtonState.Pressed){ redIntensity++;}

if (keys.IsKeyDown(Keys.R) || pad1.DPad.Right == ButtonState.Pressed || pad1.Buttons.B == ButtonState.Pressed){ redIntensity++;}

Page 13: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

Advanced Logic

This code sets the intensities to 0 if both Shift keys are pressed or both shoulder buttons are pressed on the gamepad

It combines a number of conditions to do this

Chapter 3.2.1: Using the Keyboard 13

if ( (keys.IsKeyDown(Keys.LeftShift) && keys.IsKeyDown(Keys.RightShift)) || (pad1.Buttons.LeftShoulder == ButtonState.Pressed && pad1.Buttons.RightShoulder == ButtonState.Pressed)){ redIntensity = 0; greenIntensity = 0; blueIntensity = 0;}

if ( (keys.IsKeyDown(Keys.LeftShift) && keys.IsKeyDown(Keys.RightShift)) || (pad1.Buttons.LeftShoulder == ButtonState.Pressed && pad1.Buttons.RightShoulder == ButtonState.Pressed)){ redIntensity = 0; greenIntensity = 0; blueIntensity = 0;}

Page 14: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

Advanced Logic

The two conditions are enclosed in brackets to tell the C# compiler they need to be worked out first and then combined using an OR operator

The brackets work as they would in arithmetic

Chapter 3.2.1: Using the Keyboard 14

if ( (keys.IsKeyDown(Keys.LeftShift) && keys.IsKeyDown(Keys.RightShift)) || (pad1.Buttons.LeftShoulder == ButtonState.Pressed && pad1.Buttons.RightShoulder == ButtonState.Pressed) ){ redIntensity = 0; greenIntensity = 0; blueIntensity = 0;}

if ( (keys.IsKeyDown(Keys.LeftShift) && keys.IsKeyDown(Keys.RightShift)) || (pad1.Buttons.LeftShoulder == ButtonState.Pressed && pad1.Buttons.RightShoulder == ButtonState.Pressed) ){ redIntensity = 0; greenIntensity = 0; blueIntensity = 0;}

Page 15: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

Chapter 3.1: Getting Player Input Using a Gamepad 15

Kris Athi
There is no demo slide for the 3.2.1.1 Keyboard Light demo. I have assumed it should go here because of the concepts it is showing
Page 16: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

Summary

The XNA Framework provides a type called KeyboardState to represent the state of the keyboard at a particular instant

The Keyboard class provides getState which returns a KeyboardState value

The getState method is given a value of type Keys to identify the key being tested

A KeyboardState value provides a method called isKeyDown which can check if a particular key is pressed

Chapter 3.2.1: Using the Keyboard 16

Page 17: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

True/False Revision Quiz

An XNA program can handle up to four keyboards.

The KeyboardState type can hold values that represent the state of a keyboard.

The getState method from the Keyboard class returns the state of the keyboard.

The Keys type has a value for every possible key on the keyboard.

Chapter 3.2.1: Using the Keyboard 17

Page 18: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

True/False Revision Quiz

An XNA program can handle up to four keyboards.

The KeyboardState type can hold values that represent the state of a keyboard.

The getState method from the Keyboard class returns the state of the keyboard.

The Keys type has a value for every possible key on the keyboard.

Chapter 3.2.1: Using the Keyboard 18

Page 19: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

True/False Revision Quiz

An XNA program can handle up to four keyboards

The KeyboardState type can hold values that represent the state of a keyboard.

The getState method from the Keyboard class returns the state of the keyboard.

The Keys type has a value for every possible key on the keyboard.

Chapter 3.2.1: Using the Keyboard 19

Page 20: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

True/False Revision Quiz

An XNA program can handle up to four keyboards

The KeyboardState type can hold values that represent the state of a keyboard.

The getState method from the Keyboard class returns the state of the keyboard.

The Keys type has a value for every possible key on the keyboard.

Chapter 3.2.1: Using the Keyboard 20

Page 21: 11 Using the Keyboard Session 3.2.1. Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types

True/False Revision Quiz

An XNA program can handle up to four keyboards

The KeyboardState type can hold values that represent the state of a keyboard.

The getState method from the Keyboard class returns the state of the keyboard.

The Keys type has a value for every possible key on the keyboard.

Chapter 3.2.1: Using the Keyboard 21