Array.prototype.inArray looks very much like Enumerable#include [1] which Array.prototype mixes in. The only difference is your method does identity comparison and #include does equality one (which should be enough in this case).
[1] http://www.prototypejs.org/api/enumerable/include - kangax On Mar 9, 2:35 pm, tancurrom <[EMAIL PROTECTED]> wrote: > My solution is quite long, but only for usability. It can be reduced > further to this... > > Object.extend(Array.prototype,{ > inArray: function(value) { > for (var i = 0, item; item = this[i]; i++) if (item === value) return > true; > return false; > } > > }); > > Object.extend(Event,{ > isKey: function(event, key, modifiers) { > var KeyCode = function(event) { > return Try.these( > function() { return event.keyCode }, > function() { return event.which }, > function() { return event.charCode } > ) || false > } > var withModifiers = function(event, keys) { > if (!keys) return true > var modifiers = ['shift','ctrl','alt'], check = [] > modifiers.each(function(key, index){ > if (keys.inArray(key)) check[check.length] = event[key+'Key'] > }) > return check.all() > } > if ( (KeyCode(event) == key) && (!modifiers ? true : > withModifiers(event, modifiers)) ) return true > return false > } > > }); > > Some example usage.... > > document.observe('keydown',function(event){ > if (Event.isKey(event,65)) { > console.log('a was pressed') > } > if (Event.isKey(event,65,['shift'])) { > console.log('a was pressed with shift (A)') > } > if (Event.isKey(event,65,['shift','ctrl'])) { > console.log('a was pressed with shift && ctrl (A)') > } > > }) > > As you can see... The first argument is the event, second is the > keyCode to check, third is optional > modifiers (shift, ctrl, alt). > Hope this helps. > > On Mar 7, 7:32 pm, kangax <[EMAIL PROTECTED]> wrote: > > > Observing "keydown" and checking event's "ctrlKey" seems to be > > somewhat consistent. > > There's a great article describing what the state of key detection is > > these dayshttp://unixpapa.com/js/key.html > > > Best, > > kangax > > > On Mar 7, 12:18 pm, louis w <[EMAIL PROTECTED]> wrote: > > > > Thanks TJ. > > > > Does anyone have thoughts on this? The solution proposed by tancurrom > > > seems kind of long. I would think that proto would have the cross- > > > browser keydown stuff already built in. > > > > On Mar 7, 7:07 am, "T.J. Crowder" <[EMAIL PROTECTED]> wrote: > > > > > For other readers: Several replies in louis' cross-posted > > > > threadhttp://groups.google.com/group/prototype-core/browse_thread/thread/50... > > > > > As kangax indicated, best to continue the discussion (if needed) here > > > > in spinoffs, not there in core. > > > > > On Mar 6, 4:44 pm, louis w <[EMAIL PROTECTED]> wrote: > > > > > > I am trying to detect if the control key has been pressed to change > > > > > and action. I am using the latest proto build. > > > > > > Here is my code: > > > > > > <script type="text/javascript"> > > > > > document.observe('keydown', function(k) { > > > > > if (k.keyCode != 17) return; > > > > > alert('ctrl'); > > > > > }); > > > > > </script> > > > > > > For the control key, it will only trigger the alert every other time > > > > > the key is pressed. For other keys it works every time thou. > > > > > > Can you offer any advice. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
