-
-
Save yckart/3767592 to your computer and use it in GitHub Desktop.
keyeventR: A tweety keyevent-handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function( | |
a, // The event method (keydown, keyup or keypress) | |
b, // The keycode | |
c, // The callback | |
d, // Placeholder (document) | |
e, // Placeholder (event) | |
f // Placeholder (on + event) | |
) { | |
d = document; // Contains the document object | |
f = d['on' + a]; // Contains the on-event handler | |
d['on' + a] = function(e) { // Executes the function on event | |
e = // Contains the returned keycode cross browser way | |
e ? // If (e)vent is available... | |
e.which // ...use `which` for ie... | |
: // ...else... | |
event.keyCode; // ...use `keyCode` for others | |
if ( | |
!!f // If event is not a function... | |
|| // ...OR... | |
b === e // ...given keycode is equal to pressed key | |
) { | |
c(e); // The callback function | |
} | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(a,b,c,d,e,f) {d=document;f=d['on'+a];d['on'+a]=function(e){e=e?e.which:event.keyCode;if(!!f||b===e){c(e);}};} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2012 Yannick Albert <http://yckart.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "keyeventR", | |
"description": "A tweety keyevent-handler.", | |
"keywords": [ | |
"shortcut", | |
"keypress", | |
"keydown", | |
"keydup", | |
"event-handler" | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: Press <b>left</b>, <b>right</b> or <b>space</b>.</div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var key = function(a,b,c,d,e,f) {d=document;f=d['on'+a];d['on'+a]=function(e){e=e?e.which:event.keyCode;if(!!f||b===e){c(e);}};}; | |
key('keypress', 32, function() { | |
document.getElementById('ret').innerHTML = "Spacebar is pressed with onkeypress" | |
}); | |
key('keyup', 37, function() { | |
document.getElementById('ret').innerHTML = "Left arrow is pressed with keyup" | |
}); | |
key('keydown', 39, function() { | |
document.getElementById('ret').innerHTML = "Right arrow is pressed with keydown" | |
}); | |
</script> |
github comments are strange at the moment and seem no longer to be editable. Here's the code again (I forgot that you can omit the if, too):
function(a,b,c,d,e,f){d=document;f=d[a='on'+a];d[a]=function(e){e=e?e.which:event.keyCode;!!f||b===e&&c(e)}}
Oh, and you don't have to initialize e in the first function if you do it in the second one again:
function(a,b,c,d,f){d=document;f=d[a='on'+a];d[a]=function(e){e=e?e.which:event.keyCode;!!f||b===e&&c(e)}}
Pah! Shame on me :D I've to accept, it was late and I was tired! I'll update the repo soon...
BTW: Any ideas why my gists are not shared on 140byt.es?
Since we cannot test automatically if a gist is a valid entry, jed'll have to do that by hand, which may take some time.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Keycode handling is the other way round than you commented: event.keyCode is for IE and e.which for the other browsers. Why do you trigger if the former event (f) is not a function - isn't that unneccessary? Even then, you can reuse a to prepend the "on":
function(a,b,c,d,e,f){d=document;f=d[a='on'+a];d[a]=function(e){e=e?e.which:event.keyCode;if(!!f||b===e){c(e);}};}