Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Unicode for ctrl key #20

Merged
merged 4 commits into from

2 participants

@TrevorBurnham

I love the support for ⇧, ⌥ and ⌘, but the lack of ⌃ feels inconsistent to me. I'm guessing that this was a conscious decision to avoid ambiguity with ^ (shift+6), but I hope you'll reconsider—being able to use a single character for every modifier is a win.

In fact, unless you're planning to add support for binding shift+6 as ^, I'd suggest just mapping both characters to ctrl.

@madrobby
Owner

I like it—can you add tests for the key in test/?

@TrevorBurnham

Tests are go!

@madrobby madrobby merged commit 7ffa556 into madrobby:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 18 additions and 14 deletions.
  1. +4 −4 keymaster.js
  2. +1 −1  keymaster.min.js
  3. +13 −9 test/keymaster.html
View
8 keymaster.js
@@ -10,9 +10,9 @@
// modifier keys
_MODIFIERS = {
'': 16, shift: 16,
- option: 18, '': 18, alt: 18,
- ctrl: 17, control: 17,
- command: 91, '': 91
+ '': 18, alt: 18, option: 18,
+ '': 17, ctrl: 17, control: 17,
+ '': 91, command: 91
},
// special keys
_MAP = {
@@ -27,7 +27,7 @@
',': 188, '.': 190, '/': 191,
'`': 192, '-': 189, '=': 187,
';': 186, '\'': 222,
- '[': 219, ']': 221, '\\': 220
+ '[': 219, ']': 221, '\\': 220
};
for(k=1;k<20;k++) _MODIFIERS['f'+k] = 111+k;
View
2  keymaster.min.js
@@ -1,4 +1,4 @@
// keymaster.js
// (c) 2011 Thomas Fuchs
// keymaster.js may be freely distributed under the MIT license.
-(function(a){function h(a,b){var c=a.length;while(c--)if(a[c]===b)return c;return-1}function i(a){var b,g,i,j,l,m;g=(a.target||a.srcElement).tagName,b=a.keyCode;if(b==93||b==224)b=91;if(b in d){d[b]=!0;for(j in f)f[j]==b&&(k[j]=!0);return}if(g=="INPUT"||g=="SELECT"||g=="TEXTAREA")return;if(!(b in c))return;for(l=0;l<c[b].length;l++){i=c[b][l];if(i.scope==e||i.scope=="all"){m=i.mods.length>0;for(j in d)if(!d[j]&&h(i.mods,+j)>-1||d[j]&&h(i.mods,+j)==-1)m=!1;(i.mods.length==0&&!d[16]&&!d[18]&&!d[17]&&!d[91]||m)&&i.method(a,i)===!1&&(a.preventDefault?a.preventDefault():a.returnValue=!1,a.stopPropagation&&a.stopPropagation(),a.cancelBubble&&(a.cancelBubble=!0))}}}function j(a){var b=a.keyCode,c;if(b==93||b==224)b=91;if(b in d){d[b]=!1;for(c in f)f[c]==b&&(k[c]=!1)}}function k(a,b,d){var e,h,i,j;d===undefined&&(d=b,b="all"),a=a.replace(/\s/g,""),e=a.split(","),e[e.length-1]==""&&(e[e.length-2]+=",");for(i=0;i<e.length;i++){h=[],a=e[i].split("+");if(a.length>1){h=a.slice(0,a.length-1);for(j=0;j<h.length;j++)h[j]=f[h[j]];a=[a[a.length-1]]}a=a[0],a=g[a]||a.toUpperCase().charCodeAt(0),a in c||(c[a]=[]),c[a].push({shortcut:e[i],scope:b,method:d,key:e[i],mods:h})}}function l(a){e=a||"all"}function m(a,b,c){a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent&&a.attachEvent("on"+b,function(){c(window.event)})}var b,c={},d={16:!1,18:!1,17:!1,91:!1},e="all",f={"⇧":16,shift:16,option:18,"⌥":18,alt:18,ctrl:17,control:17,command:91,"⌘":91},g={backspace:8,tab:9,clear:12,enter:13,"return":13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,"delete":46,home:36,end:35,pageup:33,pagedown:34,",":188,".":190,"/":191,"`":192,"-":189,"=":187,";":186,"'":222,"[":219,"]":221,"\\":220};for(b=1;b<20;b++)f["f"+b]=111+b;for(b in f)k[b]=!1;m(document,"keydown",i),m(document,"keyup",j),a.key=k,a.key.setScope=l,typeof module!="undefined"&&(module.exports=key)})(this)
+(function(a){function m(a,b,c){a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent&&a.attachEvent("on"+b,function(){c(window.event)})}function l(a){e=a||"all"}function k(a,b,d){var e,h,i,j;d===undefined&&(d=b,b="all"),a=a.replace(/\s/g,""),e=a.split(","),e[e.length-1]==""&&(e[e.length-2]+=",");for(i=0;i<e.length;i++){h=[],a=e[i].split("+");if(a.length>1){h=a.slice(0,a.length-1);for(j=0;j<h.length;j++)h[j]=f[h[j]];a=[a[a.length-1]]}a=a[0],a=g[a]||a.toUpperCase().charCodeAt(0),a in c||(c[a]=[]),c[a].push({shortcut:e[i],scope:b,method:d,key:e[i],mods:h})}}function j(a){var b=a.keyCode,c;if(b==93||b==224)b=91;if(b in d){d[b]=!1;for(c in f)f[c]==b&&(k[c]=!1)}}function i(a){var b,g,i,j,l,m;g=(a.target||a.srcElement).tagName,b=a.keyCode;if(b==93||b==224)b=91;if(b in d){d[b]=!0;for(j in f)f[j]==b&&(k[j]=!0)}else{if(g=="INPUT"||g=="SELECT"||g=="TEXTAREA")return;if(!(b in c))return;for(l=0;l<c[b].length;l++){i=c[b][l];if(i.scope==e||i.scope=="all"){m=i.mods.length>0;for(j in d)if(!d[j]&&h(i.mods,+j)>-1||d[j]&&h(i.mods,+j)==-1)m=!1;(i.mods.length==0&&!d[16]&&!d[18]&&!d[17]&&!d[91]||m)&&i.method(a,i)===!1&&(a.preventDefault?a.preventDefault():a.returnValue=!1,a.stopPropagation&&a.stopPropagation(),a.cancelBubble&&(a.cancelBubble=!0))}}}}function h(a,b){var c=a.length;while(c--)if(a[c]===b)return c;return-1}var b,c={},d={16:!1,18:!1,17:!1,91:!1},e="all",f={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":91,command:91},g={backspace:8,tab:9,clear:12,enter:13,"return":13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,"delete":46,home:36,end:35,pageup:33,pagedown:34,",":188,".":190,"/":191,"`":192,"-":189,"=":187,";":186,"'":222,"[":219,"]":221,"\\":220};for(b=1;b<20;b++)f["f"+b]=111+b;for(b in f)k[b]=!1;m(document,"keydown",i),m(document,"keyup",j),a.key=k,a.key.setScope=l,typeof module!="undefined"&&(module.exports=key)})(this)
View
22 test/keymaster.html
@@ -39,10 +39,12 @@
(el||document).dispatchEvent(event);
}
- var KEYS = { '': 16, shift: 16,
- option: 18, '': 18, alt: 18,
- ctrl: 17, control: 17,
- command: 91, '': 91 };
+ var KEYS = {
+ '': 16, shift: 16,
+ '': 18, alt: 18, option: 18,
+ '': 17, ctrl: 17, control: 17,
+ '': 91, command: 91
+ };
Evidence.TestCase.extend('KeymasterTest', {
testShortcut: function(t){
@@ -106,15 +108,17 @@
testFancyModifierKeys: function(t){
var sequence = '';
- key('+a', function(){ sequence += 'a' });
+ key('+a', function(){ sequence += 'a' });
key('⌥+b', function(){ sequence += 'b' });
- key('⌘+c', function(){ sequence += 'c' });
+ key('⇧+c', function(){ sequence += 'c' });
+ key('⌘+d', function(){ sequence += 'd' });
- keydown(KEYS.shift); keydown(65); keyup(65); keyup(KEYS.shift);
+ keydown(KEYS.ctrl); keydown(65); keyup(65); keyup(KEYS.ctrl);
keydown(KEYS.option); keydown(66); keyup(66); keyup(KEYS.option);
- keydown(KEYS.command); keydown(67); keyup(67); keyup(KEYS.command);
+ keydown(KEYS.shift); keydown(67); keyup(67); keyup(KEYS.shift);
+ keydown(KEYS.command); keydown(68); keyup(68); keyup(KEYS.command);
- t.assertEqual('abc', sequence);
+ t.assertEqual('abcd', sequence);
},
testNonAlphanumericKeys: function(t){
Something went wrong with that request. Please try again.