FocusMgr

Although 'device independence' is not completely possible using lingo alone (you need that initial mouseClick to give Shockwave focus), once Shockwave has focus, you can use the following script to manage keyboard control over your interface. Basically, all this script does is capture key events and send them on to the object that currently has focus. How your interface respond the gaining or losing focus is completely up them.

Objects register themselves with FocusMgr using #RegisterFocusableItem. Such objects should have methods to handle the following messages from the FocusMgr:

  • #Focus
  • #Blur
  • #CanAcceptFocus -- return true or false
  • #KeyInput {optional}

FocusableItems can interact with the FocusMgr using the following public methods:

  • #RegisterFocusableItem (FocusableItem)
  • #RemoveFocusableItem (FocusableItem)
  • #SetFocusTo (registeredFocusableItem)
  • #ReleaseFocus () -- release focus from current focussed item
  • #ReleaseFocusFrom (registeredFocusableItem)

Note: When tabbing, this script will ask a widget (by sending #CanAcceptFocus) whether it can receive focus before actually assigning focus (this way, a widget that is disabled can refuse focus)

Using the Script

The following behaviour outlines how a widget or GUI object might interact with the FocusMgr script:

on beginSprite (me)
  -- register with FocusMgr
  script("Widget.FocusMgr").RegisterFocusableItem(me)
end

on CanAcceptFocus (me) -- sent by FocusMgr
  -- this widget doesn't have a 'disbaled' state so it
  -- can always accept focus
  return 1 
end


on Destroy (me)
  if script("Widget.FocusMgr").ilk = #script then
    script("Widget.FocusMgr").RemoveFocusableItem(me)
  end if
end


on FocusKeyInput (me, k) -- sent by FocusMgr
  case (k) of 
    36, #return:  me.ActivateThisControl()
  end case
end

on Focus (me) -- sent by FocusMgr
  -- SHOW THICK RING ETC
end

on Blur (me) -- sent by FocusMgr
  -- REMOVE THE THICK RING ETC
end


on ActivateThisControl (me)
  script("Widget.FocusMgr").SetFocusTo(me)
  put "CONTROL ACTIVATED"
end

Downloads

FocusMgr script

Demo Movie (Director 11)

First published 24/04/2008