Making a MaxGUI Application: StripAnimMakerLite Part 8 - Catching Keyboard Events
(c) Assari Jan 29 2006

I like using the mouse but there are times when the keyboard gives greater precision. So what we are going to do now is allow the user to use the keyboard for precise cursor movements, both during normal and resize cursor mode on our Main Canvas

The keys that we are going to use are:-

New Additions to Part 1 - Variable Definitions

No new variables need

New Additions to Part 2 - Gadget Creation Block

No new gadget required

New Additions to Part 3 - MAIN LOOP

We need to add a new process_keydownevent function here.
        Case EVENT_KEYDOWN
        Process_KeyDownEvent



New Additions to Part 4 - Function Definitions

In this block we need to do the following
Function Process_KeyDownEvent()

Once the keydown event is generated by MaxGUI, we first need to determine that the event came from the Main Canvas, then we can check for which key has been pressed using the EventData() function.

We need to process the keypresses differently depending on whether which mode we are in, normal or resize. Then it's just a matter of checking through each key event that we wish to trap and change the relevant variables appropriately.
    Function Process_KeyDownEvent:Int()

Select EventSource()
    Case Canvas1

      If CursorResize=True 'we are in change size mode
          Select EventData()
            Case KEY_LEFT
                xsize=xsize-1; If xsize<1 xsize=1
                mx1a=mx1a-1; If mx1a<0 mx1a=0
              Case KEY_RIGHT
                xsize=xsize+1; If xsize>128 xsize=128
                mx1a=mx1a+1
                If mx1a>GadgetWidth(Canvas1) Then ..
                   mx1a=GadgetWidth(Canvas1)
            Case KEY_UP
                ysize=ysize-1; If ysize<1 ysize=1
                my1a=my1a-1; If my1a<0 my1a=0
            Case KEY_DOWN
                ysize=ysize+1; If ysize>128 ysize=128
                my1a=my1a+1
                If my1a>GadgetHeight(Canvas1) Then ..
                   my1a=GadgetHeight(Canvas1)
               
            Case KEY_SPACE
                  CursorResize=False        
                xsize=mx1a-mx1_Old
                  ysize=my1a-my1_Old
                If xsize>MaxCursorSize Then xsize=MaxCursorSize
                If ysize>MaxCursorSize Then ysize=MaxCursorSize
                MoveMouse(mx1_Old,my1_Old)
            Case KEY_ENTER
                CopyPixUnderCursorToAnimStrip()

        End Select
      Else
        Select EventData()
            Case KEY_LEFT
                 mx1=mx1-1; If mx1<0 mx1=0
              Case KEY_RIGHT
                mx1=mx1+1
                If mx1>GadgetWidth(Canvas1) Then ..
                   mx1=GadgetWidth(Canvas1)
               
            Case KEY_UP
                my1=my1-1; If my1<0 my1=0
            Case KEY_DOWN
                my1=my1+1
                If my1>GadgetHeight(Canvas1) Then ..
                   my1=GadgetHeight(Canvas1)

            Case KEY_SPACE
                 CursorResize=True    'user click left mouse
                xsize_old=xsize
                ysize_old=ysize
                  mx1_Old=mx1
                  my1_Old=my1
                  MoveMouse(mx1+xsize,my1+ysize)
            Case KEY_ENTER
                CopyPixUnderCursorToAnimStrip()

        End Select        
      EndIf
    RedrawGadget(Canvas1)
End Select
End Function

Function Process_MouseEnterEvent

Add the activateGadget(Canvas1) statement to ensure we can capture keyboard events
    Function Process_MouseEnterEvent:Int()

 Select EventSource()
    Case Canvas1
        HideMouse()
        MouseInCanvas1=True
        RedrawGadget(Canvas1)
        ActivateGadget(Canvas1)
 End Select

End Function


Appearance wise nothing has changed in our program but we should now be able to use the keyboard to manipulate our cursor. Download the current source (here) and have a play.

That's all for now. Return to Main Index.