3 Replies Latest reply on Oct 7, 2006 8:11 AM by FasterPastor

    Check for simultaneous key presses

    Panda_iam
      Hi,

      I'm creating a game in Director and would like to make it so that when the spacebar is held down and an arrow key presses, the next (or previous) level is loaded, depending on which arrow key was used.

      My problem is checking for the arrow key being pressed while the spacebar is held down. It shouldn't work if the spacebar is released before the arrow key is pressed.

      Any help would be much appreciated!

      Thanks,
      Panda.
        • 1. Re: Check for simultaneous key presses
          duckets Level 1

          Use the 'keypressed()' function.

          For 'untypable' keys, you need to use the key code for that key. Here's a keycode table:

          http://www.robotduck.com/content/articles/director/misc/keycodes/keycodes.txt

          if keypressed(49) and keypressed(126) then
          -- space and up-arrow are pressed!
          end if
          • 2. Re: Check for simultaneous key presses
            Panda_iam Level 1
            Ah, so simple!

            For some reason I didn't think that would work, but it does, perfectly!
            Thank you very much duckets!

            Panda.
            • 3. Check for simultaneous key presses
              FasterPastor
              Great post, RobotDuck. Simple and to the point.

              One more comment for Panda's benefit:

              The same can be accomplished with "CASE / END CASE" instead of "IF / ELSE IF / END IF/ END IF" ... which may benefit you if your available choices ever become more numerous. CASE logic is a little bit easier to code than deeply nested IF statements. For two different examples:

              quote:

              CASE TRUE OF

              keypressed(49) , keypressed(126):
              -- SPACE and UP-CURSOR are both pressed, so do the following
              DoSpace()
              GoUp()

              keypressed(49) , keypressed(125):
              -- SPACE and DOWN-CURSOR are both pressed, so do the following
              DoSpace()
              GoDown()

              otherwise doSomethingElse(arg)

              END CASE
              Note: The "otherwise" part is optional.

              You can also do more complex logic by using AND and/or OR:

              quote:

              CASE TRUE OF

              ( keypressed(49) AND keypressed(126) ):
              -- SPACE and UP-CURSOR are both pressed, so do the following
              DoSpace()
              GoUp()

              ( keypressed(49) AND keypressed(125) ):
              -- SPACE and DOWN-CURSOR are both pressed, so do the following
              DoSpace()
              GoDown()

              otherwise doSomethingElse(arg)

              END CASE
              Notes:

              Judicious use of parentheses, combined with use of "AND" and/or "OR" conditions, can accommodate very complex logic. The whole string within the the various parentheses should result to a potential match for the condition in the CASE TRUE OF opening statement.

              Another option to consider would be CASE FALSE OF, instead of CASE TRUE OF. In such a case, the whole string within the the various parentheses should result to a potential MISMATCH for the condition in the CASE...OF opening statement.

              The most commonly used option is CASE VARIABLE1 OF, instead of CASE TRUE/FALSE OF.

              Lastly, place your more complex condition(s) earlier in the list, because the way CASE works is to cease making comparisons as soon as its first match is found. Thus, if you wanted to test for the UP CURSOR alone, as well as SPACE+UPCURSOR, then test for the latter first, and the simpler one later.