Option Explicit
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type Msg
hwnd As Long
message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Private Declare Function RegisterHotKey Lib "user32" _
(ByVal hwnd As Long, _
ByVal id As Long, _
ByVal fsModifiers As Long, _
ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" _
(ByVal hwnd As Long, _
ByVal id As Long) As Long
Private Declare Function GetMessage Lib "user32" _
Alias "GetMessageA" _
(lpMsg As Msg, _
ByVal hwnd As Long, _
ByVal wMsgFilterMin As Long, _
ByVal wMsgFilterMax As Long) As Long
Private Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Declare Function GetProp Lib "user32" Alias "GetPropA" _
(ByVal hwnd As Long, _
ByVal lpString As String) As Long
Private Declare Function SetProp Lib "user32" Alias "SetPropA" _
(ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal hData As Long) As Long
Private Declare Function OpenClipboard Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Const WM_HOTKEY = &H312
Private bHotKeyRemoved As Boolean
Private bLoopRunning As Boolean
Sub test()
Call SetHotKeyToCopyAndPasteActiveWindows(VBA.vbKeyF10, ActiveSheet.Range("B2"))
End Sub
Sub RemoveHotKey()
bHotKeyRemoved = True
End Sub
Private Sub SetHotKeyToCopyAndPasteActiveWindows _
(ByVal HotKey As Long, ByVal PasteWhere As Range)
Dim tMsg As Msg
Dim lHotKeyID As Long
If bLoopRunning Then Exit Sub
bHotKeyRemoved = False
Call UnregisterHotKey(0, GetProp(Application.hwnd, "HotKeyID"))
lHotKeyID = 1
SetProp Application.hwnd, "HotKeyID", lHotKeyID
If RegisterHotKey(0, lHotKeyID, 0&, HotKey) <> 0 Then
bLoopRunning = True
Do While GetMessage(tMsg, 0, 0, 0) And bHotKeyRemoved = False
bLoopRunning = True
With tMsg
If .message = WM_HOTKEY And .hwnd <> Application.hwnd Then
OpenClipboard 0&
EmptyClipboard
CloseClipboard
Application.SendKeys "(%{1068})", True
On Error Resume Next
PasteWhere.Parent.Paste PasteWhere
On Error GoTo 0
End If
PostMessage .hwnd, .message, .wParam, .lParam
End With
DoEvents
Loop
End If
bLoopRunning = False
Call UnregisterHotKey(0, GetProp(Application.hwnd, "HotKeyID"))
End Sub