keydown not firing on div

605 Views Asked by At

I'm having issues with my keydown. When the page is on the div "screenA" the user has two options, press space or "i" key. The "i" key shows an alert, and the space bar shows the div screenTimer . When the user is on the div screenTimer she should be able to press the right arrow key to call up startTesting() and also a console log.

The i key and space bar work, but the right arrow key (39) does nothing, and doesn't display the console log either.

$(document).keydown( function(e)
            {
                if ( boolKeyDepressed )
                    return;
                else
                    boolKeyDepressed = true;

        var e = e || window.event;
                var ltr = e.keyCode ? e.keyCode : e.charCode;
                var trans = "";


                if ( ltr == 13 ) 
                {
                    event.preventDefault();
                }

                switch(ltr)
                {

                    case 73: // "i" key

                        if (!$('screenA').is(':visible')) {
                        alert("i key");
                        }   
                        break;
                    case 32: // space bar 

                        if (!$('screenA').is(':visible')) {

                        showDiv('screenTimer');
                        hideDiv("screenA");

                        }

                        break;

                    case 39: // right arrow key

                        if (!$('screenTimer').is(':visible')) {
                     console.log("timer");
                        startTesting();
                    }

                        break;


                }


        });
1

There are 1 best solutions below

3
On

U have to use keypress not keydown, try your code with keypress