icecats11 / js-hotkeys

Automatically exported from code.google.com/p/js-hotkeys
0 stars 0 forks source link

Bind w/Closures #58

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
var keys = ["Alt+a","Alt+r","Alt+p","Alt+j","Alt+h","Alt+c"];
var baseUrl = "http://www.public.omu.edu/";
var localUrl = ['index','portfolio','projects','photographs','contact'];

function bindUrl(item){
    window.location = baseUrl + item + ".html";
}

function makebindUrl(item){
    return function(){
        bindUrl(item);
    }
}

function handleKeyboardShortcuts(){
    for(var i=0;i<keys.length;i++){
        var item = localUrl[i];
        alert(item);
        $(document).bind('keypress', keys[i],makebindUrl(item));
    };
}

$(document).ready(function(){
    handleKeyboardShortcuts();
});

Hi, 

I am trying to bind several events in a loop using closures but
unfortunately failing every single time.

Any possible pointers would be of immense help.

Original issue reported on code.google.com by lifeinaf...@gmail.com on 10 Jul 2009 at 12:49

GoogleCodeExporter commented 8 years ago
I tried your code but used keydown instead of keypress and it worked
so if there is no special reason for using keypress you may get the same result 
this way.

Original comment by Afro.Sys...@gmail.com on 10 Jul 2009 at 4:22

GoogleCodeExporter commented 8 years ago
Many Thanks!

However, is keydown broken or my usage was simply wrong ?

Original comment by lifeinaf...@gmail.com on 10 Jul 2009 at 5:31

GoogleCodeExporter commented 8 years ago
Also, is it possible to just capture "Alt". No additional key ?

I tried it but it didn't work.

Original comment by lifeinaf...@gmail.com on 10 Jul 2009 at 6:12