I have a student that has written some code in gamelab to check for keyboard input. When the user presses the “a”, “w”,“s”,or “d” keys, he makes something happen. He asked me if there was a way to know if they user pressed any other key so that he can play a buzz sound. I tried everything that I could think of to help him, but nothing worked the way I thought it would. He said he would just put in a bunch of if statements for all the other keys, but I know there is a better way.
Here are some of the things that we tried:
if (keyDown(“a”)) target = 25;
else if (keyDown(“s”)) target = 57;
else if (keyDown(“w”)) target = 89;
else if (keyDown("d)) target = 121;
This plays the failure sound even when no key is pressed.
We need to know there was a key pressed and then check to see if it was the correct key. Is there a method to do this?