Note that there are some explanatory texts on larger screens.

plurals
  1. POkeycode shortcut each function
    primarykey
    data
    text
    <p>I'm working yet on a small script to capture keyevents and easily bind functions on them. But I'm stuck for now!</p> <p>The problem is, that if I initalize more than one "eventhandler" it overides arguments from the first initialization.</p> <p>Thousands of words do not say more than some lines of code. So, here's what I've done so far:</p> <pre><code>var keyCodes={a:65,b:66,c:67,d:68,e:69,f:70,g:71,h:72,i:73,j:74,k:75,l:76,m:77,n:78,o:79,p:80,q:81,r:82,s:83,t:84,u:85,v:86,w:87,x:88,y:89,z:90,"0":48,"1":49,"2":50,"3":51,"4":52,"5":53,"6":54,"7":55,"8":56,"9":57,f1:112,f2:113,f3:114,f4:115,f5:116,f6:117,f7:118,f8:119,f9:120,f10:121,f11:122,f12:123,shift:16,ctrl:17,control:17,alt:18,option:18,opt:18,cmd:224,command:224,fn:255,"function":255,backspace:8,"delete":8,enter:13,"return":13,left:37,up:38,right:39,down:40}; var keyCall = function(k, fn, c, e, k2, nk) { this.onkeydown = function() { nk = k.split(" "); c = e ? e.which : event.keyCode; if (nk.length &gt; 1) { if (keyCodes[nk[0]] === c) { k2 = true; } if (keyCodes[nk[1]] === c &amp;&amp; k2 === true) { fn(); k2 = false; } } else if (keyCodes[nk[0]] === c) { fn(); } }; }; keyCall('ctrl a', function() { // overridden by `keyCall('shift a'` alert('callback1'); }); keyCall('shift a', function() { alert('callback2'); }); </code></pre> <p>I've optimized it for a highly minimum of bytes, if you think an annotated version is usefull, so just say it! For everyone who needs, here's a <a href="http://jsfiddle.net/ARTsinn/HLUYt/" rel="nofollow">fiddle</a></p> <p><strong>Update</strong></p> <p>pascalfree has fixed it! But anoter problem is now coming up... </p> <p>The "placeholder"-variable <code>k2</code> checks if the first key was/is pressed. But for now if you press and release <code>ctrl</code> and hit thereafter the <code>a</code>-key the function will fired. Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload