Handling User Input in C#

Jonathan Weinberger
A free video tutorial from Jonathan Weinberger
Authorized Unity Instructor
4.5 instructor rating • 10 courses • 95,415 students

Learn more from the full course

The Unity C# Survival Guide

Created in partnership with Unity Technologies: Master C# with Unity in this Complete Guide!

12:21:51 of on-demand video • Updated March 2019

  • Master C# with Unity
  • Beginner C# concepts (variables, if-statements, methods, etc.)
  • Intermediate C# Concepts (lists, dictionaries, abstract classes, etc)
  • Advanced C# Concepts (delegates, events, lambda expressions, design patterns, etc)
  • Interview Prep
  • Unity quick tips and challenges
  • How to program from scratch
English [Auto] And this quick tip we're gonna take a look at how to check for keyboard input. Let's create a new C shape scripts. Call it Main and attach it to your main camera upon opening this. Where do we check for user input. All user input needs to go inside the Update method. You need something that runs continuously checking for if a user hits. For example the space key if I wanted to check for the space key I can write out some pseudocode here that says if space key was pressed down. Then what do we want to do. Let's print a message to convert this. We can use an if statement and we can use the input class once we've done that. We can say input dot get key down and that's going to allow us to grab a key when we press it down get key means that you're holding the key down and get key up is checking for when you lift the key up. So if I only want to check when I press the space key down I'm going to use get key down when I open a parentheses. It's going to ask for a key code by typing in a key code dot operator. You'll see here that we have a list of every key that's on our keyboard as well as some joystick options. I can search for space in once found I can then close this off. So what we have here is if the user hits the space key then what do we want to do. We can say here debug that log space key let's save this hop into unity and you'll see here that when I hit the space key we have a message print out that says space key. Let's say I want to get the key and while I'm holding it down I want it to print out the message. Well here we can use an if statement and let's have some pseudocode. If e space will say if e key held down print message. So here we're going to access the input class. We're going to get key which is going to check if it's held down in the key code that I'm looking for is going to be key code dot e. Once I found that we can close it off and we'll say debug that log we'll say holding E.. Let's save this up into unity let it compile and test it out you'll see here that in the console if I hold down the key let's move the console up here to see this you'll see here that it's at ninety one and as I hold it down it's continuously getting called. So it only checks for the key when it's held down. Last but not least if I want to check for the f key. So here if f key is lifted up we'll say print f then what we can do is use the if statement. We can check for that key using if we hit the f key so input I get keyed up. So that means that we're lifting our finger off the key and the key code that we want to map that too is going to be key code dot f. Once we do that we can then print out this message debug that log key save this and now that's only gonna get called when you actually lift your key or you lift your finger off the FCC. So here we'll run the application and you'll see here that if I hit the FCC nothing happens I'm holding it down if I lift up it then prints out f e key only gets called when it's held down in this space key happens when it gets pressed down as well. So here every time we have the space key it's gonna call that all right if we check our log here's Effie in space key.