YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Container Family: Implementing Container Keyboard Shortcuts with KeyListener

# Container Family: Implementing Container Keyboard Shortcuts with KeyListener

The KeyListener class integrates with the Container family allowing you to specify specific keys or key combinations to show and hide your containers.

View example in new window.

panel1:
KeyListener Example Panel
Press [ESC] to dismiss this Panel.

## Using the Panel's "keylistener" Property

The KeyListener class provides an easy way to listen for single or compound key events and fire the associated handler function. KeyListener also provides enable and disable methods that dynamically attach and detach DOM listeners to the associated element. The container classes (Panel and its subclasses) take advantage of KeyListeners using the "keylisteners" property. Any associated listeners are enabled when the Panel is shown, and disabled when the Panel is hidden.

In this tutorial, we will two KeyListeners: one that will hide a Panel when the escape key is pressed and one that will show the Panel when the user presses Ctrl+Y. First, we'll create a basic Panel and a KeyListener to enable when the Panel is visible.

YAHOO.example.container.panel1 = new YAHOO.widget.Panel("panel1", { xy:[150,100], visible: false } ); var kl = new YAHOO.util.KeyListener(document, { keys:27 }, { fn:YAHOO.example.container.panel1.hide, scope:YAHOO.example.container.panel1, correctScope:true } ); YAHOO.example.container.panel1.cfg.queueProperty("keylisteners", kl);

There are several important things to note about the KeyListener and how it works with the Panel. The first argument of KeyListener's constructor is the element that the DOM event should be attached to. In both cases in this tutorial, the element will be `document` because we want our key presses to be registered document-wide, regardless of the focus.

The second argument is an object literal containing data defining which keys to listen for. The "keys" argument can either be a number or an array of numbers representing the character code(s) to listen for. This literal also accepts boolean values for "ctrl", "alt", and "shift". We will use the "ctrl" argument in our second KeyListener.

Finally, the third argument defines the handler to be executed when a keypress is detected. This is an object literal as well and it contains three name/value pairs: "fn" represents the function to execute, "scope" represents the scope of the function's execution, and "correctScope", if true, will redefine "this" in your handler to refer the "scope" object.

After defining the KeyListener, we set it into the "keylisteners" property using queueProperty, which indicates that the KeyListeners will be applied to the Panel after it has been rendered.

Our next KeyListener will be created independently of the Panel. It will react to the "Y" key, which has a character code of 89, with the "Control" key depressed at the same time. After instantiating the listener, we can enable it by calling enable directly. Once the listener has been abled, pressing Ctrl+Y should cause the Panel to be displayed.

var kl2 = new YAHOO.util.KeyListener(document, { ctrl:true, keys:89 }, { fn:YAHOO.example.container.panel1.show, scope:YAHOO.example.container.panel1, correctScope:true } ); kl2.enable();

### YUI Logger Output:

Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

### Container Family Examples:

- The Module Control
- Creating and Positioning an Overlay
- Simple Tooltip Example
- One Tooltip, Many Context Elements
- Simple Panel Example
- Skinning a Panel with Custom CSS: Introduction
- Skinning a Panel with Custom CSS: Advanced
- Creating a Modal "Loading" Panel
- Creating a Resizable Panel
- Dialog Quickstart Example
- SimpleDialog Quickstart Example
- Using ContainerEffect Transitions
- Using the Overlay Manager to Manage Multiple Panels
- Implementing Container Keyboard Shortcuts with KeyListener
- Using the Button ARIA Plugin (included with examples for Button Control)
- Using the Container ARIA Plugin
- Example of Color Picker Built in a Dialog via JavaScript (included with examples for Color Picker Control)
- Layout inside a resizable Panel (included with examples for Layout Manager)
- Calendar inside a Container (included with examples for Calendar Control)
- Menu Buttons (included with examples for Button Control)
- Showing, Hiding, and Reordering Columns. (included with examples for DataTable Control)
- Split Buttons (included with examples for Button Control)
- Simple Calendar Menu Button (included with examples for Button Control)
- Calendar Menu Button with Date on Button Face (included with examples for Button Control)
- Color Picker Button (included with examples for Button Control)
- Fixed Width Menu Button (included with examples for Button Control)
- OS-Style Programs Menu (included with examples for Menu Family)
- Application Menubar (included with examples for Menu Family)
- Slider Button (included with examples for Button Control)
- Complex Application (included with examples for Layout Manager)

### More Container Family Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2008 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings