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
Perform the check at the beginning of your macro. It checks what the key
state is at that time (not when the menu was clicked), but I believe it is
about the best you can do without subclassing Excel.
--
Regards,
Tom Ogilvy
"mjclare " wrote in message
...
Is it possible to read the ShiftState from within VBA?
My idea is to create a new menu item in the Data menu and have it open
one of two possible dialog boxes. (Conceptually this is similar to
what happens when you hold down SHIFT while selecting Edit.) In my
case I wish to use the shift state to force it pop up a user form that
can be used to modify settings that the user usually would not have any
need to access.
i.e.
Sub data_menu_new_1()
'
'
if ShiftState() Then
UserForm1.Show
else
UserForm2.Show
End If
End Sub
I've played around with KeyDown and KeyPress but it appears that these
only fire when an actual keyboard (ANSI character) character flows
through the buffer.
Thanks,
Mike
---
Message posted from http://www.ExcelForum.com/