Problem regarding onkeypress and onkeydown

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gjain12
    New Member
    • Jun 2008
    • 19

    Problem regarding onkeypress and onkeydown

    Hi all,

    I am using the following code to disable the ctrl+a/c/v/x keys.

    Code:
    <html>
    <head>
    <script language="JavaScript" type="text/javascript">
    function disableCtrlKeyCombination(e)
    {
    //list all CTRL + key combinations you want to disable
    
    var forbiddenKeys = new Array('a', 'c', 'x', 'v');
    var key;
    var isCtrl;
    
    if(window.event)
    {
    
    key = window.event.keyCode;//IE
    if(window.event.ctrlKey)
    isCtrl =true;
    else
    isCtrl =false;
    }
    
    else
    {
    
    key = e.which; //firefox
    
    if(e.ctrlKey)
    isCtrl = true;
    else
    isCtrl = false;
    }
    
    //if ctrl is pressed check if other key is in forbidenKeys array
    
    if(isCtrl)
    {
    for(i=0; i< forbiddenKeys.length; i++)
    {
    
    //case-insensitive comparation
    
    if(forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase())
    {
    return false;
    }
    
    }
    
    }
    
    return true;
    }
    
    document.onkeypress=disableCtrlKeyCombination
    document.onkeydown=disableCtrlKeyCombination
    
    </script>
    
    <title>Untitled Page</title>
    </head>
    <body>
    
    <form id="form1">
    
    <input type="text" name="mytext" id="txtinput" />
    
    <input type="text" name="test" />
    
    </form>
    </body>
    </html>
    It's working fine, but the problem is that it disables the keys for the complete document wherever I want it only for the particular text field.

    Also one point to note is that I can't use the following to achieve this

    Code:
    <input type="text" name="mytext" onkeypress="return disableCtrlKeyCombination(event);" onkeydown="return disableCtrlKeyCombination(event);" />
    Is it possible to use the onkeypress and onkeydown in the script itself in place of using it on a particular field so that it will disable the key for that particular field.

    Thanks
    Gaurav Jain
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Attach it to the input text element. To do that, first give it an id, e.g. "mytext":
    [code=javascript]document.getEle mentById("mytex t").onkeypress= disableCtrlKeyC ombination;
    document.getEle mentById("mytex t").onkeydown=d isableCtrlKeyCo mbination;[/code]
    An alternative is to use addEventListene r/attachEvent. Make sure that this is added after the page has loaded because the input element won't be available during page load.

    Comment

    • gjain12
      New Member
      • Jun 2008
      • 19

      #3
      Hi,

      Thanks for such a quick and accurate response.
      Can you please also tell me how can I disable the right click and copy/paste option from the Edit menu of explorer for the same field.

      Thanks
      Gaurav Jain
      Originally posted by acoder
      Attach it to the input text element. To do that, first give it an id, e.g. "mytext":
      [code=javascript]document.getEle mentById("mytex t").onkeypress= disableCtrlKeyC ombination;
      document.getEle mentById("mytex t").onkeydown=d isableCtrlKeyCo mbination;[/code]
      An alternative is to use addEventListene r/attachEvent. Make sure that this is added after the page has loaded because the input element won't be available during page load.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        did you try mrhoo's suggestion, that he gave you? you cannot disable the edit menu but you may workaround its usage with this suggestion ...

        kind regards

        Comment

        Working...