PageRenderTime 32ms CodeModel.GetById 19ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/src/cocktail/keyboard/as3/Keyboard.hx

http://github.com/silexlabs/Cocktail
Haxe | 88 lines | 36 code | 10 blank | 42 comment | 0 complexity | 51e08353c3e1a4fe01cd933b68bc44d1 MD5 | raw file
 1/*This file is part of Silex - see http://projects.silexlabs.org/?/silex
 2
 3Silex is Š 2010-2011 Silex Labs and is released under the GPL License:
 4
 5This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License (GPL) as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 
 6
 7This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 8
 9To read the license please visit http://www.gnu.org/copyleft/gpl.html
10*/
11package cocktail.keyboard.as3;
12
13import flash.events.KeyboardEvent;
14import flash.Lib;
15import haxe.Log;
16import cocktail.keyboard.abstract.AbstractKeyboard;
17import cocktail.keyboard.KeyboardData;
18
19/**
20 * This is the flash AVM2 implementation of the keyboard abstraction.
21 * Set listeners on native flash keyboard event and call the corresponding
22 * callback
23 * 
24 * @author Yannick DOMINGUEZ
25 */
26class Keyboard extends AbstractKeyboard
27{
28	/**
29	 * class constructor. Set native keyboard listeners
30	 */
31	public function new() 
32	{
33		super();
34	}
35	
36	//////////////////////////////////////////////////////////////////////////////////////////
37	// EVENTS
38	// overriden private native keyboard event handler method
39	//////////////////////////////////////////////////////////////////////////////////////////
40	
41	/**
42	 * Set the listeners for flash keyboard event
43	 */
44	override private function setNativeKeyboardListeners():Void
45	{
46		//in flash keyboard event are listened from the stage to receive global 
47		//keyboard event. We might evolve this features with focus management
48		//eventually
49		Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onNativeKeyDown);
50		Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onNativeKeyUp);
51	}
52	
53	/**
54	 * removes the listeners for flash keyboard event
55	 */
56	override private function unsetNativeKeyboardListeners():Void
57	{
58		Lib.current.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onNativeKeyDown);
59		Lib.current.stage.removeEventListener(KeyboardEvent.KEY_UP, onNativeKeyUp);
60	}
61	
62	//////////////////////////////////////////////////////////////////////////////////////////
63	// Overriden private key utils methods
64	//////////////////////////////////////////////////////////////////////////////////////////
65	
66	/**
67	 * Returns the key that triggered the keyboard event
68	 * @param	event the native key up or down event
69	 * @return a sruct containing the key code and other key values
70	 */
71	override private function getKeyData(event:Dynamic):Key
72	{
73		//cast the flash KeyboardEvent
74		var typedEvent:KeyboardEvent = cast(event);
75		
76		var key:Key = {
77			value : getKeyValue(typedEvent.keyCode),
78			code : typedEvent.keyCode,
79			ascii : typedEvent.charCode,
80			altKey : typedEvent.altKey ,
81			controlKey : typedEvent.ctrlKey,
82			shiftKey : typedEvent.shiftKey
83		}
84		
85		return key;
86	}
87	
88}