Skip to main content
Known Participant
February 11, 2013
Answered

Buttons on keys

  • February 11, 2013
  • 1 reply
  • 1074 views

I have a hangman game that uses an input text field called 'letterIn_txt' where the user enters a letter and presses a button called 'guess_btn' which the user presses after enter a letter which turns it to capitals and if the guessed letter is equal to the inProgress word, it replaces it with the letter. I was wondering if I'm able to press keys between a-z to preform this making it easier for the user to play the game. It must turn to capital otherwise it wouldn't be seen as equal to the word. Here's how my button works so far.

this.guess_btn.addEventListener(MouseEvent.CLICK, playMe);

function playMe(Event:MouseEvent):void
{
guessLetter = this.letterIn_txt.text.toUpperCase();
for(var i = 0; i< wordLength; i++)
  {
      if(inProgress.charAt(i) == guessLetter)
      {
         myDisplay=guessLetter;
         correctGuess=true;
         rightCount++;      }
}

I only want to know how I can press keys to preform this action on screen instead of typing the letter out and pressing the button. Any help would be great, thanks!

This topic has been closed for replies.
Correct answer Ned Murphy

You can have a listener for the keyboard and determine which key was pressed.

stage.addEventListener(KeyboardEvent.KEY_UP, checkKey);

function checkKey(evt:KeyboardEvent):void {
   trace(String.fromCharCode(evt.charCode))
}

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
February 11, 2013

You can have a listener for the keyboard and determine which key was pressed.

stage.addEventListener(KeyboardEvent.KEY_UP, checkKey);

function checkKey(evt:KeyboardEvent):void {
   trace(String.fromCharCode(evt.charCode))
}

Syphon25Author
Known Participant
February 12, 2013

Thanks! I'm just having a problem changing myDisplay after pushing a key. How do I name the pushed key 'guessedLetter' so that I can compare it to the in progress word?  if(inProgress.charAt(i) == guessLetter)

Ned Murphy
Legend
February 12, 2013

guessLetter = String.fromCharCode(evt.charCode)