Jump to content

Passing Parameters To The Onkeydown Event?


tinfanide

Recommended Posts

document.onkeydown = function(e){  e = (typeof e == "undefined") ? event : e ;  b = (typeof e.which == "number") ? e.which : e.KeyCode ;		    if(b==37){alert("You've pressed the left arrow key.")}  }

In this case, can parameters be passed to onkeydown?Like:

document.onkeydown = function(e,k,f){  e = (typeof e == "undefined") ? event : e ;  b = (typeof e.which == "number") ? e.which : e.KeyCode ;		    if(b==k){f;}  }

so that later on whenever I needa use the onkeydown event, I just need to do: func X (39,func(){})

Link to comment
Share on other sites

If what you mean is to be able to assign a key code to a callback function, you could do it this way:

// Create a callbacks array where each keycode is assigned to a function.  var callbacks = [];// This method adds a function to the specified keycode.function addCallback(keyCode, f) {  callbacks[keyCode] = f;} document.onkeydown = function(e) {  // Get the event object  e = (e ? e : window.event);  // Get the keycode  var key = (e.keyCode ? e.keyCode : (e.which ? e.which : false))   // If a function exists for this key, run it (and pass the event object just in case)  if(callbacks[key]) {	callbacks[key](e);  }}

If you want multiple callbacks for each keycode, you'll have to make a multidimensional array and loop through each function when a button is pressed.

Link to comment
Share on other sites

This is a bit hard to understand, but thanks so much for the model codes.Needa do a lot more on assigning or callbacking arrays.

Link to comment
Share on other sites

"Callbacks" is any normal array, you could call it anything.

var bob = [];function addCallback(keyCode, f) {  bob[keyCode] = f;}

When addCallback is called, it assigns the function to the element of the array with the keycode as the key:

addCallback(37, function(e) { alert("You pressed the left key"); } );

The previous line of code simply does the following.callbacks[37] = function(e) { alert("You pressed the left key"); }; After you have done that, every time the left key is pressed that function will be run, because I have this in the document's keydown event:

if(callbacks[key]) {  // This just runs the function that was stored in the "key" element of the "callbacks" array  callbacks[key](e);  // For example: callbacks[37](e);}

The drawback of this method is that you can't assign two functions to the same keycode. The second one will overwrite the first one.So only the second function of the following two would be executed when a key is pressed.

addCallback(37, function() { alert("a") } );addCallback(37, function() { alert("b") } );

Link to comment
Share on other sites

Yes, that's just what I've used in my code:

addCallback(37, function(e) { alert("You pressed the left key"); } );

but I do it with the (e), just ()something like this:

window.onload = function(){addKeyStroker(37,function(){alert(You pressed the left key);});}

The bit I don't quite understand is the new syntax (to me):

var bob = [];function addCallback(keyCode, f) {  bob[keyCode] = f;}

It seems to me that an array is used to store a function (f) and the function f is passed through another function addCallback.Am I getting it right?

Link to comment
Share on other sites

An element of an array can be anything, even a function.

var x = [];x[0] = "String";x[1] = 50;x[2] = function() { return 10; };

So all I'm doing is making an array where the number ( x[number]) is the code of the key that's pressed, so that it indicates which function to run.

Link to comment
Share on other sites

Yes, now I see the connection between the array and the key event.But what is this syntax all about:

if(callbacks[key]) {	    callbacks[key](e);  }

Why after an array is followed by (e)?I knew (e) is attached to the onkeydown event function(e){ }

Link to comment
Share on other sites

callbacks[key] is a function, so to run the function, you need parentheses:callbacks[key](); It's pretty much like this:

callbacks[37] = function() { /* Do something */ };callbacks[37]();

I'm just passing the event object "e" to the function just in case somebody wants to use it. Like if they assign the same function to several keycodes and then want to know the number using e.keyCode The if() statement just makes sure there's a function there before trying to execute it.

Link to comment
Share on other sites

Is that just after

callbacks[keyCode] = f;

The array callbacks[keyCode] becomes a functionand(e) is the parameter passed to the callbacks function?

Link to comment
Share on other sites

The array itself doesn't become a function, only that element does. "e" is an event object. It gets created by the browser when an event occurs. I'm passing "e" to the callback function so that it can be available there just in case that the person wants it. It is not necessary, you could write the parenthesis without "e" as well ()

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...