Key down event ?

ChrisUK

Well-known Member
Joined
Sep 3, 2002
Messages
675
Hi.

Is there a way of checking if the shift or ctrl key is pressed at all ?

I've written a very simple worksheet event which responds to cell selection changes, I want to check if the shift or ctrl key is depressed when executing this event.

Any ideas please ?

TIA

Chris
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Yes you can do this. The thing with keyboards is, they are not unique to Excel, so code declarations at a level higher than VBA is required for detection, such as Windows API.

In a standard module, enter this:



Option Explicit

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Const VK_SHIFT = &H10
Private Const KEY_DOWN As Long = &H1000

Sub ShiftKey()
If GetKeyState(VK_SHIFT) = True And KEY_DOWN = True Then
MsgBox "Shift key is pressed down."
Else
MsgBox "Shift key is not pressed down."
End If
End Sub



Now if you run the ShiftKey macro while simultaneously pressing the Shift key or not pressing the Shift key, the message box will display the appropriate situation.
 
Upvote 0
A similar approach as Tom Urtis's post.

In a standard module:

Code:
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
And since, you asked about the Worksheet SelectionChange event:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If GetKeyState(vbKeyShift) < 0 And GetKeyState(vbKeyControl) < 0 Then
    MsgBox "CTRL-SHIFT pressed"
Else
    If GetKeyState(vbKeyShift) < 0 Then MsgBox "Shift Pressed"
    If GetKeyState(vbKeyControl) < 0 Then MsgBox "Ctrl Pressed"
End If
End Sub
 
Upvote 0
Thought I'd throw this in too, to check for the Alt key as well if you're interested. Plus, I didn't include the Ctrl key in my first post and see you wanted a Selection Change event associated with it.

This approach gives you the best of both worlds, where you can call the macro from the Selection Change, or use it for other purposes if you want to evaluate the key situation when not selecting a cell.

In the worksheet module:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Run "KeyCheck"
End Sub



In a standard module:


Option Explicit

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Const VK_CONTROL = &H11
Private Const VK_SHIFT = &H10
Private Const VK_MENU = &H12

Sub KeyCheck()

'Ctrl
If GetAsyncKeyState(VK_CONTROL) < 0 Then
MsgBox "Control is pressed."
Else
MsgBox "Control is not pressed."
End If

'Shift
If GetAsyncKeyState(VK_SHIFT) < 0 Then
MsgBox "Shift is pressed."
Else
MsgBox "Shift is not pressed."
End If

'Alt
If GetAsyncKeyState(VK_MENU) < 0 Then
MsgBox "Alt is pressed."
Else
MsgBox "Alt is not pressed."
End If

End Sub



Here are the other key codes...if anyone sees a mistake in this please let me know but I think it's correct.


' Virtual Keys, Standard Set
Public Const VK_LBUTTON = &H1
Public Const VK_RBUTTON = &H2
Public Const VK_CANCEL = &H3
Public Const VK_MBUTTON = &H4

Public Const VK_BACK = &H8
Public Const VK_TAB = &H9

Public Const VK_CLEAR = &HC
Public Const VK_RETURN = &HD

Public Const VK_SHIFT = &H10
Public Const VK_CONTROL = &H11
Public Const VK_MENU = &H12
Public Const VK_PAUSE = &H13
Public Const VK_CAPITAL = &H14

Public Const VK_ESCAPE = &H1B

Public Const VK_SPACE = &H20
Public Const VK_PRIOR = &H21
Public Const VK_NEXT = &H22
Public Const VK_END = &H23
Public Const VK_HOME = &H24
Public Const VK_LEFT = &H25
Public Const VK_UP = &H26
Public Const VK_RIGHT = &H27
Public Const VK_DOWN = &H28
Public Const VK_SELECT = &H29
Public Const VK_PRINT = &H2A
Public Const VK_EXECUTE = &H2B
Public Const VK_SNAPSHOT = &H2C
Public Const VK_INSERT = &H2D
Public Const VK_DELETE = &H2E
Public Const VK_HELP = &H2F

' VK_A thru VK_Z are the same as their ASCII equivalents: 'A' thru 'Z'
' VK_0 thru VK_9 are the same as their ASCII equivalents: '0' thru '9'

Public Const VK_NUMPAD0 = &H60
Public Const VK_NUMPAD1 = &H61
Public Const VK_NUMPAD2 = &H62
Public Const VK_NUMPAD3 = &H63
Public Const VK_NUMPAD4 = &H64
Public Const VK_NUMPAD5 = &H65
Public Const VK_NUMPAD6 = &H66
Public Const VK_NUMPAD7 = &H67
Public Const VK_NUMPAD8 = &H68
Public Const VK_NUMPAD9 = &H69
Public Const VK_MULTIPLY = &H6A
Public Const VK_ADD = &H6B
Public Const VK_SEPARATOR = &H6C
Public Const VK_SUBTRACT = &H6D
Public Const VK_DECIMAL = &H6E
Public Const VK_DIVIDE = &H6F
Public Const VK_F1 = &H70
Public Const VK_F2 = &H71
Public Const VK_F3 = &H72
Public Const VK_F4 = &H73
Public Const VK_F5 = &H74
Public Const VK_F6 = &H75
Public Const VK_F7 = &H76
Public Const VK_F8 = &H77
Public Const VK_F9 = &H78
Public Const VK_F10 = &H79
Public Const VK_F11 = &H7A
Public Const VK_F12 = &H7B
Public Const VK_F13 = &H7C
Public Const VK_F14 = &H7D
Public Const VK_F15 = &H7E
Public Const VK_F16 = &H7F
Public Const VK_F17 = &H80
Public Const VK_F18 = &H81
Public Const VK_F19 = &H82
Public Const VK_F20 = &H83
Public Const VK_F21 = &H84
Public Const VK_F22 = &H85
Public Const VK_F23 = &H86
Public Const VK_F24 = &H87

Public Const VK_NUMLOCK = &H90
Public Const VK_SCROLL = &H91

' VK_L VK_R - left and right Alt, Ctrl and Shift virtual keys.
' Used only as parameters to GetAsyncKeyState() and GetKeyState().
' No other API or message will distinguish left and right keys in this way.

Public Const VK_LSHIFT = &HA0
Public Const VK_RSHIFT = &HA1
Public Const VK_LCONTROL = &HA2
Public Const VK_RCONTROL = &HA3
Public Const VK_LMENU = &HA4
Public Const VK_RMENU = &HA5

Public Const VK_ATTN = &HF6
Public Const VK_CRSEL = &HF7
Public Const VK_EXSEL = &HF8
Public Const VK_EREOF = &HF9
Public Const VK_PLAY = &HFA
Public Const VK_ZOOM = &HFB
Public Const VK_NONAME = &HFC
Public Const VK_PA1 = &HFD
Public Const VK_OEM_CLEAR = &HFE
 
Upvote 0

Forum statistics

Threads
1,215,522
Messages
6,125,312
Members
449,218
Latest member
Excel Master

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