Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

Onkeydown wont fire properly in Firefox or Google Chrome.

I have a web application where my custer wants to use the plus sign on the number pad as a tab.  The following code below works with Internet Explorer but now with any other browsers and I can't figure out why?



<script type="text/javascript" language="javascript">
    function convertEnterToTab() {
      if(event.keyCode==107) {
        event.keyCode = 9;
      }
    }
    document.onkeydown = convertEnterToTab;    
  </script>

Open in new window

0
drhamel69
Asked:
drhamel69
  • 4
1 Solution
 
sjklein42Commented:
Try adding "event" as an argument to convertEnterToTab:

<script type="text/javascript" language="javascript">
    function convertEnterToTab(event) {
      if(event.keyCode==107) {
        event.keyCode = 9;
      }
    }
    document.onkeydown = convertEnterToTab;    
  </script>

Open in new window

0
 
drhamel69Author Commented:
Nope.  Didn't work sjklein42
0
 
sjklein42Commented:
Based on what I've learned, the way you pick up the event is different in IE and Firefox.  Also whether the event is writable or readonly.

In Firefox, you must pick up the event as an argument.  Not so in IE.

In Firefox, the event is read-only, so you must create and dispatch a new event based on the old one, and then cancel the old event before it gets executed.

I found a couple examples and came up with this code, which "should" work but doesn't.  For some reason, even though the new event is being dispatched, it is not having any effect.

(By the way, if you are not already using the Firebug add-on to Firefox, I strongly recommend you install it right away.)

<script type="text/javascript" language="javascript">

function convertEnterToTab(winEvent) {
	var keyCode;     // event passed as param in Firefox, not IE where it's defined as window.event
	if(!winEvent) {
		winEvent = window.event;
		keyCode = winEvent.keyCode;
		if(keyCode==107) {
			winEvent.keyCode= 9;
		}
	}
	else {			 // ff
		keyCode = winEvent.which;
		if(keyCode==107) {
			var newEvent = document.createEvent("KeyboardEvent")
			newEvent.initKeyEvent("keydown", true, true, document.defaultView, false, false, false, false, 9, 0)
			winEvent.preventDefault()
			winEvent.target.dispatchEvent(newEvent)
		}
	}
}

document.onkeydown = convertEnterToTab;    

</script>

Open in new window

0
 
sjklein42Commented:
I kept trying, and then found this.  The plot thickens:

http://forums.mozillazine.org/viewtopic.php?f=19&t=665240

(no solution posted)

0
 
sjklein42Commented:
Try this.  For FF, it explicitly advances the focus to the next sibling element on the '+' keypad key.

<script type="text/javascript" language="javascript">


function convertEnterToTab(winEvent) {
	var keyCode;     // event passed as param in Firefox, not IE where it's defined as window.event
	if(!winEvent) {
		winEvent = window.event;
		keyCode = winEvent.keyCode;
		if(keyCode==107) {
			winEvent.keyCode= 9;
		}
	}
	else {			 // ff
		if ( ! winEvent.which != 0 ) keyCode = winEvent.which;
		else keyCode = winEvent.keyCode;	// arrow keys in ff

		if(keyCode==107) {
			winEvent.target.nextElementSibling.focus()
			winEvent.preventDefault()
		}
	}
}

document.onkeydown = convertEnterToTab;    

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now