Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 11 Using the Keyboard Session 3.2.1

2 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  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 Keyboard2

3 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  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 Keyboard3

4 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  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 Keyboard4

5 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  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 Keyboard5

6 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  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 Keyboard6 KeyboardState keys;

7 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  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 Keyboard7 KeyboardState keys = Keyboard.GetState();

8 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  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 Keyboard8 if (keys.IsKeyDown(Keys.R)) redIntensity++;

9 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  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 Keyboard9

10 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  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 Keyboard10

11 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  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 Keyboard11 if (keys.IsKeyDown(Keys.Escape)) { this.Exit(); } if (keys.IsKeyDown(Keys.Escape)) { this.Exit(); }

12 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  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 Keyboard12 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++; }

13 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  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 Keyboard13 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; }

14 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  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 Keyboard14 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; }

15 Chapter 3.1: Getting Player Input Using a Gamepad15

16 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  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 Keyboard16

17 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.  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 Keyboard17

18 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.  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 Keyboard18

19 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.  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 Keyboard19

20 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.  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 Keyboard20

21 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.  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 Keyboard21


Download ppt "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."

Similar presentations


Ads by Google