Question:
Help JavaScript Experts: YUI KeyListener Problem....?
red scar
2007-09-02 23:49:32 UTC
YUI KeyListener has this code...
YAHOO.util.KeyListener ( attachTo , keyData , handler , event )

Please explain it to me in your own words - what a handler is??? I tried using a function for handler but will not work when I press a key in the keyboard.
Please help... I tried searching it in the web but all I get are the codes. What I need are explanations.
Please help...
Three answers:
John T
2007-09-03 03:21:53 UTC
I'm no javascript expert but since no one has taken a shot at this I will.



A handler tells a message dispatcher to go to function or method A when Event B occurs. A message dispatcher is a loop that is checked periodically for events fires the "handler" you hooked in. In this case the dispatcher is called a "Listener". It listens for keypresses and calls the handler that you define when those keypresses are made.





// This is the actual handler

var handler = function(type, args, obj)

{

// Put your code here

}



// Create an object that listens for when the keys 49, 50, or 51 are pressed and attaches the handler to the listener



YAHOO.example.keylistener.kpl1 = new YAHOO.util.KeyListener(document, { keys:[49,50,51] }, { fn:handler } );



// This turns ON your custom listener so it can start processing keypresses and sending them to your handler.



YAHOO.example.keylistener.kpl1.enable();
richarduie
2007-09-03 14:10:45 UTC
This is the publish/subscribe pattern. Some element "publishes" notices of the occurrences of events that reflect changes in its state. Other elements register an intent to receive notification about these events, i.e., they "subscribe." This is also called "event-listening," and handlers are often referred to as "listeners."



The keyboard, pointing device (mouse, touch-pad, etc.), form elements, window - all of these items and more - publish a wide variety of events without your having to do anything. For instance, onmouse* events, onchange, onclick, onkeypress are standard events published for a wide variety of elements. In order to subscribe, you register a handler. That is, you declare that a function should be called whenever a particular event is published by a particular element.



"Handler" is simply the common term applied to these functions. The handler can do anything you find appropriate in response to receiving notification of the event.

______________



Here's a couple of samples, one with the YUI class you were wrestling, one without.





















anonymous
2007-09-03 13:48:38 UTC
It is not that easy. May be you can contact a javascript expert. Check websites like http://askexpert.info/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...