Option Explicit
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL] VBA7 Then
Private Declare PtrSafe Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare PtrSafe Function SetTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr, ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr
Private Declare PtrSafe Function KillTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL] If
Public Sub HookArrowKeys()
SetTimer Application.hwnd, 0, 0, AddressOf WatchKeyState
End Sub
Public Sub UnHookArrowKeys()
KillTimer Application.hwnd, 0
End Sub
Private Sub WatchKeyState()
Dim vKey As Variant, vKeysArray As Variant, vKeysNames As Variant, i As Integer
vKeysArray = Array(vbKeyDown, vbKeyUp, vbKeyLeft, vbKeyRight, vbKeyLButton)
vKeysNames = Array("Down", "Up", "Left", "Right", "MouseClick")
For i = 0 To UBound(vKeysArray)
If GetAsyncKeyState(vKeysArray(i)) Then vKey = vKeysArray(i): Exit For
Next i
If vKey = vbKeyLButton Then KillTimer Application.hwnd, 0: Exit Sub
On Error Resume Next
vKey = vKeysNames(i)
i = GetAsyncKeyState(vbKeyLButton)
If NavigationKeyStateUp Then
KillTimer Application.hwnd, 0
Call OnKeyUpPseudoEvent(Selection, CStr(vKey))
End If
End Sub
Private Property Get NavigationKeyStateUp() As Boolean
NavigationKeyStateUp = GetAsyncKeyState(vbKeyDown) + GetAsyncKeyState(vbKeyUp) _
+ GetAsyncKeyState(vbKeyLeft) + GetAsyncKeyState(vbKeyRight) = 0
End Property
[B][COLOR=#008000]'=================================================================================
' PSEUDO-EVENT
'=================================================================================[/COLOR][/B]
Private Sub OnKeyUpPseudoEvent(ByVal Target As Range, ByVal vKey As String)
MsgBox "You Relased the '" & vKey & "' Key" & vbNewLine & "At cell : '" & Target.Address & "'"
End Sub
[B][COLOR=#008000]'==================================================================================[/COLOR][/B]