Understanding Hexadecimal Values and Bitwise Operations in Visual Basic (Classic)

By CodeCraftsman | Created on 2025-07-21 20:06:20

Written with a enthusiastic tone 🤩 | Model: qwen2.5-coder:14b

0:00 / 0:00

Welcome back to the Tek-Tips forums! Today we're diving into the fascinating world of hexadecimal values and bitwise operations, specifically in the context of Visual Basic (Classic). If you've ever wondered what those &H numbers mean or how they interact with your code, you've come to the right place. Let's get started!

Hexadecimal Numbers: A Quick Recap

The &H prefix in programming is used to denote hexadecimal numbers. Hexadecimal is a base-16 number system, which means it uses 16 symbols: 0-9 and A-F. Here are a few examples:

  • &H1 = 1
  • &H8000 = 32768
  • &HFF = 255

Hexadecimal numbers are particularly useful in programming because they allow for easy representation of binary data, especially when dealing with flags or key states.

The &H1 and &H8000 Values Explained

In the context of keyboard input, &H1 and &H8000 have specific meanings:

  • &H10 represents the VK_SHIFT key. When this key is pressed, its state can be checked using the GetKeyState function.
  • &H8000 is a bitmask used to check if a particular bit in the result of GetKeyState is set. Specifically, it checks the high-order bit, which indicates whether the key is currently pressed.

To understand this better, let's look at an example:

```vb Public Const VK_SHIFT = &H10 Dim IsShifted As Boolean IsShifted = (GetKeyState(VK_SHIFT) And &H8000) ```

In this code snippet, GetKeyState(VK_SHIFT) returns a 16-bit value. If the high-order bit of this value is set (i.e., the key is pressed), then IsShifted will be True.

The &H8000 Bitmask in Action

The &H8000 bitmask is a powerful tool for checking specific bits in a value. Here's how it works:

  • &H10 (binary: 0000000000000010)
  • &H8000 (binary: 1000000000000000)

When you use the bitwise AND operator (And) between these two values, the result will be:

```vb 0000000000000010 AND 1000000000000000 --------------------- = 0000000000000000 ```

If the high-order bit of the result is set, then the key is pressed. Otherwise, it's not.

Checking Multiple Keys

Using this knowledge, you can easily check for multiple keys simultaneously. For example, if you want to detect when both the Alt and X keys are pressed at the same time, you can use the following code:

```vb Public Const VK_ALT = &H12 Public Const VK_X = &H58 Dim IsAlt As Boolean Dim IsX As Boolean IsAlt = (GetKeyState(VK_ALT) And &H8000) IsX = (GetKeyState(VK_X) And &H8000) If IsAlt And IsX Then ' Copy the text from an input box to the clipboard Clipboard.Clear Clipboard.SetText(strUserInput) End If ```

In this code snippet:

  • IsAlt checks if the Alt key is pressed.
  • IsX checks if the X key is pressed.
  • If both keys are pressed simultaneously, the text from an input box is copied to the clipboard.

Conclusion

Hexadecimal values and bitwise operations are essential tools in programming, especially when dealing with keyboard input. Understanding how &H1 and &H8000 work can help you write more efficient and effective code. If you have any further questions or need more examples, feel free to ask!

References

That's all for now! Keep coding and stay curious!

Ovatvvon
Programmer
[email protected]



Sources:
- [What are &H1, &H8000, etc.? | Tek-Tips] (https://www.tek-tips.com/threads/what-are-h1-h8000-etc.1080522/)
- [stop h1 tag going onto next line | Tek-Tips] (https://www.tek-tips.com/threads/stop-h1-tag-going-onto-next-line.876965/)
- [Microsoft VBScript runtime error '800a01b6' | Tek-Tips] (https://www.tek-tips.com/threads/microsoft-vbscript-runtime-error-800a01b6.419805/)
- [Why does font-size not inherit ? | Tek-Tips] (https://www.tek-tips.com/threads/why-does-font-size-not-inherit.1050326/)
- [Launch .VBS file from button on HTML page | Tek-Tips] (https://www.tek-tips.com/threads/launch-vbs-file-from-button-on-html-page.931380/)
- [center-align menu item text | Tek-Tips] (https://www.tek-tips.com/threads/center-align-menu-item-text.1769404/)
- [Moving Aplications to a new screen in C# | Tek-Tips] (https://www.tek-tips.com/threads/moving-aplications-to-a-new-screen-in-c.1416749/)
- [Error: Only content controls are allowed directly in a ... - Tek-Tips] (https://www.tek-tips.com/threads/error-only-content-controls-are-allowed-directly-in-a-content-page.1504551/)
- [SDDPCM query : pcmpath query device | Tek-Tips] (https://www.tek-tips.com/threads/sddpcm-query-pcmpath-query-device-lt-n-gt.1276941/)
- [how do i keep sections from moving when i minimize window?] (https://www.tek-tips.com/threads/how-do-i-keep-sections-from-moving-when-i-minimize-window.1611893/)