Skip to content

Commit

Permalink
Refactoring code style
Browse files Browse the repository at this point in the history
  • Loading branch information
pantuza committed Nov 1, 2013
1 parent f2b9a26 commit 61ccd80
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions keyBoardShortCuts.js
@@ -1,81 +1,90 @@
/*
* A simple example of key boards shortcuts implementation with javaScript
* using a State Machine
*
* @author : Gustavo Pantuza
* @since : 04.08.2011
*
* http://github.com/pantuza/
*
*/
var keyBoardShortCuts = (function(){

/* Possibles commands to bind */

var keyBoardShortCuts = (function() {


/* Possibles commands to bind */
var keys = {
'ctrl' : false,
'left' : false,
'right': false
},

/* Key Codes */
CTRL = 17,
LEFT = 37,
RIGHT = 39,

element,


/*
* That is the main function of the nameSpace. It is the 'remote control'
* that controls and verify which keys are pressed. Based on these keys,
* could trigger any action. In this case, changes the element style.
*/
remoteControl = function(window){
remoteControl = function(window) {

if( keys.ctrl && keys.left )
if (keys.ctrl && keys.left)
element.style.left = "-10%";

else if( keys.ctrl && keys.right )
else if (keys.ctrl && keys.right)
element.style.left = "+10%";
},


/*
* Each key pressed will trigger these event that verify the key code
* pressed. If the key code matchs one of the codes below, it will set
* true for the key defined on the keys object.
* Then call the 'remoteControl' function that verify the bind keys for
* execute something.
*/
keyDownEventFunction = function(event){
keyDownEventFunction = function(event) {

if( event.keyCode == CTRL )
if (event.keyCode == CTRL)
keys.ctrl = true;

else if( event.keyCode == LEFT )
else if (event.keyCode == LEFT)
keys.left = true;

else if( event.keyCode == RIGHT )
else if (event.keyCode == RIGHT)
keys.right = true;

else
return false;



remoteControl();

event.preventDefault();
event.stopPropagation();
},


/*
* Verify when the key is up (not pressed more). For the same
* reasons, it changes the keys values on the 'keys' object. Then,
* call the 'remoteControl' function again.
*/
keyUpEventFunction = function(event){
keyUpEventFunction = function(event) {

if( event.keyCode == CTRL )
if (event.keyCode == CTRL)
keys.ctrl=false;

else if( event.keyCode == LEFT )
else if (event.keyCode == LEFT)
keys.left=false;

else if( event.keyCode == RIGHT )
else if (event.keyCode == RIGHT)
keys.right=false;

else
Expand All @@ -87,17 +96,19 @@ var keyBoardShortCuts = (function(){
event.stopPropagation();
};


/**
* Waits the 'on ready' of the documento to set variables
*/
window.onload = function(){
window.onload = function() {
element = document.getElementById('square');
}

/**

/**
* Key board event binds
*/
window.addEventListener('keydown' , keyDownEventFunction );
window.addEventListener('keyup' , keyUpEventFunction );
window.addEventListener('keydown', keyDownEventFunction);
window.addEventListener('keyup', keyUpEventFunction);

})(window);

0 comments on commit 61ccd80

Please sign in to comment.