[Greasemonkey] Globalize Key Window Events Values script

Bill Donnelly donnelly at snowcrest.net
Fri Aug 5 05:16:38 EDT 2005

Following is a new GmScript I created.
I am looking for input as to what people think about it.
Good idea. Bad. (Indifferent)
Good implementation. etc...
Have I forgotten something, or.... I don't know -- whatever.

Mostly I want access to the Control and Shift keys, myself.
But I decided to make it generic by adding refs to all values.

(fyi: for those who don't know why this is 'necessary'/needed,
in JS, key values aren't available outside of event handlers,
unlike most programming languages that usually make keypress
values available any time the programmer wants to access them,
usually through a function call -- one example usage for this
functionality might be to cause a routine to perform differently
if the control key is pressed -- this is very useful for
bookmarklets, for example, which have no way of knowing if a
'control'/'modifier' key was pressed when they were clicked)


   Globalize Key Window Events Values.

   Author:   William Donnelly. Copyright (c) 2005. All right reserved. 
   Contact:  snowcrest.net | donnelly
   See:      http://www.snowcrest.net/donnelly/gmscripts/

   Globalize Key Window Events Values.    (2005)
   Globalize Key Window Events Values for easy, outside of event handler
   reference by document JS, bookmarklets and other GmScripts.

   The global values are named as follows:


   And corresponde to the following Event properties:


   Ver 1.0.0  Alpha-Beta.  (8/2005)

// userscript metadata follows...

// ==UserScript==
// @name          Globalize Key Window Events Values
// @namespace     http://www.snowcrest.net/donnelly/gmscripts/
// @description   Globalize Key Window Events Values.
// @include       *
// @exclude

// @Version       1.0.0
// @GmVersion     0.5.0
// @Author        William Donnelly
// @Email         snowcrest.net | donnelly
// ==/UserScript==

(function() {

   var unsafeWindow.gkwe_VK_CONTROL = false;
   var unsafeWindow.gkwe_VK_SHIFT = false;
   var unsafeWindow.gkwe_VK_ALT = false;
   var unsafeWindow.gkwe_VK_META = false;

   var unsafeWindow.gkwe_VK_KEYCODE = 0;
   var unsafeWindow.gkwe_VK_CHARCODE = 0;
   var unsafeWindow.gkwe_VK_ISCHAR = false;

   function keyDown (eEvent) {

      unsafeWindow.gkwe_VK_CONTROL = eEvent.ctrlKey;
      unsafeWindow.gkwe_VK_SHIFT = eEvent.shiftKey;
      unsafeWindow.gkwe_VK_ALT = eEvent.altKey;
      unsafeWindow.gkwe_VK_META = eEvent.metaKey;

      unsafeWindow.gkwe_VK_KEYCODE = eEvent.keyCode;
      unsafeWindow.gkwe_VK_CHARCODE = eEvent.charCode;
      unsafeWindow.gkwe_VK_ISCHAR = eEvent.isChar;

   } // theKey

   function keyUp (eEvent) {

      unsafeWindow.gkwe_VK_CONTROL = false;
      unsafeWindow.gkwe_VK_SHIFT = false;
      unsafeWindow.gkwe_VK_ALT = false;
      unsafeWindow.gkwe_VK_META = false;

      unsafeWindow.gkwe_VK_KEYCODE = -1;
      unsafeWindow.gkwe_VK_CHARCODE = -1;
      unsafeWindow.gkwe_VK_ISCHAR = false;

   } // theKey

 try {

   unsafeWindow.addEventListener ("keydown", keyDown, false);
   unsafeWindow.addEventListener ("keyup", keyUp, false);

   var FL = unsafeWindow.frames.length;

   for (var jj = 0; jj < FL; ++jj) {

      unsafeWindow.frames[jj].addEventListener ("keydown", keyDown, false);
      unsafeWindow.frames[jj].addEventListener ("keyup", keyUp, false);

   } // for

 } catch (eErr) { };   // do nothing if something weird occurs


Jack the Ripper may have been quite insane,
but he had beautiful penmanship.
btw -- Don't look back!
The lemmings are gaining on you.

More information about the Greasemonkey mailing list