Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Hey Guys . I'm designing a game calculator . My problem is how to link between the keyboard event and the button. **When I click no.2 button on my Keyboard the button doesn't roolover . I want the exact button roolover wen I use the keyboard .

Note

every button have it's instance name ex: the button of number one : one_mc

alt text

my code is

switch (event.keyCode)
    {
    case 97 :
     res[1].appendText("1");
    break;
    case 98 :
     res[1].appendText("2");
    break;
    case 99 :
     res[1].appendText("3");
    break;
    case 100 :
     res[1].appendText("4");
    break;
    case 101 :
     res[1].appendText("5");
    break;
    case 102 :
     res[1].appendText("6");
    break;
    case 103 :
     res[1].appendText("7");
    break;
    case 104 :
     res[1].appendText("8");
    break;
    case 105 :
     res[1].appendText("9");
    break;
    case 96 :
     res[1].appendText("0");
    break;
    case 8 :
      res[1].replaceText(res[1].text.length-1,res[1].text.length,'');
    break;
    }
share|improve this question
4  
The horror! Please replace someway all these horrible cases with a simple event.keyCode-96... (don't forget to check the range, ofc) –  Lohoris Nov 5 '10 at 17:47
1  
you can also do case (int)'0' or case (int)'+' which might help in readability. –  Stephen Furlani Nov 5 '10 at 19:56

3 Answers 3

up vote 3 down vote accepted

Don't use Buttons, use MovieClip instances (different states on different frames) instead. That gives some more work to do since you'll have to switch states manually, but gives more flexibility.

Here's an example with 3 different states (normal, hover, click): http://pastie.org/1276725

As you can see, it has public methods to trigger all states. The mouse-events are triggered automatically, but you can use these public methods to change states externally (eg. from keyboard input).

share|improve this answer
    
that's work thank you . –  Maged Nov 7 '10 at 19:53

Well, the keyboard doesn't exactly have a "rollover" since it's not a mouse. But you can use the keydown event to change the image to the rollover image, and the keyup event to change it back to the normal image. Then put your code (which you pasted in the question) into either keyup or keypress. This should make the behavior you're looking for.

By the way, what happens when you press backspace before entering any numbers? I suspect a JavaScript error will occur; look at your case 8.

share|improve this answer
    
For the first part I gauss it will work well but I'll use many function for every button . Maybe if I can make the button move to the next frame it 'll make sense . –  Maged Nov 5 '10 at 22:54
    
For the second part I use As3 , and there isn't any errors when I use backspace before entering any numbers. Thanks for your help. –  Maged Nov 5 '10 at 22:57

Depending on the language, you could do this:

switch (Event.keyCode)
{
  case (int)'0':
    myButton0.click();
    res[1].appendText("0");
  break;
  ...
}

It's hard to answer specifics if you don't provide a language.


[Edit]

I don't know anything about actions script3 sorry. Some languages allow you to set the depressed button status.

myButton.setDepressed = true;
wait(100);
myButton.setDepressed = false;
share|improve this answer
    
I use action script3 –  Maged Nov 5 '10 at 22:52
    
please explain more what you mean . thanks for your help . –  Maged Nov 5 '10 at 22:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.