Published on: Monday 10th May 1999 By: Ryan Detert
In order to read a key in VBScript we must first make a subroutine that will be triggered every time that a key is pressed in the Document. This can be done like so:
Sub Document_onKeyPress() ?Code End Sub
The pevious example will execute the code in the subroutine every time that a key is pressed within the document.
Now that we know how to read a keypress from the keyboard, we need to now figure out how to tell exactly which key is being pressed. This is only slightly more difficult and I will show two ways of doing this.
However, VBScript does place some limitations on the keys that you can read in order to maintain the functionality of the browser. You may not read keys that the browser utilizes such as the arrow keys, lock keys, function keys, TAB, backspace, ALT, SHIFT (alone), CTRL, the Number Pad, INS, DEL, HOME, END, PAGE UP/DOWN, PRINT SCRN, PAUSE/BRK, etc.
I will not post a large table of all of these alphanumeric values, however, a chart may be found at ftp://dkuug.dk/i18n/WG15-collection/charmaps/ANSI_X3.4-1968 or an even better one at http://www.bbsinc.com/symbol.html.
Here is the code to trap the m key being pressed:
window.event.keyCode If (window.event.keyCode = 109) Then Msgbox("You just pressed the 'm' key") End If
VBScript offers a very nice property called the Chr() function which allows you to convert an alphanumeric ASCII code to its equivalent string character. In this case, we will use it to convert the alphanumeric generated by the window.event method into an actual letter that us humanoids can understand. For instance, to refine the previous example we can use:
If (Chr(window.event.keyCode) = "m") Then Msgbox("You just pressed the 'm' key") End If
This makes it much easier to understand, and it also saves you the time of having to look up the ASCII codes.
Suppose you wanted to test to see whether or not the M key was pressed, not caring whether it was upper or lowercase. You could use:
If (Chr(window.event.keyCode) = "m" XOR Chr(window.event.keyCode) = "M") Then ?
But it would be much easier, and a little creative, to do something like:
If (UCase(Chr(window.event.keyCode)) = "M") Then ?
This uses UCase() to convert the result of Chr() to uppercase, before comparing it with an uppercase M.
Capturing the mouse is just as easy as reading key presses. The only difference is that we have the ability of retrieving the mouse x and y coordinates separately. These coordinates are expressed in pixels where the top left of the window (roughly) is (0, 0). There are also two ways of reading the pixels from the screen which we will discuss.
The first way to capture the mouse x and y coordinates is to do it relative to the entire window using the offsetX and offsetY attributes. This means that no matter what the size of the window or where it is positioned, x=0 will be the left-most border of the window and y=0 will be at the very top of the blue title bar of the window. The code for this is:
Dim X_absolute, Y_absolute X_absolute = window.event.offsetX Y_absolute = window.event.offsetY
The next way to capture the mouse x and y coordinates, is to do it relative to the document body of the window. Hence, x=0 at the left-most boundary and y=0 at the top-most part of the document's body - i.e. excluding the browsers toolbars and scrollbars. The code for this will look something like:
Dim X_relative, Y_relative X_relative = window.event.X Y_relative = window.event.Y
To trigger an event every time that the mouse does something you may use onClick() and onMouseMove() methods which can be done like so:
Sub Document_onMouseMove() ?Code End Sub Sub Document_onClick() ?Code End Sub
Also note that VBScript allows you to substitute the Document attribute with id's. For instance if you had an image and gave it id = myPic you could trigger an event every time that the mouse moves over the image by using:
Sub myPic_onMouseMove() ?Code End Sub
<script language="VBScript"> Sub Document_onKeyPress() If (UCase(Chr(window.event.keyCode)) = "Y") Then Msgbox("Thank you for Your Support") End If End Sub Sub myPic_onClick() Dim wX, wY, halfX, halfY wX = window.event.offsetX wY = window.event.offsetY halfX = myPic.width \ 2 halfY = myPic.height \ 2 If wX > halfX AND wY < halfY Then Msgbox("You clicked the Blue square!") ElseIf wX < halfX AND wY < halfY then Msgbox("You clicked the Yellow square!") ElseIf wX < halfX AND wY > halfY then Msgbox("You clicked the Red square!") Else Msgbox("You clicked the Green square!") End If End Sub </script>