Get and Use Specific Keyboard Keys

From SCI Wiki
Revision as of 16:45, 5 August 2013 by Andrew Branscom (talk | contribs) (1 revision)
Jump to navigationJump to search

By Cloudee1

This is a real old post that I dug up from the depths of Mega-Tokyo. I'm in the middle of a big mini game kick and in order to expand control options I am looking into using keys. But I wasn't sure how to check for the keys pressed because I didn't know the values to check for. This old post first describes how to check for the pressed buttons if they have been declared in the Keys.sh file and the second little bit of code describes how to output the value of a key pressed such that you wouldn't need to edit the keys file to check for it. So you can just go check for one anytime you need a keys value.


INTERCEPTING KEYBOARD AND MOUSE EVENTS

For this demonstration, Ego will change into Brian if the player presses B (capital) and back to Ego when E (capital) is pressed.

Add Brian as a view as described in the Tutorial Chapter 19 (Resource->Add Sample->View->Brian and save it as view number 1.

In the instance of RoomScript add the following code:

Code:
(instance RoomScript of Script
  (properties)
  (method (handleEvent pEvent)
    (var str)  //Local variable
    (super:handleEvent(pEvent))

    (switch( (send pEvent:type) )
      (case evKEYBOARD
          (if(== (send pEvent:message) KEY_B )  //Check if B was pressed
              (send gEgo:view(1))  //switch to Brian
              (send pEvent:claimed(TRUE))
          )//if B

          (if(== (send pEvent:message) KEY_E )  //Check if E was pressed
              (send gEgo:view(0))  //switch to Ego
              (send pEvent:claimed(TRUE))
          )//if
      )//evKEYBOARD

      //For fun intercept the Mouse event as well
      (case evMOUSEBUTTON
          (Print("You pressed a Mouse Button!"))
      )//evMOUSEBUTTON
    )//switch
  )//method
)//instance

The code: send pEvent:claimed(TRUE)): specified whether the event object can be used; in this case the :Said: command will ignore the event when the player presses either a B or E.

Compile and test. Ego will be transformed into Brian if you press a B and back to Ego if you press an E. If it does not work, the values may not be declared in the Keys.sh file. Open the file (..\SciStudio\include\Keys.sh) to view the defined values. You may want to expand the list with the lower case values (a=$61,..z=7a). If you want to trap other values, you can view the value of the key code by adding the following code just below the :case evKEYBOARD: statement:

Code:
=str ""
(Format(str "str=%x" (send pEvent:message)))
(Print(str))

Test by pressing Ctrl-k. The value should be $0b. You can add the value in the Keys.sh file or using it directly in the statement (if(== (send pEvent:message) $0b) ext