I hope this will help you out. Its a little addition to the Event
object that I made for a project.

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,{
  _specialKeys: {
    'backspace':    8,
    'tab':          9,
    'return':       13,  'enter':      13,
    'shift':        16,
    'ctrl':         17,  'control':    17, 'cmd': 17, // 'cmd' is
detected by opera on macs instead of ctrl
    'alt':          18,  'alterative': 18,
    'pause':        19,  'break':      19,
    'capsLock':     20,  'caps':       20,
    'esc':          27,  'escape':     27,
    'pageUp':       33,
    'pageDown':     34,
    'end':          35,
    'home':         36,
    'left':         37,  'leftArrow':  37,
    'up':           38,  'upArrow':    38,
    'right':        39,  'rightArrow': 39,
    'down':         40,  'downArrow':  40,
    'insert':       45,
    'delete':       46,  'del':        46,
    'windowsLeft':  91,  'Windows':    91,
    'windowsRight': 92,
    'select':       93,
    'f1':           112,
    'f2':           113,
    'f3':           114,
    'f4':           115,
    'f5':           116,
    'f6':           117,
    'f7':           118,
    'f8':           119,
    'f9':           120,
    'f10':          121,
    'f11':          122,
    'f12':          123,
    'numLock':      144, 'num':        144,
    'scrollLock':   145, 'scroll':     145
  },
  _code: function(event) {
    var keyResult;
         if (Object.isNumber(event.keyCode))   keyResult =
event.keyCode;   //DOM
    else if (Object.isNumber(event.which))     keyResult =
event.which;     //NS 4 compatible
    else if (Object.isNumber(event.charCode))  keyResult =
event.charCode;  //also NS 6+, Mozilla 0.9+
    else                                       keyResult =
null;            //total failure
    return keyResult;
  },
  _withModifiers: function(event, keys) {
    if (!keys) return false;
    var check = [], modifiers = [ 'shift', 'ctrl', 'alt' ];
    if (Object.isString(keys)) keys = $w(keys);
    keys.each(function(key, index) {
      if (modifiers.inArray(key)) check[check.length] = event[key
+'Key'];
    });
    return check.all();
  },
  isKey: function(event, key, modifiers) {
    var KeyCode = this._code(event)
    if (
      ( (KeyCode == (/\D/.match(key) ? this._specialKeys[key] : key))
|| (/\d/.match(key) && String.fromCharCode(KeyCode).toLowerCase() ==
key) )
        &&
      (Object.isUndefined(modifiers) ? true :
this._withModifiers(event, modifiers))
    ) return true;
    return false;
  }
});

Some example usage....

document.observe('keydown',function(event){
  if (Event.isKey(event,'ctrl')) {
    console.log('ctrl pressed')
  }
  if (Event.isKey(event,'capsLock','ctrl')) {
    console.log('capsLock with ctrl pressed')
  }
  if (Event.isKey(event,'return',['shift','ctrl'])) {
    console.log('return with shift && ctrl pressed')
  }
  if (Event.isKey(event,65)) {
    console.log('a was pressed')
  }
  if (Event.isKey(event,65,'shift')) {
    console.log('A was pressed')
  }
})


As you can see... The first argument is the event, second is the key
name(responding to _specialKeys)/keyCode to check, third is optional
modifiers (shift, ctrl, alt).
I'm sure some stuff could be done better, its just what I got and hope
it helps you out.

On Mar 6, 1:19 am, 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>
>
> I have noticed that not always on keydown does it register, it comes
> to about every other time.
>
> Can you offer any advice.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" 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/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to