ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Reading the ShiftKey state (https://www.excelbanter.com/excel-programming/301328-reading-shiftkey-state.html)

mjclare[_5_]

Reading the ShiftKey state
 
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 ope
one of two possible dialog boxes. (Conceptually this is similar t
what happens when you hold down SHIFT while selecting Edit.) In m
case I wish to use the shift state to force it pop up a user form tha
can be used to modify settings that the user usually would not have an
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 thes
only fire when an actual keyboard (ANSI character) character flow
through the buffer.

Thanks,

Mik

--
Message posted from http://www.ExcelForum.com


Greg Wilson[_4_]

Reading the ShiftKey state
 
Try:

Public Declare Function GetKeyState _
Lib "user32" (ByVal NVirtKey As Long) As Integer
Public Const VK_SHIFT As Integer = &H10

Sub ReadShiftKey()
If GetKeyState(VK_SHIFT) < 0 Then _
MsgBox "Goodbye" Else MsgBox "Hello"
End Sub

Regards,
Greg

-----Original 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/

.


Tom Ogilvy

Reading the ShiftKey state
 
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/





All times are GMT +1. The time now is 12:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com