1. #1
    Ext User
    Join Date
    Mar 2007
    Posts
    43
    Vote Rating
    0
    allaboutyui is on a distinguished road

      0  

    Default YUI Help - KeyListener + Event

    YUI Help - KeyListener + Event


    I'm not entirely sure if this is appropriate - but I'd like to think that this could become a YUI help forum as well as a yui-ext resource. I'm not a fan of mailing lists.

    What do you think to making a YUI forum for discussing and getting help on the non -ext part of the library. There's quite a wealth of YUI users here to provide expertise.

    Secondly my question - I have an object containing the following code

    Code:
    var kl2 = new YAHOO.util.KeyListener(document, { keys:[74,175,170,171,172,174,176] }, 
    																   { fn:this.kp});//, 
    																	 //scope:this,
    																	 //correctScope:true } );					
    		kl2.enable();
    
    //this.kp is a function within the object which calls 
    kp : function(e){	
    	
    	YAHOO.log('pressed: ' + $E.getCharCode(e));
    }
    but the charCode is always 0. I'm stumped. 74 is a keypress for J.

  2. #2
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    17
    jack.slocum will become famous soon enough jack.slocum will become famous soon enough

      0  

    Default


    Try a yui-ext KeyMap.

    Code:
    // builds a KeyMap internally
    var km = getEl(document).addKeyListener([74,175,170,171,172,174,176], this.kp, this);
    
    // or
    var km = getEl(document).addKeyMap({
       key: [74,175,170,171,172,174,176],
       fn: this.kp,
       scope: this
    });
    
    
    // or
    var km = new YAHOO.ext.KeyMap(document, {
       key: [74,175,170,171,172,174,176],
       fn: this.kp,
       scope: this
    });
    
    FYI if you are doing chars, with the yui-ext keymap you can just pass in the chars:
    
    var km = getEl(document).addKeyListener('abcdefg', this.kp, this);
    There's some other examples in the docs.

    If you still get 0, let me know.

  3. #3
    Ext User
    Join Date
    Mar 2007
    Posts
    43
    Vote Rating
    0
    allaboutyui is on a distinguished road

      0  

    Default


    Thanks for the tip Jack.

    From the mailing list I learned that YUI uses a custom event for the keypresses and this explains the problem I was having.

    In slider.js you can find the code with which $E.getCharCode can be used - its something like onkeydown = (my function). The YUI keylistener works differently and the keypress can be found via one of the arguments.

    Code:
    //code when not using keylistener
    
    Event.on(this.id, "keydown",  this.handleKeyDown,  this, true);
    
    handleKeyPress: function(e) {
            if (this.enableKeys) {
                var Event = YAHOO.util.Event;
                var kc = Event.getCharCode(e);
                switch (kc) {
                    case 0x25: // left
                    case 0x26: // up
                    case 0x27: // right
                    case 0x28: // down
                    case 0x24: // home
                    case 0x23: // end
                        Event.preventDefault(e);
                        break;
                    default:
                }
            }
        },
    Hopefully this will help someone else ![/code]

Similar Threads

  1. how to stop click event when having dblclick event
    By seldon in forum Ext 1.x: Help & Discussion
    Replies: 5
    Last Post: 8 Nov 2011, 12:31 AM
  2. HelloWorld Dialog KeyListener and IE7 Problem
    By jongohr in forum Ext 2.x: Help & Discussion
    Replies: 4
    Last Post: 1 May 2008, 12:30 AM
  3. KeyListener not registering
    By Domitian in forum Ext 1.x: Help & Discussion
    Replies: 2
    Last Post: 31 Jan 2007, 12:45 PM
  4. KeyListener, Tabpanel and rest of the site
    By wannabe in forum Ext 1.x: Help & Discussion
    Replies: 1
    Last Post: 15 Sep 2006, 10:43 AM

Thread Participants: 1