- Home /
checking for keyboard input with exceptions
I'm making a hangman type of game, and basically in the CheckIfCorrect() function i call it when a user enters a letter. However it still is called when the user hits tab or backspace. How can i make these two buttons an exception? After further tests, it seems that backspace and tab are exceptions only when no letters have been guessed. Not sure why though. Code:
function OnGUI() {
if(endGame == false)
{
TimeHandler();
var keyEvent : Event = Event.current;
var textPosX = 45;
var textPosY = Screen.height - 50;
lastLetterSet = new String[typeAreas.Length];
for(i = 0; i < typeAreas.Length; i++)
{
typeAreas[i] = GUI.TextField(Rect(textPosX, textPosY, 50, 50), typeAreas[i], 1, typeAreaStyle);
textPosX += 50;
if(typeAreas[i] == "") //reset to underscore if area is empty
{
typeAreas[i] = "_";
}
}
if(keyEvent.isKey)
{
if(keyEvent.keyCode != KeyCode.Tab && keyEvent.keyCode != KeyCode.Backspace)
{
Debug.Log("key event pressed");
checkWord = true;
FinalAnswer();
}
}
}
}
function CheckIfCorrect() {
if(checkWord == true) {
for(i = 0; i < typeAreas.Length; i++)
{
if(typeAreas[i] != "_")
{
if(typeAreas[i] == word[i])
{
isCorrect = 1;
feedback = true;
}
else {
isCorrect = 2;
feedback = true;
}
}
}
checkWord = false;
}
}
Comment
Your answer
Follow this Question
Related Questions
OnPointerDown vs OnMouseDown 1 Answer
Detect a specific Key Press Event Without Keyboard Input 2 Answers
EventType.MouseDrag not working in WebPlayer 0 Answers
Mac Keys not showing up, or very strangely... 2 Answers
New Input System: Event on input type changed (between mouse, keyboard and controller) 1 Answer