O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Flash Hacks
By Sham Bhangal
June 2004
More Info

HACK
#47
Use Accessibility Text as Help Text
Accessibility text is normally used for screen readers for the sight impaired. However, you can also use it as help text for the sighted
The Code
[Discuss (0) | Link to this hack]

The Code

function addHelp(target) {
  target.onRollOver = function( ) {
    // Delay the appearance of the help text by 1.2 seconds
    helpInterval = setInterval(createHelpText, 1200, target);
  };
  target.onRollOut = target.onDragOut=function ( ) {
    // Hide the help text when the user rolls off the clip
    clearInterval(helpInterval);
    removeHelpText( );
  };
}
function createHelpText(target) {
  // Create a clip to hold the help text
  var helpClip:MovieClip = attachMovie("helpText", "helpText", 
                          this.getNextHighestDepth( ));
  clearInterval(helpInterval);
  // Set the attributes of the help text
  var helpText:TextField = helpClip.helpText;
  helpText.background = true;
  helpText.backgroundColor = 0xFFFFCC;
  // Format the help text
  var helpFormat:TextFormat = new TextFormat( );
  helpFormat.leftMargin = 2;
  helpFormat.rightMargin = 2;
  helpFormat.indent = 0;
  helpFormat.size = 15;
  helpFormat.font = "_sans";
  // If the target has an accessibility name, 
  // display it in the help text box.
  if (target._accProps.name != undefined) {
    helpText.text = target._accProps.name;
  }
  if (target._accProps.shortcut != undefined) {
    // If the target has a keyboard shortcut, 
    // add it to the help text box.
    helpText.text += "\n" + target._accProps.shortcut;
  }
  // Set the help text's dimensions
  var fieldSize:Object = helpFormat.getTextExtent(helpText.text);
  helpText._height = fieldSize.textFieldHeight;
  helpText._width = fieldSize.textFieldWidth;
  helpClip._x = _xmouse - helpClip._width / 2;
  helpClip._y = _ymouse - helpClip._height - 10;
}
function removeHelpText( ) {
  helpText.removeMovieClip( );
}
function aHandler( ) {
  // Event handler for button A
  trace("You clicked A");
}
function bHandler( ) {
  // Event handler for button B
  trace("You clicked B");
}
function cHandler( ) {
  // Event handler for button C
  trace("You clicked C");
}
function down( ) {
  // Function to detect a key combo and 
  // run the associated button script.
  var allKeys;
  // Test to see if all the keys in the combo are down
  for (var j = 0; j < keys.length; j++) {
    allKeys = true;
    for (var i = 0; i < keys[j].combo.length; i++) {
      allKeys = allKeys && Key.isDown(keys[j].combo[i]);
    }
    if (allKeys) {
      // If all the keys in the combo are down, give the 
      // associated button focus, and run the associated
      // button's event handler. Finally, disable further 
      // events until the combo is released.
      Selection.setFocus(keys[j]);
      this.onKeyDown = undefined;
      this.onKeyUp = up;
      keys[j].btn.onRelease( );
      break;
    }
  }
}
function up( ) {
  // Function runs when a combo is released
  this.onKeyUp = undefined;
  this.onKeyDown = down;
}
//
var keys:Array = new Array( );
var keyListener:Object = new Object( );
keys[0] = {btn:buttonA, combo:[65]};
keys[1] = {btn:buttonB, combo:[Key.CONTROL, 66]};
keys[2] = {btn:buttonC, combo:[Key.CONTROL, 68]};
buttonA.onRelease = aHandler;
buttonB.onRelease = bHandler;
buttonC.onRelease = cHandler;
addHelp(buttonA);
addHelp(buttonB);
addHelp(buttonC);
keyListener.onKeyDown = down;
Key.addListener(keyListener);


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.