Skip to content

Instantly share code, notes, and snippets.

@yckart
Forked from 140bytes/LICENSE.txt
Created September 22, 2012 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yckart/3767592 to your computer and use it in GitHub Desktop.
Save yckart/3767592 to your computer and use it in GitHub Desktop.
keyeventR: A tweety keyevent-handler
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
}
};
}
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);}};}
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.
{
"name": "keyeventR",
"description": "A tweety keyevent-handler.",
"keywords": [
"shortcut",
"keypress",
"keydown",
"keydup",
"event-handler"
]
}
<!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>
@atk
Copy link

atk commented Sep 24, 2012

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);}};}

@atk
Copy link

atk commented Sep 24, 2012

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)}}

@atk
Copy link

atk commented Sep 24, 2012

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)}}

@yckart
Copy link
Author

yckart commented Sep 24, 2012

Pah! Shame on me :D I've to accept, it was late and I was tired! I'll update the repo soon...

@yckart
Copy link
Author

yckart commented Sep 24, 2012

BTW: Any ideas why my gists are not shared on 140byt.es?

@atk
Copy link

atk commented Sep 25, 2012

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