Get and Use Specific Keyboard Keys

From SCI Wiki
Jump to: navigation, 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 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.


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:

(instance RoomScript of Script
  (method (handleEvent pEvent)
    (var str)  //Local variable
    (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))
      //For fun intercept the Mouse event as well
      (case evMOUSEBUTTON
          (Print("You pressed a Mouse Button!"))

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 file. Open the file (..\SciStudio\include\ 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:

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

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