3
\$\begingroup\$

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;
    }
\$\endgroup\$
2
  • 4
    \$\begingroup\$ The horror! Please replace someway all these horrible cases with a simple event.keyCode-96... (don't forget to check the range, ofc) \$\endgroup\$
    – o0'.
    Commented Nov 5, 2010 at 17:47
  • 1
    \$\begingroup\$ you can also do case (int)'0' or case (int)'+' which might help in readability. \$\endgroup\$ Commented Nov 5, 2010 at 19:56

3 Answers 3

3
\$\begingroup\$

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).

\$\endgroup\$
0
1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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 . \$\endgroup\$
    – john
    Commented Nov 5, 2010 at 22:54
  • \$\begingroup\$ 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. \$\endgroup\$
    – john
    Commented Nov 5, 2010 at 22:57
-1
\$\begingroup\$

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;
\$\endgroup\$
2
  • \$\begingroup\$ I use action script3 \$\endgroup\$
    – john
    Commented Nov 5, 2010 at 22:52
  • \$\begingroup\$ please explain more what you mean . thanks for your help . \$\endgroup\$
    – john
    Commented Nov 5, 2010 at 22:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .