1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2008-2010 Ricardo Quesada
  4  Copyright (c) 2011      Zynga Inc.
  5 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 
 28 /**
 29  * you must extend the keyboardDelegate and
 30  * implement your own game logic in
 31  * keydown and keyup functions
 32  * @class
 33  * @extends cc.Class
 34  */
 35 cc.KeyboardDelegate = cc.Class.extend(/** @lends cc.KeyboardDelegate# */{
 36     /**
 37      * Call back when a key is pressed down
 38      */
 39     onKeyDown:function () {
 40     },
 41 
 42     /**
 43      * Call back when a key is released
 44      */
 45     onKeyUp:function () {
 46     }
 47 });
 48 
 49 /**
 50  * KeyboardHandler is an object that contains KeyboardDelegate
 51  * @class
 52  * @extends cc.Class
 53  */
 54 cc.KeyboardHandler = cc.Class.extend(/** @lends cc.KeyboardHandler# */{
 55     /**
 56      * returns the keyboard delegate
 57      * @return {cc.KeyboardDelegate}
 58      */
 59     getDelegate:function () {
 60         return this._delegate;
 61     },
 62 
 63     /**
 64      * set the keyboard delegate
 65      * @param {cc.KeyboardDelegate} delegate
 66      */
 67     setDelegate:function (delegate) {
 68         this._delegate = delegate;
 69     },
 70     /**
 71      * initializes a cc.KeyboardHandler with a delegate
 72      * @param {cc.KeyboardDelegate} delegate
 73      * @return {Boolean}
 74      */
 75     initWithDelegate:function (delegate) {
 76         cc.Assert(delegate != null, "It's a wrong delegate!");
 77 
 78         this._delegate = delegate;
 79 
 80         return true;
 81     },
 82     _delegate:null
 83 });
 84 /**
 85  * Create a KeyboardHandler with KeyboardDelegate
 86  * @param delegate
 87  * @return {cc.KeyboardHandler}
 88  */
 89 cc.KeyboardHandler.create = function (delegate) {
 90     var handler = new cc.KeyboardHandler();
 91     handler.initWithDelegate(delegate);
 92     return handler;
 93 };
 94