Haw can i test if CTRL is activated (hold down) in VBA

Discussion in 'Microsoft Excel Programming' started by Guest, Jul 30, 2006.

  1. Guest

    Guest Guest

    I wana use it in a event handle like

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    if not CTRL is pressed (hold down) then exit sub ???

    haw do i test for that ?

    tanks in advanse
     
    Guest, Jul 30, 2006
    #1
    1. Advertisements

  2. Guest

    Ron de Bruin Guest

    Hi excelent

    This in a normal module

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

    Const VK_CONTROL As Integer = &H11 'Ctrl


    Sub test()
    If GetKeyState(VK_CONTROL) < 0 Then Ctrl = True Else Ctrl = False
    If Ctrl = True Then
    MsgBox "pressed"
    Else
    MsgBox "Not"
    End If
    End Sub



    And this in the sheet module

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Call test
    End Sub




    --
    Regards Ron de Bruin
    http://www.rondebruin.nl



    "excelent" <> wrote in message news:D...
    >I wana use it in a event handle like
    >
    > Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    > if not CTRL is pressed (hold down) then exit sub ???
    >
    > haw do i test for that ?
    >
    > tanks in advanse
     
    Ron de Bruin, Jul 30, 2006
    #2
    1. Advertisements

  3. Guest

    Tom Ogilvy Guest

    Here is some code to illustrate - it isn't how I would implement it, but
    gives you the idea.

    Declare Function GetKeyState Lib "User32" _
    (ByVal vKey As Integer) As Integer

    Const SHIFT_KEY = 16
    Const CTRL_KEY = 17
    Const ALT_KEY = 18


    Sub find_key()


    'Checks key states to see if both CTRL and ALT Keys are pressed
    If GetKeyState(CTRL_KEY) < 0 And GetKeyState(ALT_KEY) < 0 Then
    MsgBox "CTRL + ALT KEYS PRESSED"
    'Checks key states to see if only CTRL key is pressed
    ElseIf GetKeyState(CTRL_KEY) < 0 Then
    MsgBox "CTRL KEY PRESSED"
    'Checks key states to see if only ALT key is pressed
    ElseIf GetKeyState(ALT_KEY) < 0 Then
    MsgBox "ALT KEY PRESSED"
    'Checks key states to see if only SHIFT key is pressed
    ElseIf GetKeyState(SHIFT_KEY) < 0 Then
    MsgBox "SHIFT KEY PRESSED"
    End If


    End Sub



    --

    Regards,

    Tom Ogilvy




    "excelent" <> wrote in message
    news:D...
    >I wana use it in a event handle like
    >
    > Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    > if not CTRL is pressed (hold down) then exit sub ???
    >
    > haw do i test for that ?
    >
    > tanks in advanse
     
    Tom Ogilvy, Jul 30, 2006
    #3
  4. Guest

    Guest Guest

    Great tanks Ron :)

    i wonder can i put this

    If GetKeyState(VK_CONTROL) < 0 Then Ctrl = True Else Ctrl = False
    If Ctrl = True Then
    MsgBox "pressed"
    Else
    MsgBox "Not"
    End If

    in the Sheet event-module, or do i have to call it like u show it ?

    and do i have to use the Declare Function too or is that just another
    way to do the same ?
     
    Guest, Jul 30, 2006
    #4
  5. Guest

    Tom Ogilvy Guest

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

    Const VK_CONTROL As Integer = &H11 'Ctrl


    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If GetKeyState(VK_CONTROL) < 0 Then Ctrl = True Else Ctrl = False
    If Ctrl = True Then
    MsgBox "pressed"
    Else
    MsgBox "Not"
    End If

    End Sub


    all in the sheet module worked fine for me.

    --
    Regards,
    Tom Ogilvy

    "excelent" <> wrote in message
    news:...
    > Great tanks Ron :)
    >
    > i wonder can i put this
    >
    > If GetKeyState(VK_CONTROL) < 0 Then Ctrl = True Else Ctrl = False
    > If Ctrl = True Then
    > MsgBox "pressed"
    > Else
    > MsgBox "Not"
    > End If
    >
    > in the Sheet event-module, or do i have to call it like u show it ?
    >
    > and do i have to use the Declare Function too or is that just another
    > way to do the same ?
    >
     
    Tom Ogilvy, Jul 30, 2006
    #5
  6. Guest

    Guest Guest

    ok tanks Tom now i think i got it
     
    Guest, Jul 30, 2006
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    Disabling Ctrl-PgUp and Ctrl-PgDn

    Guest, Oct 8, 2004, in forum: Microsoft Excel Programming
    Replies:
    3
    Views:
    679
    Guest
    Oct 9, 2004
  2. Guest

    CTRL+Page Up and CTRL+Page Down

    Guest, Jun 10, 2005, in forum: Microsoft Excel Programming
    Replies:
    2
    Views:
    226
    Guest
    Jun 10, 2005
  3. frenic

    Haw can I to create the numeric counter?

    frenic, Jul 21, 2005, in forum: Microsoft Excel Programming
    Replies:
    1
    Views:
    188
    R.VENKATARAMAN
    Jul 21, 2005
  4. excelent

    Haw to count ";" in a string

    excelent, Oct 27, 2009, in forum: Microsoft Excel Programming
    Replies:
    4
    Views:
    158
    Rick Rothstein
    Oct 27, 2009
  5. Robert Crandal

    Test if a Userform is loaded or activated??

    Robert Crandal, Dec 7, 2009, in forum: Microsoft Excel Programming
    Replies:
    6
    Views:
    398
    Peter T
    Dec 7, 2009
Loading...

Share This Page