1. Home
  2. Computing & Technology
  3. Visual Basic
Symbolic Logic
Part 4: Going Behind the Symbolic Logic Mask
 More of this Feature
• Part 1: Symbolic Logic
• Part 2: The Basics of Symbolic Logic
• Part 3: Mapping Out Symbolic Logic
• Part 5: Visualize Symbolic Logic
 
 Join the Discussion
Is this the kind of article that helps you?
Let us know!
 
 Related Resources
• Calculating a Contrasting Color Code
The first article in the series. • The Imp Logical Operator
• VB .NET New Logical Operators AndAlso and OrElse
 
 Elsewhere on the Web
• Association for Symbolic Logic
• Karnaugh Maps
• A game for understanding Venn Diagrams
 

In the Color Code article, we used hex numbers as masks to select part of a three-byte color code. Another example where masking is required in VB can be found in event subroutines such as the KeyUp and KeyDown events. Let's look at Microsoft's documentation of KeyDown in detail to see how masking works.

The Microsoft documentation describes the syntax of the KeyDown event as follows:

Private Sub Form_KeyDown(keycode As Integer, shift As Integer)

keycode

A key code, such as vbKeyF1 (the F1 key) or vbKeyHome (the HOME key). To specify key codes, use the constants in the Visual Basic (VB) object library in the Object Browser.

shift

An integer that corresponds to the state of the SHIFT, CTRL, and ALT keys at the time of the event. The shift argument is a bit field with the least-significant bits corresponding to the SHIFT key (bit 0), the CTRL key (bit 1), and the ALT key (bit 2 ). These bits correspond to the values 1, 2, and 4, respectively. Some, all, or none of the bits can be set, indicating that some, all, or none of the keys are pressed. For example, if both CTRL and ALT are pressed, the value of shift is 6.

Let's look at an example. A shift value of 6 in binary is 0110. (Binary numbers are traditionally shown in groups of four bits even when the leading bit is zero to aid in legibility and conversion to hex.)

If you wanted to find out about the state of the Ctrl key when value of shift is 6 you could write a compound condition like this:

If KeyShift = 2 _
Or KeyShift = 3 _
Or KeyShift = 6 _
Or KeyShift = 7 Then ...

These are the four values where the second bit (Ctrl) is 1 (0010, 0011, 0110, 0111).

Microsoft, however, recommends using a logical mask to test the state of the Shift and even defines four mask values to use:

Here's Microsoft's example showing how to use the masks:

Microsoft Shift Constants

You test for a condition by first assigning each result to a temporary integer variable and then comparing shift to a bit mask. Use the And operator with the shift argument to test whether the condition is greater than 0, indicating that the modifier was pressed, as in this example:

ShiftDown = (Shift And vbShiftMask) > 0

In a procedure, you can test for any combination of conditions, as in this example:

If ShiftDown And CtrlDown Then ...

In the first statement, the variable ShiftDown is given a Boolean value of True or False by determining whether Shift And'ed with vbShiftMask is > 0. In the second statement, the result of the first comparison is used along with the result of another comparison.

Next page > Visualize Symbolic Logic > Page 1, 2, 3, 4, 5
Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.