Alternative keyboard in input text

Asked

Viewed 402 times

-1

How to make a key work only in text type fields when pressed ?

Example:

$(":text").keydown(function(e) {
    e.stopPropagation();
});

here missing the textarea:

if (e.keyCode == 70 && $(e.target).attr('type') != 'text' && $(e.target).attr('type') != 'password') {
}
  • Which key do you want to work only on text type fields??

  • key and d!!!!

  • 1

    I don’t understand. In which cases do you want these keys to do nothing?

  • 1

    Explaining which feature you want makes it easier to answer correctly. The question is not 100% clear...

  • 2

    What is the difference between this question and the another you did a few days ago? Your question is how to select the textual fields, or how to handle the keystroke events?

2 answers

2

To prevent the button from being recorded if:

  • the key is different from d or e
  • the field does not have type=text

You can use it like this:

$('input, textarea').keydown(function (e) { 
    e.stopPropagation();
    var teclasAceites = e.which == 68 || e.which == 69 || false;
    if (!teclasAceites || $(e.target).attr('type') != 'text') return false;
});

Example online

Placed textarea also, but if your HTML doesn’t have it, you can take

  • 1

    Ok, but this will fail if something prevents the event from propagating into one or more specific fields. http://jsfiddle.net/2Vh4g/.

  • @bfavaretto, really. Then maybe it’s better this way: http://jsfiddle.net/2Vh4g/1/

  • I’m confused, I don’t really understand the question!

1

Do it:

$(function(){

    // vincule o evento o keydown em todos os input type text
    $(":text").keydown(function(e) {
        // se o evento entra aqui ele não se propaga. não sendo cancelado na função global
        e.stopPropagation();
    });
    var cancelKeypress = false;

    // função global que captora todos os demais keydown na pagina e cancela os que você desejar;
    document.onkeydown = function(evt) {
        evt = evt || window.event;
        // na expressão regular você indica os keycodes que você quer bloquear, no caso 68 = d e 69 = f
        cancelKeypress = /^(68|69)$/.test("" + evt.keyCode);
        if (cancelKeypress) {
            return false;
        }
    };

    // hack para o opera
    /* For Opera */
    document.onkeypress = function(evt) {
        if (cancelKeypress) {
            return false;
        }
    };
});

An example here

And textarea is an html tag and not an type of input:

<textarea></textarea>

Note: it is important to delegate the order of events, because the text field has to occur before the global event.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.