-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmod_Hotkeys.bas
180 lines (133 loc) · 5.02 KB
/
mod_Hotkeys.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
Attribute VB_Name = "mod_Hotkeys"
Option Explicit
Private Type KBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" ( _
ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" ( _
ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
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 uIDEvent 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 WH_KEYBOARD_LL = 13
Private Const HC_ACTION = 0
Private Const GWL_HINSTANCE = (-6)
Private Const VK_SNAPSHOT = &H2C
Private blnHookEnabled As Boolean
Private hhkLowLevelKybd As Long
Private lWBhwnd As Long
Private lTimerID As Long
Private oNewApp As Application
'-------------------
'Public routines .
'-------------------
Public Sub SetHook()
Dim owb As Workbook
'Create an invisible xl instance
'to run the keyboard hook callback.
'this is to avoid accidental VBIDE crash.
Set oNewApp = New Application
oNewApp.EnableEvents = False
Set owb = oNewApp.Workbooks.Open(ThisWorkbook.FullName, ReadOnly:=True)
oNewApp.Run "Hook_KeyBoard", ThisWorkbook.FullName
End Sub
Public Sub RemoveHook()
'Proper cleanup.
If Not oNewApp Is Nothing Then
oNewApp.Run "Unhook_KeyBoard"
oNewApp.Quit
End If
End Sub
Public Sub Hook_KeyBoard(WbPathName As String)
Dim lXLhwnd, lXLHinstance As Long
Dim owb As Workbook
'get a temp pointer to calling WB.
Set owb = GetObject(WbPathName)
'store the hwnd of the calling WB.
lWBhwnd = FindWindowEx(owb.Parent.hwnd, 0, "XLDESK", vbNullString)
lWBhwnd = FindWindowEx(lWBhwnd, 0, "EXCEL7", owb.Name)
'release ref to calling WB.
Set owb = Nothing
'hook the keyboard system wide.
lXLhwnd = FindWindow("XLMAIN", Application.Caption)
lXLHinstance = GetWindowLong(lXLhwnd, GWL_HINSTANCE)
If blnHookEnabled = False Then
hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, lXLHinstance, 0)
blnHookEnabled = True
'unhook the keyboard in case it wasn't properly
'unhooked in the Before_Close event.
lTimerID = SetTimer(0, 0, 1, AddressOf MonitorWorkBook)
End If
End Sub
Public Sub Unhook_KeyBoard()
If hhkLowLevelKybd <> 0 Then UnhookWindowsHookEx hhkLowLevelKybd
hhkLowLevelKybd = 0
blnHookEnabled = False
End Sub
'------------------
'Private routines.
'------------------
Private Function LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByRef lParam As KBDLLHOOKSTRUCT) As Long
If nCode = HC_ACTION Then
'never process the Print Key.
If lParam.vkCode = VK_SNAPSHOT Then
LowLevelKeyboardProc = -1
SetTimer FindWindow("XLMAIN", Application.Caption), 0, 1, AddressOf ClearTheClipboard
Exit Function
End If
End If
LowLevelKeyboardProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
End Function
Private Sub ClearTheClipboard()
'display msgbox only once.
KillTimer FindWindow("XLMAIN", Application.Caption), 0
If FindWindowEx(0, 0, "#32770", "Microsoft Excel") Then Exit Sub
MsgBox "The Print Key is disabled.", vbSystemModal + vbInformation
'Optionally clear the clipboard.
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
'run any other code here...
End Sub
Private Sub MonitorWorkBook()
'safety net in case the calling WB didn't
'unhook the keyboard before closing.
If IsWindow(lWBhwnd) = 0 Then
DoEvents
KillTimer 0, lTimerID
Unhook_KeyBoard
Application.Quit
End If
End Sub