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 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    18
    Thanked 0 Times in 0 Posts

    controlling arrow keys

    hi everybody;

    i'm using this code to trigger functions with left and right arrow keys..

    but they work even if the arrow keys pressed in input boxes.. is there any way to prevent this code to work when user presses the arrow keys in input boxes?



    Code:
    document.onkeydown = function(evt) {
        evt = evt || window.event;
        switch (evt.keyCode) {
            case 37:
    	geri();
    	gostergeyaz();
    	break;
    
            case 39:
    	ileri();
    	gostergeyaz();
    	break;
        }
    };

  • #2
    New Coder
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    0
    Thanked 4 Times in 4 Posts
    hii Ali Haydar

    You can try to unbind and bind the document.onkeydown event depending on if the textbox has focus or not.

    eg.
    txtbox.onfocus = unbind the document.onkeydown function
    txtbox.onblur = bind the document.onkeydown function

    Hope this helps!

    Cheers

  • Users who have thanked theghostofc for this post:

    ali haydar (02-01-2012)

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Code:
    document.onkeydown = function( e ) 
    {
        var evt = e || window.event,
            srcElem = evt.target || evt.srcElement;
        
        if( !/^(INPUT|TEXTAREA)/.test( srcElem.nodeName ) )
        {
          switch (evt.keyCode) 
          {
            case 37:
    	geri();
    	gostergeyaz();
    	break;
    
            case 39:
    	ileri();
    	gostergeyaz();
    	break;
          }
       }  
          
    };

  • Users who have thanked Logic Ali for this post:

    ali haydar (02-01-2012)

  • #4
    New Coder
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    18
    Thanked 0 Times in 0 Posts
    thank you very much Logic Ali, fantastic..


  •  

    Posting Permissions

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