Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 12th, 2008, 10:24 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default How can I test for the CTRL down?

Or for that matter, the Shift or alt key?
 
Old June 5th, 2008, 07:50 AM
Authorized User
 
Join Date: May 2008
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hope this helps

Private Sub object_KeyDown([index As Integer,]keycode As Integer, shift As Integer)



Constant Value Description
vbShiftMask 1 SHIFT key bit mask.
VbCtrlMask 2 CTRL key bit mask.
VbAltMask 4 ALT key bit mask.

The constants act as bit masks that you can use to test for any combination of keys.

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



 
Old June 5th, 2008, 01:45 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

This is a VB6 event you’ve provided in a .NET forum, so this event procedure doesn't even exist in .NET.

But beyond that, I don't want to detect the key being depressed, I want to evaluate its state.

In other words, at the time of my choosing (rather than at the instant it is pressed), I want to “read” the control key’s state.
 
Old June 6th, 2008, 06:52 AM
Authorized User
 
Join Date: May 2008
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK brian, sorry for the last reply didnt know what you where after excatly. As you have probably guessed i'm knew to all this but as you where kind enough to help me out i wanted to return the favour.I did a search on the subject and filtered it for .net solutions so hopefully this is what your after if not i'll give up trying to help you as it seems i'm not helping at all

typedef enum tagKEYMODIFIERS
{
    KEYMOD_SHIFT = 0x00000001,
    KEYMOD_CONTROL = 0x00000002,
    KEYMOD_ALT = 0x00000004
} KEYMODIFIERS;

 Elements
KEYMOD_SHIFT

The Shift key is currently depressed.

KEYMOD_CONTROL

The Control key is currently depressed.

KEYMOD_ALT

The Alt key is currently depressed.

 
Old June 6th, 2008, 06:56 AM
Authorized User
 
Join Date: May 2008
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HRESULT TranslateAccelerator(
  LPMSG pMsg , //Pointer to the structure
  DWORD grfModifiers //Flags describing the state of the keys
);
 Parameters
pMsg

[in] Pointer to the MSG structure describing the keystroke to be processed.

grfModifiers

[in] Flags describing the state of the Control, Alt, and Shift keys. The value of the flag can be any valid KEYMODIFIERS enumeration values.

 Return Values
S_OK

The container processed the message.

S_FALSE

The container did not process the message. This value must also be returned in all other error cases besides E_NOTIMPL.

E_NOTIMPL

The container does not implement accelerator support.

 
Old June 6th, 2008, 11:24 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

At the risk of making a bad pun, this TranslateAccelerator() might be the kernel of the solution to my quest.
 
Old June 6th, 2008, 12:06 PM
Authorized User
 
Join Date: May 2008
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Either that or you are fed up reading my dim witted replies and just want me to stop lol

 
Old June 6th, 2008, 12:27 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

GetKeyState() retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled on or off, alternating each time the key is pressed.
Code:
SHORT GetKeyState( 
 int nVirtKey 
);
The return value specifies the status of the specified virtual key, as follows:
  • If the high-order bit = 1, the key is down; otherwise, it is up.
  • If the low-order bit = 1, the key’s toggled. A key, such as “CAP LOCK,” is toggled if it’s turned on. The key is off &untoggled if the low-order bit = 0. A toggle key’s indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.
The GetKeyState function can be used to check the down state of the following virtual keys:
Code:
VK_CONTROL  VK_RCONTROL  VK_LCONTROL
VK_MENU     VK_RMENU     VK_LMENU
VK_SHIFT    VK_RSHIFT    VK_LSHIFT
The key status returned from this function changes as a specified thread reads key messages from its message queue. The status does not reflect the interrupt-level state associated with the hardware.

Use the GetAsyncKeyState function to retrieve that information.

An app calls GetKeyState in response to a keyboard-input message. This function retrieves the state of the key when the input message was generated.

An app. can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the nVirtKey parameter. This gives the status of the SHIFT, CTRL or ALT keys w/o distinguishing between left &right.

An app can also use the following virtual-key code constants as values for nVirtKey to distinguish between the left & right keys:
Code:
VK_LSHIFT        VK_RSHIFT
VK_LCONTROL      VK_RCONTROL
VK_LMENU         VK_RMENU
These left- & right-distinguishing constants are available to an app only through the GetAsyncKeyState, GetKeyState & MapVirtualKey functions.

The GetKeyboardState function copies the status of the 256 virtual keys to the specified buffer.
Code:
BOOL GetKeyboardState(          PBYTE lpKeyState
);
lpKeyState is a pointer to a 256-byte buffer to hold the results.

If the function succeeds, the return value is nonzero.

Code:
Symbolic         Hex        Dec      Keyboard
Constant         Val        Val      Equivalent
VK_SHIFT         0x10        16         SHIFT
VK_LSHIFT        0xA0       160      L. SHIFT
VK_RSHIFT        0xA1       161      R. SHIFT

VK_CONTROL       0x11        17         CTRL
VK_LCONTROL      0xA2       162      L. CTRL 
VK_RCONTROL      0xA3       163      R. CTRL

VK_MENU          0x12        18         ALT
VK_LMENU         0xA4       164      L. ALT
VK_RMENU         0xA5       165      R. ALT

VK_PAUSE         0x13        19         PAUSE
VK_ESCAPE        0x1B        27         ESC
VK_RETURN        0x0D         13        ENTER
VK_CAPITAL       0x14        20         CAPS LOCK
VK_NUMLOCK       0x90       144         NUM LOCK
VK_SCROLL        0x91       145         SCROLL LOCK
 
Old June 6th, 2008, 01:36 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

This toggles the Alt radio button with each press of the Alt key.

If I hold more than 1 of these 3 keys down, the state of the radiobuttons rapidly change from checked to unchecked and back.
Code:
Imports System.Windows.Forms.Application
Imports System.Windows.Forms

Public Class Form1

    Declare Function GetKeyState _
            Lib "user32" (ByVal nVirtKey As Integer) _
            As Short


    Private Sub ClickIt(ByVal s As Object, _
                        ByVal e As System.EventArgs) _
                        Handles Me.Click

        Dim Ctrl As Integer = 17
        Dim Shft As Integer = 16
        Dim Allt As Integer = 18

        Do While True
           DoEvents
            rbCtrl.Checked = ((128 AND GetKeyState(Ctrl)) = 128)
           DoEvents
            rbShft.Checked = ((128 AND GetKeyState(Shft)) = 128)
           DoEvents
            rbAlt.Checked  = ((128 AND GetKeyState(Allt)) = 128)
           DoEvents
        Loop

    End Sub


    Private Sub FormClose(ByVal s As Object, _
                          ByVal e As FormClosedEventArgs) _
                          Handles Me.FormClosed
        End
    End Sub

End Class





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to make a calender ctrl harrysingh26 Classic ASP Basics 0 October 3rd, 2005 11:50 PM
\b in a Ctrl+F (Find) crmpicco Dreamweaver (all versions) 0 September 22nd, 2005 06:57 AM
Trouble with ADODC ctrl nav1 VB How-To 3 June 21st, 2005 09:19 AM
Seeing ctrl chars in C#/VS.NET? peri C# 0 December 6th, 2004 10:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.