Skip to content

Commit

Permalink
Merge pull request #33 from dominictarr/keymaster
Browse files Browse the repository at this point in the history
---

Ive moved the test for INPUT, SELECT and TEXTAREA into a property of `key`,
`key.prekeydown`

if the function assigned to `prekeydown` returns `false`, then keymaster ignores the key press.

you can also change scope in this function, so you can have a scope the shortcuts that work inside an input.

one quirk is that keys in the all scope still work when youve changed the scope.
so, if you want shortcuts that do not apply inside an input, you need to assign them to a particular scope.

Conflicts:
	keymaster.min.js
  • Loading branch information
madrobby committed Mar 21, 2012
2 parents f2c1db0 + a7c27ef commit aed2b80
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
11 changes: 8 additions & 3 deletions README.markdown
Expand Up @@ -83,10 +83,15 @@ key('o, enter', 'files', function(){ /* do something else */ });
key.setScope('issues'); // default scope is 'all'
```

## Notes
## Veto keypresses

By default, when in an `INPUT`, `SELECT` or `TEXTAREA` element is focused, Keymaster doesn't process any shortcuts.

You can change this by overwriting `key.prekeydown` with a new function.

When an `INPUT`, `SELECT` or `TEXTAREA` element is focused, Keymaster
doesn't process shortcuts.
If your function returns false, then the no shortcuts will be processed. If you only want _some_ shortcuts to work while in a input element, you can change the scope in the key.prekeydown funtcion

## Notes

Keymaster should work with any browser that fires `keyup` and `keydown` events,
and is tested with IE (6+), Safari, Firefox and Chrome.
Expand Down
17 changes: 12 additions & 5 deletions keymaster.js
Expand Up @@ -41,8 +41,7 @@

// handle keydown event
function dispatch(event){
var key, tagName, handler, k, i, modifiersMatch;
tagName = (event.target || event.srcElement).tagName;
var key, handler, k, i, modifiersMatch;
key = event.keyCode;

// if a modifier key, set the key.<modifierkeyname> property to true and return
Expand All @@ -54,8 +53,9 @@
return;
}

// ignore keypressed in any elements that support keyboard data input
if (tagName == 'INPUT' || tagName == 'SELECT' || tagName == 'TEXTAREA') return;
// see if we need to ignore the keypress (ftiler() can can be overridden)
// by default ignore key presses if a select, textarea, or input is focused
if(!assignKey.filter.call(this, event)) return;

// abort if no potentially matching shortcuts found
if (!(key in _handlers)) return;
Expand Down Expand Up @@ -131,6 +131,12 @@
}
};

function filter(event){
var tagName = (event.target || event.srcElement).tagName;
// ignore keypressed in any elements that support keyboard data input
return !(tagName == 'INPUT' || tagName == 'SELECT' || tagName == 'TEXTAREA');
}

// initialize key.<modifier> to false
for(k in _MODIFIERS) assignKey[k] = false;

Expand Down Expand Up @@ -166,11 +172,12 @@
// reset modifiers to false whenever the window is (re)focused.
addEvent(window, 'focus', resetModifiers);

// set window.key and window.key.setScope
// set window.key and window.key.set/get/deleteScope, and the default filter
global.key = assignKey;
global.key.setScope = setScope;
global.key.getScope = getScope;
global.key.deleteScope = deleteScope;
global.key.filter = filter;

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

Expand Down
2 changes: 1 addition & 1 deletion keymaster.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions test/keymaster.html
Expand Up @@ -195,6 +195,27 @@ <h1>Keymaster unit tests</h1>

testDoesntFireOnUserInputElements: function(t){
// TODO
},

testCallsFilterFunction: function(t){
var oldFilter = key.filter, filterCalls = 0, keyCalls = 0;

key.filter = function(event){
filterCalls++;
return false;
}

key('a', function(){ keyCalls++; });
keydown(65); keyup(65);

t.assertEqual(1, filterCalls);
t.assertEqual(0, keyCalls);

key.filter = oldFilter;
keydown(65); keyup(65);

t.assertEqual(1, filterCalls);
t.assertEqual(1, keyCalls);
}
});
</script>
Expand Down

0 comments on commit aed2b80

Please sign in to comment.