Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Disable the key "T"

    I have an input box so the user can type in. I just want to disable a couple of keys not all. I know how to make an alert go off if there is a keydown. But how do I make it so when a certain key go down, it will just not do anything - not even an alert.

    Thanks

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript" language="javascript">

    var blockedkeys = 't'; //lower-case

    function getKey(e) {
    return e.keyCode ? String.fromCharCode(e.keyCode).toLowerCase() :
    e.which ? String.fromCharCode(e.which).toLowerCase() : null;
    }

    function blockKeys(e) {
    var key = getKey(e);
    if (!key) return true;
    else return blockedkeys.indexOf(key) == -1;
    }

    onload = function() {document.forms[0].elements[0].focus()}

    </script>
    </head>
    <body>
    <form>
    <input type="text" onkeypress="return blockKeys(event)">
    </form>
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Jul 2002
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool That was fast!

    Very cool too!

    Thanks for your help Adios.

    Have a good day!

  • #4
    New Coder
    Join Date
    Jul 2002
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually, I just thought of something else.

    How would you disable all keys?

  • #5
    New Coder
    Join Date
    Jul 2002
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I got it.

    Thanks again.

    function blockKeys(e)
    {
    var key = getKey(e);
    if (!key) return true;
    else return false;
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •