Detect Key state in SelectionChange code

robw

Board Regular
Joined
Dec 18, 2002
Messages
161
Hi,

I have some VBA that responds to the SelectionChange event on a particular worksheet . Can I, within this code, detect whether a particular key (eg. the Ctrl key) was held down at the time of the selection change?.... I want to modify the path through the code accordingly.

Any help most welcome

Regards
Rob
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Adapted from Chip Pearson:

Code:
'General Module
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Long
Private Const KEY_MASK As Integer = &HFF80 ' decimal -128
Private Const VK_LCONTROL = &HA2
Private Const VK_RCONTROL = &HA3
Private Const VK_LCTRL = VK_LCONTROL
Private Const VK_RCTRL = VK_RCONTROL
Public Const BothLeftAndRightKeys = 0
Public Const LeftKey = 1
Public Const RightKey = 2
Public Const LeftKeyOrRightKey = 3
Public Function IsControlKeyDown(Optional LeftOrRightKey As Long = LeftKeyOrRightKey) As Boolean
    Dim Res As Long
    Select Case LeftOrRightKey
        Case LeftKey
            Res = GetKeyState(VK_LCTRL) And KEY_MASK
        Case RightKey
            Res = GetKeyState(VK_RCTRL) And KEY_MASK
        Case BothLeftAndRightKeys
            Res = (GetKeyState(VK_LCTRL) And GetKeyState(VK_RCTRL) And KEY_MASK)
        Case Else
            Res = GetKeyState(vbKeyControl) And KEY_MASK
    End Select
    IsControlKeyDown = CBool(Res)
End Function
 
'Worksheet Module
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If IsControlKeyDown(LeftKeyOrRightKey) Then
        MsgBox "You are holding down Ctrl"
    End If
End Sub

Link to original code:

http://www.cpearson.com/excel/keytest.aspx
 
Upvote 0
Andrew,

Brilliant !!!

That works just fine.

Also, it looks like I can detect the Left and Right Ctrl keys independently ...that's a bonus for which I will have to find an application :-)

Many Thanks for your help
Rob
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top