Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10226 (new enhancement)

Opened 1 year ago

Last modified 8 months ago

[PATCH] Event.isKeyCode for cross-browser event key detection

Reported by: apinstein Assigned to: sam
Priority: low Milestone: 2.x
Component: Prototype Version: edge
Severity: minor Keywords:
Cc: savetheclocktower

Description

I am using prototype to trap the return-key-in-text-fields and prevent it from submitting the form.

This works in all browsers except Safari, for both "return" and "enter" keys: if (e.element().type === 'text' && e.keyCode === Event.KEY_RETURN)

Safari is sending keyCode of 3 for the enter key, so you have to do:

if (e.element().type === 'text' && (e.keyCode === Event.KEY_RETURN e.keyCode === 3))

After reading the prototype docs here regarding keys: http://www.prototypejs.org/api/event

I would expect that it normalizes those keys into the same thing, but hey, maybe I shouldn't expect that. I expected it since there is no KEY_ENTER, and it kinda sounds like that's what the keycode normalization is supposed to do...

In any case, I thought I'd report it. This issue *may* be related to some Autocomplete bugs that I saw while searching for dupes before posting this.

This was tested with Prototype 1.6.

Attachments

isKeyCode.diff (0.6 kB) - added by kangax on 11/30/07 17:25:31.

Change History

11/24/07 02:36:18 changed by kangax

KEY_RETURN and others are just named aliases for numerical key codes. They do not guarantee keyCode normalization.

11/24/07 16:04:36 changed by apinstein

OK then, thanks for the clarification.

I suppose this bug report then should be modified to: 1. Missing KEY_ENTER (which Safari send when hitting ENTER; FF and IE seem to send KEY_RETURN no matter which key you press) 2. Amend documentation to explicitly state that no normalization occurs

These two things would have saved me a bit of time and confusion.

Thanks!

11/30/07 17:25:31 changed by kangax

  • attachment isKeyCode.diff added.

11/30/07 17:32:47 changed by kangax

  • priority changed from normal to low.
  • type changed from defect to enhancement.
  • severity changed from normal to minor.
  • summary changed from Safari "enter" keypress not normalized to KEY_RETURN to [PATCH] Event.isKeyCode for cross-browser event key detection.

Instead of:

  if (Event.KEY_ESC == (event.keyCode || event.which)) {
    ...
  }

a little more concise:

  if (event.isKeyCode(Event.KEY_ESC)) {
    ...
  }

04/03/08 00:52:10 changed by jdalton

  • cc set to savetheclocktower.

+1 :)

04/03/08 01:32:03 changed by jdalton