A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Lon wrote:
Thanks for the comment about the key being released before the macro is called. However, this program will be meant only for my own use, and I will know to keep the key down long enough.
Okay ;-). See this article. However, my experiments suggest that the article might not be accurate, at least for WinXP, my platform.
Try the following test event macro, pressing and releasing control in a number of contexts. In addition to 0 ("not set") and &h80 ("set -128"), sometimes I get &h01 ("set 1") and &h81 ("set -127").
EDIT.... Well, if we limit ourselves to iResult And &h80 for VK_CONTROL, as this article states, it reliably tells us if the key is up or down. It is my nature to question documentation to ferret out undocumented nuances, especially for kernel features. So I'm curious what these other states might tell us, if anything. But the other bits might simply be "garbage". TBD.
Caveat: try this with a new worksheet. It destroys column A.
Private Declare Function GetKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
Private Const VK_SHIFT As Long = &H10
Private Const VK_CONTROL As Long = &H11
Private Const VK_MENU As Long = &H12
Private Const VK_CAPITAL = &H14
Private Const VK_NUMLOCK = &H90
Private Const VK_SCROLL = &H91
'initialize by resetting VBA; click Run > Reset
Private n As Long
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim iResult As Integer
If n = 0 Then Range("a:a").Clear: n = 1
iResult = GetKeyState(VK_CONTROL) And &H80 'EDITed
'note: iResult < 0 means bit 15 is set
If iResult <> 0 Then Cells(n, 1) = "set " & iResult _
Else Cells(n, 1) = "not set"
n = n + 1
End Sub