Author Topic: How to get and use specific keyboard keys  (Read 8033 times)

0 Members and 2 Guests are viewing this topic.

Offline Cloudee1

How to get and use specific keyboard keys
« on: January 12, 2007, 08:11:31 PM »
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: [Select]


(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: [Select]


=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


Halloween Competition Brass Lantern Prop Competition Groundhog Day Competition

Offline gumby

Re: How to get and use specific keyboard keys
« Reply #1 on: January 21, 2013, 10:58:20 AM »
For ALT key mapping, see the modified keys.sh file here:

http://sciprogramming.com/community/index.php/topic,700.msg3749.html#msg3749
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline Doan Sephim

Re: How to get and use specific keyboard keys
« Reply #2 on: June 24, 2013, 02:04:15 PM »
I am having some difficulties with this. I am trying to get F2 to do something, but I haven't yet had any success.

I have put the code in and replaced KEY_B with KEY_F2 to see if I would get any change, but nothing happens. The code seems to work just fine for Shift E or B.

What very simple thing am I missing here?

EDIT - I have checked the keys.SH...and of course all the F#'s are there, as many are used as menubar hotkeys.

Offline gumby

Re: How to get and use specific keyboard keys
« Reply #3 on: June 24, 2013, 03:48:01 PM »
Doan, see if my reply in this post here helps:

http://sciprogramming.com/community/index.php/topic,610.0.html

In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline Doan Sephim

Re: How to get and use specific keyboard keys
« Reply #4 on: June 24, 2013, 04:27:06 PM »
I'm afraid there was no change. I also tried putting in the value $3C00 in for F2, but that didn't seem to do anything either.


Offline gumby

Re: How to get and use specific keyboard keys
« Reply #5 on: June 24, 2013, 07:43:42 PM »
Doan - some of those F keys are already mapped to particular menu options (F2 is toggle sound).  I can't get the F keys to work reliably on my laptop (stupid 'smart keys' - I need to turn this off in my BIOS), but replace your RoomScript instance code with this code to the rm001 script file from the template game:

Code: [Select]
(instance RoomScript of Script
(properties)
(method (handleEvent pEvent)
           (var str)  //Local variable
           (super:handleEvent(pEvent))
        
           (switch( (send pEvent:type) )
               (case evKEYBOARD
                    FormatPrint("Key = $%x" (send pEvent:message))
                  (send pEvent:claimed(TRUE))
               )
  )
  )
)

Then just start smacking keys and see what comes up.  This should help troubleshoot what is going on.
« Last Edit: June 24, 2013, 07:57:10 PM by gumby »
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline Doan Sephim

Re: How to get and use specific keyboard keys
« Reply #6 on: June 24, 2013, 10:38:27 PM »
Interestingly enough when I used that code, many of the F-keys returned a value, while the ones assigned to the menubar did not. Since F2 was for Toggle Sound it didn't return a value.

I didn't realize F2 was being used when I decided on using it as the button I wanted. Now that I know it's being used I will probably just choose some other random button.


SMF 2.0.19 | SMF © 2021, Simple Machines
Simple Audio Video Embedder

Page created in 0.069 seconds with 22 queries.