Download presentation
Presentation is loading. Please wait.
Published byMorgan Park Modified over 9 years ago
1
11 Using the Keyboard in XNA Session 9.1
2
Session Overview Discover more detail on how the XNA keyboard is implemented Find out how to use arrays to find out which keys have been pressed on the keyboard See how custom type has been created in XNA to represent key presses and how to use these in your programs Start to create a message board program Discover more detail on how the XNA keyboard is implemented Find out how to use arrays to find out which keys have been pressed on the keyboard See how custom type has been created in XNA to represent key presses and how to use these in your programs Start to create a message board program Chapter 9.1: Using the Keyboard in XNA2
3
Game Idea: “Message Board” We know how to create good looking text We can also make animated backgrounds and display a clock We can combine all these abilities to create a message board First we need to read the keyboard We know how to create good looking text We can also make animated backgrounds and display a clock We can combine all these abilities to create a message board First we need to read the keyboard Chapter 9.1: Using the Keyboard in XNA3
4
Reading the Keyboard We have already used the keyboard in our XNA programs We used it to make a keyboard version of “Color Nerve” If the R key is pressed the red intensity value will be increased We have already used the keyboard in our XNA programs We used it to make a keyboard version of “Color Nerve” If the R key is pressed the red intensity value will be increased Chapter 9.1: Using the Keyboard in XNA4 if (pad1.Buttons.B == ButtonState.Pressed || keys.IsKeyDown(Keys.R)) { redIntensity++; }
5
Levels and Edges This kind of input is managed by level Every time we test the key we will find that it is down If we just tested for keys in this way, we would have an “auto repeat” that worked 60 times a second This kind of input is managed by level Every time we test the key we will find that it is down If we just tested for keys in this way, we would have an “auto repeat” that worked 60 times a second Chapter 9.1: Using the Keyboard in XNA5 if (pad1.Buttons.B == ButtonState.Pressed || keys.IsKeyDown(Keys.R)) { redIntensity++; }
6
Detecting that a Key Has Been Pressed To detect a key being pressed we have to do what we did before with the Button Bash game We have to compare the current state of the key with the previous state A changes in the two states will mean that a key has been pressed or released To detect a key being pressed we have to do what we did before with the Button Bash game We have to compare the current state of the key with the previous state A changes in the two states will mean that a key has been pressed or released Chapter 9.1: Using the Keyboard in XNA6 if (keyState.IsKeyDown(Keys.R) && oldKeyState.IsKeyUp(Keys.R)) { // if we get here the key R has just been pressed } if (keyState.IsKeyDown(Keys.R) && oldKeyState.IsKeyUp(Keys.R)) { // if we get here the key R has just been pressed }
7
Detecting All the Keys To detect all the keys on a keyboard we would have to repeat this process for every single key This is possible, but would be very tedious Fortunately XNA provides an easier way of finding out what keys have been pressed, using arrays To detect all the keys on a keyboard we would have to repeat this process for every single key This is possible, but would be very tedious Fortunately XNA provides an easier way of finding out what keys have been pressed, using arrays Chapter 9.1: Using the Keyboard in XNA7
8
Finding Out the State of the Keyboard To get hold of the state of the keyboard we can use the GetState method This returns a value of type KeyboardState which describes the state of the keyboard at the time of the call To get hold of the state of the keyboard we can use the GetState method This returns a value of type KeyboardState which describes the state of the keyboard at the time of the call Chapter 9.1: Using the Keyboard in XNA8 KeyboardState keyState = Keyboard.GetState(); Keys[] pressedKeys; pressedKeys = keyState.GetPressedKeys(); KeyboardState keyState = Keyboard.GetState(); Keys[] pressedKeys; pressedKeys = keyState.GetPressedKeys();
9
Discovering Which Keys Are Currently Pressed The GetPressedKeys method can be called on a KeyboardState value The method returns an array which holds a list of keys that are pressed down In the code above, the array pressedKeys is set to the value returned by GetPressedKeys The GetPressedKeys method can be called on a KeyboardState value The method returns an array which holds a list of keys that are pressed down In the code above, the array pressedKeys is set to the value returned by GetPressedKeys Chapter 9.1: Using the Keyboard in XNA9 KeyboardState keyState = Keyboard.GetState(); Keys[] pressedKeys; pressedKeys = keyState.GetPressedKeys(); KeyboardState keyState = Keyboard.GetState(); Keys[] pressedKeys; pressedKeys = keyState.GetPressedKeys();
10
Creating a String of Key Names This for loop assembles a list of the keys that are presently pressed and puts their descriptions into a string variable called messageString The string can be displayed by the Draw method It does this by working through the array of pressed keys This for loop assembles a list of the keys that are presently pressed and puts their descriptions into a string variable called messageString The string can be displayed by the Draw method It does this by working through the array of pressed keys Chapter 9.1: Using the Keyboard in XNA10 messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; } messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; }
11
Creating an Empty String The messageString will hold a string of text that describes each of the keys that has been pressed It must be cleared to an empty string at the start This assignment sets messageString to an empty string The messageString will hold a string of text that describes each of the keys that has been pressed It must be cleared to an empty string at the start This assignment sets messageString to an empty string Chapter 9.1: Using the Keyboard in XNA11 messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; } messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; }
12
Creating a for Loop to Work Down the Array The for loop needs to know the size of the pressedKeys array All arrays provide a Length property which returns an integer giving the number of elements in the array This is used to control the for loop The for loop needs to know the size of the pressedKeys array All arrays provide a Length property which returns an integer giving the number of elements in the array This is used to control the for loop Chapter 9.1: Using the Keyboard in XNA12 messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; } messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; }
13
Assembling the Message String We have used the + operator between numbers to perform addition When + is used between strings it actually puts one string on the end of the other This statement adds a key description onto the end of the string, along with a separator space We have used the + operator between numbers to perform addition When + is used between strings it actually puts one string on the end of the other This statement adds a key description onto the end of the string, along with a separator space Chapter 9.1: Using the Keyboard in XNA13 messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; } messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; }
14
Getting the Key Description You can ask any object in a program to provide a string description of itself You do this by calling the ToString method on that object We are asking each key to describe itself You can ask any object in a program to provide a string description of itself You do this by calling the ToString method on that object We are asking each key to describe itself Chapter 9.1: Using the Keyboard in XNA14 messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; } messageString = ""; for (int i = 0; i < pressedKeys.Length; i++) { messageString = messageString + pressedKeys[i].ToString() + " " ; }
15
Displaying the Message The Draw method just displays the string messageString and messageVector are variables in the game world The Draw method just displays the string messageString and messageVector are variables in the game world Chapter 9.1: Using the Keyboard in XNA15 protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.DrawString(font, messageString, messageVector, Color.White); spriteBatch.End(); base.Draw(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.DrawString(font, messageString, messageVector, Color.White); spriteBatch.End(); base.Draw(gameTime); }
16
1. Key Viewer Chapter 9.1: Using the Keyboard in XNA16 This program displays a list of keys that are pressed Note that each key is uniquely identified, including the left and right shift keys This program displays a list of keys that are pressed Note that each key is uniquely identified, including the left and right shift keys
17
Representing Keys in XNA The designers of XNA needed something to represent keys on the keyboard Some of the keys were printable characters, others were control keys such as shift and escape They wanted users of the keyboard to be able to work with any key value that could be produced To solve this problem they created an enumerated type called Keys The designers of XNA needed something to represent keys on the keyboard Some of the keys were printable characters, others were control keys such as shift and escape They wanted users of the keyboard to be able to work with any key value that could be produced To solve this problem they created an enumerated type called Keys Chapter 9.1: Using the Keyboard in XNA17
18
Enumerated Types and the Keys Type The word “enumerate” means to count In C# an enumerated type is one that can hold a particular number of values In the case of the keyboard, the XNA designers wanted to hold values for every available key, but no more than that They therefore created an enumerated type called Keys The word “enumerate” means to count In C# an enumerated type is one that can hold a particular number of values In the case of the keyboard, the XNA designers wanted to hold values for every available key, but no more than that They therefore created an enumerated type called Keys Chapter 9.1: Using the Keyboard in XNA18
19
Why Make an Enumerated Type? There is no need to use an enumerated type The XNA designers could just have assigned some values for the keys: 0 means left shift 1 means right shift 2 means the A key However programmers could then use meaningless values, which would do strange things An attempt to use key number 1001 might break the program There is no need to use an enumerated type The XNA designers could just have assigned some values for the keys: 0 means left shift 1 means right shift 2 means the A key However programmers could then use meaningless values, which would do strange things An attempt to use key number 1001 might break the program Chapter 9.1: Using the Keyboard in XNA19
20
Creating Your Own Enumerated Types Later in the course you will create your own enumerated types You can regard these as variable types where you get to choose the values which are allowed They are used a lot in situations where items must be in one of a number of different states: Bank account: open, suspended, closed Video game: attract mode, playing, showing high score Person: happy, sad, angry, upset Later in the course you will create your own enumerated types You can regard these as variable types where you get to choose the values which are allowed They are used a lot in situations where items must be in one of a number of different states: Bank account: open, suspended, closed Video game: attract mode, playing, showing high score Person: happy, sad, angry, upset Chapter 9.1: Using the Keyboard in XNA20
21
Enumerated Types and Intellisense Since Microsoft Visual Studio can find out what values an enumerated type can take, it can show you these automatically This is an example of the way that the development tool can work with the language that you are using Since Microsoft Visual Studio can find out what values an enumerated type can take, it can show you these automatically This is an example of the way that the development tool can work with the language that you are using Chapter 9.1: Using the Keyboard in XNA21
22
Summary The keyboard is used in a very similar way to the gamepad, with a GetState method returning an object that describes the state of the keyboard Your code can ask for the state of an individual key, or it can ask for an array that provides a list of all the keys that are pressed down To detect a key being pressed an XNA game must use edge detection An enumerated type has a specific set of values The keyboard is used in a very similar way to the gamepad, with a GetState method returning an object that describes the state of the keyboard Your code can ask for the state of an individual key, or it can ask for an array that provides a list of all the keys that are pressed down To detect a key being pressed an XNA game must use edge detection An enumerated type has a specific set of values Chapter 9.1: Using the Keyboard in XNA22
23
True/False Revision Quiz An XNA program uses a GetState method to read the status of a keyboard. A keyboard can only detect a single key press at a time. The Length property can be used to determine how many elements an array contains. An enumerated type has a particular set of allowed values. An XNA program uses a GetState method to read the status of a keyboard. A keyboard can only detect a single key press at a time. The Length property can be used to determine how many elements an array contains. An enumerated type has a particular set of allowed values. Chapter 9.1: Using the Keyboard in XNA23
24
True/False Revision Quiz An XNA program uses a GetState method to read the status of a keyboard. A keyboard can only detect a single key press at a time. The Length property can be used to determine how many elements an array contains. An enumerated type has a particular set of allowed values. An XNA program uses a GetState method to read the status of a keyboard. A keyboard can only detect a single key press at a time. The Length property can be used to determine how many elements an array contains. An enumerated type has a particular set of allowed values. Chapter 9.1: Using the Keyboard in XNA24
25
True/False Revision Quiz An XNA program uses a GetState method to read the status of a keyboard. A keyboard can only detect a single key press at a time. The Length property can be used to determine how many elements an array contains. An enumerated type has a particular set of allowed values. An XNA program uses a GetState method to read the status of a keyboard. A keyboard can only detect a single key press at a time. The Length property can be used to determine how many elements an array contains. An enumerated type has a particular set of allowed values. Chapter 9.1: Using the Keyboard in XNA25
26
True/False Revision Quiz An XNA program uses a GetState method to read the status of a keyboard. A keyboard can only detect a single key press at a time. The Length property can be used to determine how many elements an array contains. An enumerated type has a particular set of allowed values. An XNA program uses a GetState method to read the status of a keyboard. A keyboard can only detect a single key press at a time. The Length property can be used to determine how many elements an array contains. An enumerated type has a particular set of allowed values. Chapter 9.1: Using the Keyboard in XNA26
27
True/False Revision Quiz An XNA program uses a GetState method to read the status of a keyboard. A keyboard can only detect a single key press at a time. The Length property can be used to determine how many elements an array contains. An enumerated type has a particular set of allowed values. An XNA program uses a GetState method to read the status of a keyboard. A keyboard can only detect a single key press at a time. The Length property can be used to determine how many elements an array contains. An enumerated type has a particular set of allowed values. Chapter 9.1: Using the Keyboard in XNA27
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.