Skip to content
This repository has been archived by the owner on Oct 13, 2020. It is now read-only.

Commit

Permalink
Added functions for recording whether a key is down
Browse files Browse the repository at this point in the history
  • Loading branch information
mimshwright committed Oct 24, 2012
1 parent dfe6c02 commit 110b114
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
20 changes: 19 additions & 1 deletion README.markdown
Expand Up @@ -61,14 +61,32 @@ and `f1` through `f19`.
## Modifier key queries

At any point in time (even in code other than key shortcut handlers),
you can query the `key` object for the state of modifier keys. This
you can query the `key` object for the state of any keys. This
allows easy implementation of things like shift+click handlers. For example,
`key.shift` is `true` if the shift key is currently pressed.

```javascript
if(key.shift) alert('shift is pressed, OMGZ!');
```

## Other key queries

At any point in time (even in code other than key shortcut handlers),
you can query the `key` object for the state of any key. This
is very helpful for game development using a game loop. For example,
`key.isDown(77)` is `true` if the M key is currently pressed.

```javascript
if(key.isDown("M")) alert('M key is pressed, can ya believe it!?');
if(key.isDown(77)) alert('M key is pressed, can ya believe it!?');
```

You can also get these as an array using...
```javascript
key.getPressedKeyCodes() // returns an array of key codes currently pressed
```


## Scopes

If you want to reuse the same shortcut for seperate areas in your single page app,
Expand Down
35 changes: 33 additions & 2 deletions keymaster.js
Expand Up @@ -28,7 +28,8 @@
'`': 192, '-': 189, '=': 187,
';': 186, '\'': 222,
'[': 219, ']': 221, '\\': 220
};
},
_downKeys = [];

for(k=1;k<20;k++) _MODIFIERS['f'+k] = 111+k;

Expand All @@ -43,6 +44,10 @@
function dispatch(event, scope){
var key, handler, k, i, modifiersMatch;
key = event.keyCode;

if (index(_downKeys, key) == -1) {
_downKeys.push(key);
}

// if a modifier key, set the key.<modifierkeyname> property to true and return
if(key == 93 || key == 224) key = 91; // right command on webkit, command on Gecko
Expand Down Expand Up @@ -86,7 +91,14 @@

// unset modifier keys on keyup
function clearModifier(event){
var key = event.keyCode, k;
var key = event.keyCode, k,
i = index(_downKeys, key);

// remove key from _downKeys
if (i >= 0) {
_downKeys.splice(i, 1);
}

if(key == 93 || key == 224) key = 91;
if(key in _mods) {
_mods[key] = false;
Expand Down Expand Up @@ -130,6 +142,23 @@
_handlers[key].push({ shortcut: keys[i], scope: scope, method: method, key: keys[i], mods: mods });
}
};

// Returns true if the key with code 'keyCode' is currently down
// Converts strings into key codes.
function isPressed(keyCode) {
if (typeof(keyCode)=='string') {
if (keyCode.length == 1) {
keyCode = (keyCode.toUpperCase()).charCodeAt(0);
} else {
return false;
}
}
return index(_downKeys, keyCode) != -1;
}

function getPressedKeyCodes() {
return _downKeys;
}

function filter(event){
var tagName = (event.target || event.srcElement).tagName;
Expand Down Expand Up @@ -178,6 +207,8 @@
global.key.getScope = getScope;
global.key.deleteScope = deleteScope;
global.key.filter = filter;
global.key.isPressed = isPressed;
global.key.getPressedKeyCodes = getPressedKeyCodes;

if(typeof module !== 'undefined') module.exports = key;

Expand Down
9 changes: 9 additions & 0 deletions test.html
Expand Up @@ -20,6 +20,8 @@ <h1>
<li>Press 'i'. Switches scope to 'issues'.</li>
<li>Press 'c'. Console should log function call.</li>
<li>Press 'o' or Enter or Cursor &larr;. Console should log function call.</li>
<li>Press and hold 'm'. Console should log a message every second.</li>
<li>Every second console should log a message listing all the currently pressed keycodes.</li>
</ol>

<p>
Expand Down Expand Up @@ -76,6 +78,13 @@ <h1>
key('/', function(){ console.log('/') });
key('shift+]', function(){ console.log('shift+]') });

setInterval(function () {
console.log('All keys currently down: ' + key.getPressedKeyCodes());
if (key.isPressed(77)) {
console.log('M key is currently down');
}
}, 1000);

// document.onkeydown = function(event){
// console.log(event.keyCode);
// }
Expand Down

0 comments on commit 110b114

Please sign in to comment.