Skip to content Skip to sidebar Skip to footer

Js Deactivate Spacebar When Typing In Textfield

I am creating a music player which uses the spacebar to pause and play audio, now is it possible to know if someone is currently typing in a textfield, because right now, if this u

Solution 1:

Check the event sources tagName attribute:

function(e) {
 e = e || event;
 var el = e.srcElement || e.target,
     cando = !(/textarea|input/i.test(el.tagName));

 if(cando && e.keyCode == 32)  {
   /etc.
 }
}

Solution 2:

You could also try

if(e.keyCode == 32 && document.activeElement != document.getElementById('someTextBox'))

New code

//IF Space bar is Pressed
$(window).keypress(function(e) {
if(e.keyCode == 32) {
    var inputs = document.getElementsByTagName('input');

    for(var item in inputs)
    {
        if(inputs[item] == document.activeElement)
            return;
    }

    if(document.getElementById('audio').paused){
        if(document.getElementById('video').style.display=="block"){

        }
        else{
            document.getElementById('audio').play();
            document.getElementById('pause').style.display="block"; 
            document.getElementById('play').style.display="none";
        }
    }
    else{
        if(document.getElementById('video').style.display=="block"){

        }
        else{
            document.getElementById('audio').pause();
            document.getElementById('pause').style.display="none";
            document.getElementById('play').style.display="block";
        }
    }
}
});
//END IF Space bar is pressed

Solution 3:

Check if a textarea has focus

if(e.keyCode == 32 && !text_area_has_focus) { ... 

Logic for that could be something like this, if you're willing to use jQuery:

if(e.keyCode == 32 && !$('input[type=text]:focus').length) { ... 

Post a Comment for "Js Deactivate Spacebar When Typing In Textfield"