Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org

Related items

un-Gratuitous Gradients

The Amazing ActiveX - Part 1

Browser Redirection using VBScript

Introduction to Visual Basic Scripting (VBScript)

Rattling Keys and Chasing Mice With VBScript

You are here: irt.org | Articles | VBScript | Rattling Keys and Chasing Mice With VBScript [ previous next ]

Published on: Monday 10th May 1999 By: Ryan Detert

Ah! Visual Basic Scripting

Visual Basic Scripting (VBScript) is a powerful tool in web development, much more powerful than many are willing to admit. Its only downfall is that it is only supported by Microsoft Internet Explorer. It has many new operators, such as the XOR operator, that are foreign to JavaScript. Some of its features, such as its support of object classes are fairly more complex than anything JavaScript has to offer, however many things that JavaScript can accomplish with the same efficiency are sometimes easier to grasp for the novice. In this tutorial we will learn how easy it is to read key presses and locate the mouse on the screen via VBScript.

Starting Out From Key 1

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.

Interpreting Key Presses

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.

ASCII AlphaNumeric Method

Every key is assigned an ASCII alphanumeric numeral, for instance the space bar is decimal 32 (hexadecimal 20). These alphanumeric numerals are the same as they are in JavaScript or C++, etc.

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

The User-Friendly Method

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

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.

Viewing the Mouse via the Window

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

Relating the Mouse via the Body

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

Retrieving All Mouse Clicks

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

The Finished Code - A Semi-Useful Example

-Click one of the squares (code featured below)

<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>

Related items

un-Gratuitous Gradients

The Amazing ActiveX - Part 1

Browser Redirection using VBScript

Introduction to Visual Basic Scripting (VBScript)

©2018 Martin Webb