What's the Shift in VB?

By Ethan Wychwood | Created on 2025-10-02 04:47:19

Written with a enthusiastic tone 🤩 | Model: keyless-gpt-o3-mini

0:00 / 0:00
In Visual Basic (VB), when working with keyboard input, it can be puzzling to understand how to check for specific key states. One common issue is deciphering what the hexadecimal values (&H1, &H8000, etc.) mean and how they're used. For instance, Ovatvvon asked about the significance of &H1 and &H8000 in relation to the Shift key in a discussion on Tek-Tips forums. ADoozer provided an initial response, explaining that hexadecimal represents base 16 numbers and offering a way to convert between bases using Windows Calculator. However, Sheco's subsequent explanation and code examples shed more light on the matter. According to MSDN documentation for the GetKeyState function, the return value is a 16-bit integer (a SHORT data type in VB). When a key is pressed, the high-order bit (the leftmost bit) becomes 1, indicating the key is down. The low-order bit represents a toggle state; when it's set to 1, the key is toggled on. When checking for the Shift key specifically, Sheco demonstrated that in binary, VK_SHIFT (&H10) would be represented as 0000000000000010 and GetKeyState returns 1000000000000010 if the Shift key is down. The bitwise AND operation (&&) with &H8000 (which is 1000000000000000 in binary) effectively tests for the high-order bit, allowing us to determine whether the Shift key is pressed. Using this understanding, a developer can create functions that respond to specific keyboard combinations, such as ALT and X. For example, they might want to copy text from an input box to the clipboard when the user presses ALT + X simultaneously. In summary, VB's GetKeyState function uses a 16-bit integer to represent key states, with the high-order bit indicating whether the key is pressed or not and the low-order bit representing toggle status. By using bitwise operations and understanding hexadecimal values, developers can effectively check for specific keys in their applications.

Sources:
- [What are &H1, &H8000, etc.? | Tek-Tips] (https://www.tek-tips.com/threads/what-are-h1-h8000-etc.1080522/)
- [Query a Registry Key | Tek-Tips] (https://www.tek-tips.com/threads/query-a-registry-key.860209/)
- [Coverage Paths | Tek-Tips] (https://www.tek-tips.com/threads/coverage-paths.1632312/)
- [Why does'nt text-align:justify work? Help | Tek-Tips] (https://www.tek-tips.com/threads/why-doesnt-text-align-justify-work-help.1690560/)
- [H1 in publisher | Tek-Tips] (https://www.tek-tips.com/threads/h1-in-publisher.1601129/)
- [Forwarding one internal extension to External number | Tek-Tips] (https://www.tek-tips.com/threads/forwarding-one-internal-extension-to-external-number.1574548/)
- [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/)
- [Using ShellExecute to close/stop a running VB6 exe | Tek-Tips] (https://www.tek-tips.com/threads/using-shellexecute-to-close-stop-a-running-vb6-exe.1679973/)
- [Microsoft VBScript runtime error '800a01b6' | Tek-Tips] (https://www.tek-tips.com/threads/microsoft-vbscript-runtime-error-800a01b6.419805/)
- [hta application to submit url and create multiple tabs] (https://www.tek-tips.com/threads/hta-application-to-submit-url-and-create-multiple-tabs.1632994/)
Related Posts