11 using the keyboard session 3.2.1. session overview introduce the keyboard device show how keys...
TRANSCRIPT
![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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/15.jpg)
Chapter 3.1: Getting Player Input Using a Gamepad 15
![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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649ece5503460f94bdbcc5/html5/thumbnails/21.jpg)
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