Learn how to use switches to detect which keys are pressed in this Flash Professional 8 advanced next level training video series.


More DIY videos at 5min.com

Video Transcription

To finish out our handler what we want to do is each time the key-down event is broadcast, we are first going to run in and see which key was pressed and then we will set it up so that we can have a different event happening on different keys. But typically this would take a whole bunch of If statements. So what we are going to do is we are going to try out a different structure in the program called a Switch Structure, and let's go ahead and build a switch structure into our key-down event handler. I am just going to change the Trace statement directly into a switch. Now the switch key will take whatever is in the parameter set and use it as a condition resembling an If statement. We'll need to get rid of the semicolon at the end and change that into a little grouping braces so that we can add the results of each one of these events into it. Now in order to respond to different structures, what I'll use is a case keyword. Now I'd like to set this up so that if the user presses the A key, we do one thing, and if they press the D key, we'll do something else. With the case keyword, I can specify what the results of the condition should be. So I can type-in the letter A. I will type a colon after that and then on the next line I can describe what we are going to do if the letter pressed was A? Now I am just going to do a simple Trace statement and I will just trace pressed A since that's the letter we are looking for, and since we wanted to check if the user press the D key as well, we can add a case for that, and we will add a trace for it. Now I am going to add one more thing to my Switch Structure. Pull it up here just a little bit. At the end of each case, I would like it to stop checking because basically we found the letter that we are looking for. So I am going to add in a Break keyword, and I will do that in both cases here. Now if we just take a look at this and try to read it, we are doing a switch based on the character that the user has pressed. If they have pressed A, we are going to execute the trace for the Press A statement, and if they have pressed D, we are going to execute Press D. Let's give it a try and see that in action. I will test my movie I will press the A key and you can see that it correctly responded with A. If I press the D key, we will get D, and if I press any other key, I am not getting any response at all because we haven't setup a case for that. If you would like to have a case that catches all other ones, at the bottom of your case structure, you can use a default. I am going to go right after the last line, the last break in my case D, and we will add a default. Now this is going to act just like the other cases except this one will be executed when all the other cases have been exhausted. I'll need to put a colon at the end of the line just like the case structure and following that, we can add our action. Now I am going to switch this over from Trace statement, so we can make use of the text field on the screen, so we don't have to go down to the Trace Window as much. So we will pick up that text field, it's called txtKey, we'll take the text property, and in this case, what we will do is we will put in here the question mark character. That will indicate that our user pressed some key but it wasn't one of the keys we were interested in. Now I am just going to modify it to all of our other keys using the same text field, and that way, we'll have our Switch Structure interacting with our user environment as well. Let me copy that and I will just change these Trace statements out here. I'll need to clean them up so that we know which character was pressed. Get rid of those parenthesis leftover from the Trace statement, and now we have a case structure that's looking for the lowercase letter a, lowercase d, and if we press anything else, we will get a question mark character. Let's give that a try. Alright, I'll press the A key and you can see that my text field changed to show the letter that was pressed. I will press the D key, I will press something else and you can see that we get the question mark for all other keyword characters pressed. Now I should point out that if we press Shift+A, that results in a question mark as well since we don't currently have a case for the capital letter A. So as one more thing I will show about the case structure, and that is, if you would like to have multiple cases, respond the same way we can actually add that case structure in. I will add a line right where we defined the case A structure and let's add a case for the capital A. Now you notice, I defined those two structures right next to each other. They both have the same response, and they'll break right after the response. So let's test that out. Now I will press the lowercase a and I will Shift and press an uppercase A. You can see that I get a response of an A as well. So by this point we can see that setting up a keyboard listener allows us to enable all kinds of different events being triggered straight from the keyboard, and also we have got the Switch Structure which takes the place of multiple repeated If statements.