Easy Way to Check for 'Shift/Alt/Ctrl' keys

hicksi

Board Regular
Joined
Mar 5, 2012
Messages
203
I needed to provide multiple uses for Command-Buttons in an Excel UserForm.
Ie, to Preview a report (click); to Print the report without preview (Alt-Click); to Save the report to file (Control-click)

The following code was developed to illustrate the options, so I post it here. Use it as you wish...

Using a form that looks like this:
ShiftKey Tester.jpg


I have the behind-code

VBA Code:
Option Explicit
Dim tState          As ShiftKeys
Dim isDblClick      As Boolean

Private Sub CommandButton1_Click()
    Call showKeySelection
    Label1.Caption = tState
    isDblClick = False
End Sub

Private Sub CommandButton1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    isDblClick = True
    Call CommandButton1_Click
End Sub

Private Sub showKeySelection()
    tState = CheckKeyState
    CheckBoxLS.Value = tState And KeyLeftShift
    CheckBoxRS.Value = tState And KeyRightShift
'    CheckBoxS.Value = tState And (KeyLeftShift + KeyRightShift)
    CheckBoxS.Value = tState And KeyShift
    CheckBoxLC.Value = tState And KeyLeftCtrl
    CheckBoxRC.Value = tState And KeyRightCtrl
'    CheckBoxC.Value = tState And (KeyLeftCtrl + KeyRightCtrl)
    CheckBoxC.Value = tState And KeyCtrl
    CheckBoxLA.Value = tState And KeyLeftAlt
    CheckBoxRA.Value = tState And KeyRightAlt
'    CheckBoxA.Value = tState And (KeyLeftAlt + KeyRightAlt)
    CheckBoxA.Value = tState And KeyAlt
'
    CheckBoxDblClick.Value = isDblClick
End Sub

which uses the single common module code of:

VBA Code:
Option Explicit

'   This routine demonstrates the tests for 'shift' key options when you click a button
'   It determines whether you have pressed any of the Shift, Alt or Ctrl keys as well
'
' Based on information gleaned from 'http://www.cpearson.com/excel/keytest.aspx'
'
'==========================================================================================

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    
Public Enum ShiftKeys
    KeyLeftShift = 1
    KeyRightShift = 2
    KeyLeftCtrl = 4
    KeyRightCtrl = 8
    KeyLeftAlt = 16
    KeyRightAlt = 32
    KeyShift = 64       ' Common ones aren't really necessary,
    KeyCtrl = 128       '   but are included for completeness.
    KeyAlt = 256        '-------------------------------------
End Enum

Private Const VK_LSHIFT     As Long = &HA0  ' 160
Private Const VK_RSHIFT     As Long = &HA1  ' 161
Private Const VK_LCTRL      As Long = &HA2  ' 162
Private Const VK_RCTRL      As Long = &HA3  ' 163
Private Const VK_LALT       As Long = &HA4  ' 164
Private Const VK_RALT       As Long = &HA5  ' 165
'
'             vbKeyShift            = &H10  '  16
'             vbKeyControl          = &H11  '  17
'             vbKeyMenu             = &H12  '  18
'------------------------------------------------

Public Function CheckKeyState() As ShiftKeys
    CheckKeyState = 0
    If GetKeyState(VK_LSHIFT) < 0 Then CheckKeyState = CheckKeyState + KeyLeftShift
    If GetKeyState(VK_RSHIFT) < 0 Then CheckKeyState = CheckKeyState + KeyRightShift
    If GetKeyState(VK_LCTRL) < 0 Then CheckKeyState = CheckKeyState + KeyLeftCtrl
    If GetKeyState(VK_RCTRL) < 0 Then CheckKeyState = CheckKeyState + KeyRightCtrl
    If GetKeyState(VK_LALT) < 0 Then CheckKeyState = CheckKeyState + KeyLeftAlt
    If GetKeyState(VK_RALT) < 0 Then CheckKeyState = CheckKeyState + KeyRightAlt
'=====
    If GetKeyState(vbKeyShift) < 0 Then CheckKeyState = CheckKeyState + KeyShift
    If GetKeyState(vbKeyControl) < 0 Then CheckKeyState = CheckKeyState + KeyCtrl
    If GetKeyState(vbKeyMenu) < 0 Then CheckKeyState = CheckKeyState + KeyAlt
End Function
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
This is great - thank you kindly for sharing.
 
Upvote 0

Forum statistics

Threads
1,215,772
Messages
6,126,811
Members
449,339
Latest member
Cap N

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