none
Using key inputs RRS feed

  • Question

  • Hi

    After successfully completing my first program I have decided that I want to make a program which uses key inputs so I decided to make a simple program which uses them first. I have tried to make a program which draws a yellow rectangle if you push the y key and a green rectangle if you push the green key. This is the code I have tried but it doesnt seem to work:

    GraphicsWindow.Width=500
    GraphicsWindow.Height=500
    Sub yellow
      If GraphicsWindow.LastKey= y Then
        GraphicsWindow.PenColor="green"
        GraphicsWindow.DrawRectangle(20,20,50,50)
      EndIf
    EndSub

    sub green
      If GraphicsWindow.LastKey= g Then
        GraphicsWindow.PenColor="yellow"
        GraphicsWindow.DrawRectangle(20,20,50,50)
      EndIf
     EndSub

    thanks


    Tuesday, May 29, 2012 7:28 AM

Answers

  • Hey Alex!

    Subroutines are only executed if you call them up using name_of_subroutine(). Thus, the subs -> yellow & green never "happen" in your code above!

    And besides, to auto-call a Sub every time a user presses a key, you need to setup a Key Event, which is a trigger for it!

    You setup one using GraphicsWindow.KeyDown = the_name_of_the_key_sub.

    Another one is GraphicsWindow.KeyUp, which triggers when a key is released; but it's rarely used.

    To demonstrate the concept, here's your code modified to use both types of key trigger. Notice the rectangle is only drawn after you release the "Y" or the "G" keys!

    ' Draw Colored Rectangle Key Event
    
    GraphicsWindow.Width = 500
    GraphicsWindow.Height= 500
    
    GraphicsWindow.KeyDown = KeyColor
    GraphicsWindow.KeyUp   = KeyRect
    
    '________________________________________'
    Sub KeyColor
      
      key = GraphicsWindow.LastKey
      
      If     key = "Y" Then
        GraphicsWindow.PenColor = "Yellow"
      ElseIf key = "G" Then
        GraphicsWindow.PenColor = "Green"
      Else
        key = ""
      EndIf
      
    EndSub
    '________________________________________'
    Sub KeyRect
      
      If key <> "" Then
        GraphicsWindow.DrawRectangle(20,20  50,50)
      EndIf
      
    EndSub 
    '________________________________________'
    Tuesday, May 29, 2012 8:55 AM
    Answerer

All replies

  • Hey Alex!

    Subroutines are only executed if you call them up using name_of_subroutine(). Thus, the subs -> yellow & green never "happen" in your code above!

    And besides, to auto-call a Sub every time a user presses a key, you need to setup a Key Event, which is a trigger for it!

    You setup one using GraphicsWindow.KeyDown = the_name_of_the_key_sub.

    Another one is GraphicsWindow.KeyUp, which triggers when a key is released; but it's rarely used.

    To demonstrate the concept, here's your code modified to use both types of key trigger. Notice the rectangle is only drawn after you release the "Y" or the "G" keys!

    ' Draw Colored Rectangle Key Event
    
    GraphicsWindow.Width = 500
    GraphicsWindow.Height= 500
    
    GraphicsWindow.KeyDown = KeyColor
    GraphicsWindow.KeyUp   = KeyRect
    
    '________________________________________'
    Sub KeyColor
      
      key = GraphicsWindow.LastKey
      
      If     key = "Y" Then
        GraphicsWindow.PenColor = "Yellow"
      ElseIf key = "G" Then
        GraphicsWindow.PenColor = "Green"
      Else
        key = ""
      EndIf
      
    EndSub
    '________________________________________'
    Sub KeyRect
      
      If key <> "" Then
        GraphicsWindow.DrawRectangle(20,20  50,50)
      EndIf
      
    EndSub 
    '________________________________________'
    Tuesday, May 29, 2012 8:55 AM
    Answerer
  • Thanks that works, I understand the whole program except for 1 bit, the line that says:

    If key <> "" Then

    Im not sure what this means, if you could explain it it would be appreciated. also if I wanted to use the arrow keys (up down left right) what would I call them?

    Thanks


    Tuesday, May 29, 2012 6:05 PM
  • Sup Alex!

    "" means empty, nothing stored.

    <> means different, not equal. Something that can be less than or greater than, but never equal to!

    The If key <> "" Then can be understood as something like this:
    If key is not empty, then do the folowing...
    or
    If there's anything stored inside key, then do the following...

    You can see the Else clause where I've had attributed key = "". It only happens if a user pressed a key different amongst the valid ones, that is, "Y" & "G" for now.

    This key = "" works as a flag not to draw a rectangle at Sub KeyRect when a key is released.
    Otherwise, the drawing happens!

    You should notice though GraphicsWindow.KeyUp is rarely used. I only used it to show you that this option exists.

    Here's the same code w/o GraphicsWindow.KeyUp:

    ' Draw Colored Rectangle Key Event
    
    GraphicsWindow.Width = 500
    GraphicsWindow.Height= 500
    
    GraphicsWindow.KeyDown = KeyColor
    
    '________________________________________'
    Sub KeyColor
    
      key = GraphicsWindow.LastKey
    
      If     key = "Y" Then
        GraphicsWindow.PenColor = "Yellow"
      ElseIf key = "G" Then
        GraphicsWindow.PenColor = "Green"
      Else
        key = ""
      EndIf
    
      If key <> "" Then
        GraphicsWindow.DrawRectangle(20,20  50,50)
      EndIf
    
    EndSub
    '________________________________________'

    The only difference is that the rectangle is immediately drawn, instead of waiting the key to be released by user.

    Hope you can fully understand the code now.  :-D

    Tuesday, May 29, 2012 11:59 PM
    Answerer
  • And if you wanna know about the key names to use for GraphicsWindow.LastKey, then take a look at this thread and possibly this one too; look for GraphicsWindow Key Names part.
    Wednesday, May 30, 2012 12:18 AM
    Answerer