* Binds a on key down and on key up callback to a key or key combo. Accepts a string containing the name of each * key you want to bind to comma separated. If you want to bind a combo the use the plus sign to link keys together. * Example: 'ctrl + x, ctrl + c' Will fire if Control and x or y are
(keyCombo, callback, endCallback)
| 276 | * @param endCallback |
| 277 | */ |
| 278 | function bindKey(keyCombo, callback, endCallback) { |
| 279 | |
| 280 | function clear() { |
| 281 | if(keys && keys.length) { |
| 282 | var keyBindingGroup = keyBindingGroups[keys.length]; |
| 283 | |
| 284 | if(keyBindingGroup.indexOf(keyBinding) > -1) { |
| 285 | var index = keyBindingGroups[keys.length].indexOf(keyBinding); |
| 286 | keyBindingGroups[keys.length].splice(index, 1); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | //create an array of combos from the first argument |
| 292 | var bindSets = keyCombo.toLowerCase().replace(/\s/g, '').split(','); |
| 293 | |
| 294 | //create a binding for each key combo |
| 295 | for(var i = 0; i < bindSets.length; i += 1) { |
| 296 | |
| 297 | //split up the keys |
| 298 | var keys = bindSets[i].split('+'); |
| 299 | |
| 300 | //if there are keys in the current combo |
| 301 | if(keys.length) { |
| 302 | if(!keyBindingGroups[keys.length]) { keyBindingGroups[keys.length] = []; } |
| 303 | |
| 304 | //define the |
| 305 | var keyBinding = { |
| 306 | "callback": callback, |
| 307 | "endCallback": endCallback, |
| 308 | "keyCombo": bindSets[i], |
| 309 | "keys": keys |
| 310 | }; |
| 311 | |
| 312 | //save the binding sorted by length |
| 313 | keyBindingGroups[keys.length].push(keyBinding); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return { |
| 318 | "clear": clear |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Binds keys or key combos to an axis. The keys should be in the following order; up, down, left, right. If any |