Understanding Hexadecimal Notation in Visual Basic

By TechTrekker | Created on 2025-07-21 03:40:44

Written with a analytical tone 🧠 | Model: qwen2.5-coder:14b

0:00 / 0:00

Welcome to Tek-Tips! Today, we'll explore the meaning behind hexadecimal values like &H1 and &H8000 in Visual Basic. These values are often used in keyboard state detection functions.

What Does &H Mean?

In programming, especially in languages like Visual Basic, &H is a prefix that denotes a number is in hexadecimal (base 16) format. This means the digits can range from 0-9 and A-F, where A represents decimal 10, B represents decimal 11, and so on up to F which represents decimal 15.

Converting Hexadecimal to Decimal

To understand what &H1 and &H8000 represent in decimal form:

  • &H1 = 1 in decimal
  • &H8000 = 32768 in decimal

Using Hexadecimal Values with GetKeyState Function

The `GetKeyState` function in Visual Basic returns a SHORT (16-bit) integer that specifies the status of a virtual key. The return value can be interpreted as follows:

  • If the high-order bit (the leftmost bit) is 1, the key is currently down.
  • If the low-order bit is 1, the key is toggled (e.g., CAPS LOCK).

For example:

```vb Public Const VK_SHIFT = &H10 ' Shift key Dim IsShifted As Boolean IsShifted = (GetKeyState(VK_SHIFT) And &H8000) <> 0 ' Checks if the high-order bit is set ```

In this code, `VK_SHIFT` is the virtual keycode for the Shift key. By performing a bitwise AND operation with &H8000, we check the high-order bit of the result from `GetKeyState`. If it's set (i.e., not zero), then the Shift key is currently pressed.

Checking Multiple Keys

To check for multiple keys simultaneously, you can use similar logic. For instance, if you want to detect when both ALT and X are pressed:

```vb Public Const VK_ALT = &H12 ' Alt key Public Type KBDLLHOOKSTRUCT vkCode As Long ' virtual keycode scanCode As Long ' hardware scancode flags As Long time As Long dwExtraInfo As Long End Type Dim IsALT As Boolean Dim tempVKCodeHolder As Integer IsALT = (GetKeyState(VK_ALT) And &H8000) <> 0 ' Checks if the high-order bit is set for ALT key tempVKCodeHolder = correctedInputValues(CInt(Trim(HookStruct.vkCode))) ' Converts keyboard input to ASCII value If IsALT And tempVKCodeHolder = Asc("X") Then Clipboard.Clear Clipboard.SetText(strUserInput) End If ```

In this example, we first check if the ALT key is pressed. Then, we convert the virtual keycode from `HookStruct` to its corresponding ASCII character. Finally, we compare it with the ASCII value of "X". If both conditions are met, the text in `strUserInput` is copied to the clipboard.

Summary

Understanding hexadecimal notation and how to use it with functions like `GetKeyState` is essential for handling keyboard input in Visual Basic. By using bitwise operations, you can easily detect key states and perform actions based on user input.

If you have any more questions or need further clarification, feel free to ask!



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/)
- [Why does font-size not inherit ? | Tek-Tips] (https://www.tek-tips.com/threads/why-does-font-size-not-inherit.1050326/)
- [Microsoft VBScript runtime error '800a01b6' | Tek-Tips] (https://www.tek-tips.com/threads/microsoft-vbscript-runtime-error-800a01b6.419805/)
- [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/)
- [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/)
- [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/)
- [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/)