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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 1999-2000 by Florian Klaempfl
member of the Free Pascal development team
Event Handling unit for setting Keyboard and Mouse Handlers
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit WinEvent;
interface
{
We need this unit to implement keyboard and mouse,
because win32 uses only one message queue for mouse and key events
}
uses
Windows;
type
TEventProcedure = Procedure(var ir:INPUT_RECORD);
{ these procedures must be used to set the event handlers }
{ these doesn't do something, they signal only the }
{ the upper layer that an event occured, this event }
{ must be handled with Win32-API function by the upper }
{ layer }
Procedure SetMouseEventHandler(p : TEventProcedure);
Procedure SetKeyboardEventHandler(p : TEventProcedure);
Procedure SetFocusEventHandler(p : TEventProcedure);
Procedure SetMenuEventHandler(p : TEventProcedure);
Procedure SetResizeEventHandler(p : TEventProcedure);
Procedure SetUnknownEventHandler(p : TEventProcedure);
{ these procedures must be used to get the event handlers }
Function GetMouseEventHandler : TEventProcedure;
Function GetKeyboardEventHandler : TEventProcedure;
Function GetFocusEventHandler : TEventProcedure;
Function GetMenuEventHandler : TEventProcedure;
Function GetResizeEventHandler : TEventProcedure;
Function GetUnknownEventHandler : TEventProcedure;
implementation
const
{ these procedures are called if an event occurs }
MouseEventHandler : TEventProcedure = nil;
KeyboardEventHandler : TEventProcedure = nil;
FocusEventHandler : TEventProcedure = nil;
MenuEventHandler : TEventProcedure = nil;
ResizeEventHandler : TEventProcedure = nil;
UnknownEventHandler : TEventProcedure = nil;
{ if this counter is zero, the event handler thread is killed }
InstalledHandlers : Byte = 0;
var
HandlerChanging : TCriticalSection;
EventThreadHandle : Handle;
EventThreadID : DWord;
{ true, if the event handler should be stoped }
ExitEventHandleThread : boolean;
Function GetMouseEventHandler : TEventProcedure;
begin
GetMouseEventHandler:=MouseEventHandler;
end;
Function GetKeyboardEventHandler : TEventProcedure;
begin
GetKeyboardEventHandler:=KeyboardEventHandler;
end;
Function GetFocusEventHandler : TEventProcedure;
begin
GetFocusEventHandler:=FocusEventHandler;
end;
Function GetMenuEventHandler : TEventProcedure;
begin
GetMenuEventHandler:=MenuEventHandler;
end;
Function GetResizeEventHandler : TEventProcedure;
begin
GetResizeEventHandler:=ResizeEventHandler;
end;
Function GetUnknownEventHandler : TEventProcedure;
begin
GetUnknownEventHandler:=UnknownEventHandler;
end;
Function EventHandleThread(p : pointer) : DWord;StdCall;
const
irsize = 10;
var
ir : array[0..irsize-1] of TInputRecord;
i,dwRead : DWord;
begin
while not(ExitEventHandleThread) do
begin
{ wait for an event }
WaitForSingleObject(StdInputHandle,INFINITE);
{ guard this code, else it is doomed to crash, if the
thread is switched between the assigned test and
the call and the handler is removed
}
if not(ExitEventHandleThread) then
begin
if ReadConsoleInput(StdInputHandle,ir[0],irsize,dwRead) then
begin
i:=0;
EnterCriticalSection(HandlerChanging);
while i<dwRead do
begin
{ call the handler }
case ir[i].EventType of
KEY_EVENT:
begin
if assigned(KeyboardEventHandler) then
KeyboardEventHandler(ir[i]);
end;
_MOUSE_EVENT:
begin
if assigned(MouseEventHandler) then
MouseEventHandler(ir[i]);
end;
WINDOW_BUFFER_SIZE_EVENT:
begin
if assigned(ResizeEventHandler) then
ResizeEventHandler(ir[i]);
end;
MENU_EVENT:
begin
if assigned(MenuEventHandler) then
MenuEventHandler(ir[i]);
end;
FOCUS_EVENT:
begin
if assigned(FocusEventHandler) then
FocusEventHandler(ir[i]);
end;
else
begin
if assigned(UnknownEventHandler) then
UnknownEventHandler(ir[i]);
end;
end;
inc(i);
end;
LeaveCriticalSection(HandlerChanging);
end;
end;
end;
EventHandleThread:=0;
end;
Procedure NewEventHandlerInstalled(p,oldp : TEventProcedure);
var
oldcount : Byte;
ir : TInputRecord;
written : DWord;
begin
oldcount:=InstalledHandlers;
if Pointer(oldp)<>nil then
dec(InstalledHandlers);
if Pointer(p)<>nil then
inc(InstalledHandlers);
{ start event handler thread }
if (oldcount=0) and (InstalledHandlers=1) then
begin
ExitEventHandleThread:=false;
EventThreadHandle:=CreateThread(nil,0,@EventHandleThread,
nil,0,EventThreadID);
end
{ stop and destroy event handler thread }
else if (oldcount=1) and (InstalledHandlers=0) then
begin
ExitEventHandleThread:=true;
{ create a dummy event and sent it to the thread, so
we can leave WaitForSingleObject }
ir.EventType:=KEY_EVENT;
{ mouse event can be disabled by mouse.inc code
in DoneMouse
so use a key event instead PM }
WriteConsoleInput(StdInputHandle,ir,1,written);
{ wait, til the thread is ready }
WaitForSingleObject(EventThreadHandle,INFINITE);
CloseHandle(EventThreadHandle);
end;
end;
Procedure SetMouseEventHandler(p : TEventProcedure);
var
oldp : TEventProcedure;
begin
EnterCriticalSection(HandlerChanging);
oldp:=MouseEventHandler;
MouseEventHandler:=p;
LeaveCriticalSection(HandlerChanging);
NewEventHandlerInstalled(MouseEventHandler,oldp);
end;
Procedure SetKeyboardEventHandler(p : TEventProcedure);
var
oldp : TEventProcedure;
begin
EnterCriticalSection(HandlerChanging);
oldp:=KeyboardEventHandler;
KeyboardEventHandler:=p;
LeaveCriticalSection(HandlerChanging);
NewEventHandlerInstalled(KeyboardEventHandler,oldp);
end;
Procedure SetFocusEventHandler(p : TEventProcedure);
var
oldp : TEventProcedure;
begin
EnterCriticalSection(HandlerChanging);
oldp:=FocusEventHandler;
FocusEventHandler:=p;
LeaveCriticalSection(HandlerChanging);
NewEventHandlerInstalled(FocusEventHandler,oldp);
end;
Procedure SetMenuEventHandler(p : TEventProcedure);
var
oldp : TEventProcedure;
begin
EnterCriticalSection(HandlerChanging);
oldp:=MenuEventHandler;
MenuEventHandler:=p;
LeaveCriticalSection(HandlerChanging);
NewEventHandlerInstalled(MenuEventHandler,oldp);
end;
Procedure SetResizeEventHandler(p : TEventProcedure);
var
oldp : TEventProcedure;
begin
EnterCriticalSection(HandlerChanging);
oldp:=ResizeEventHandler;
ResizeEventHandler:=p;
LeaveCriticalSection(HandlerChanging);
NewEventHandlerInstalled(ResizeEventHandler,oldp);
end;
Procedure SetUnknownEventHandler(p : TEventProcedure);
var
oldp : TEventProcedure;
begin
EnterCriticalSection(HandlerChanging);
oldp:=UnknownEventHandler;
UnknownEventHandler:=p;
LeaveCriticalSection(HandlerChanging);
NewEventHandlerInstalled(UnknownEventHandler,oldp);
end;
initialization
InitializeCriticalSection(HandlerChanging);
finalization
{ Uninstall all handlers }
{ this stops also the event handler thread }
SetMouseEventHandler(nil);
SetKeyboardEventHandler(nil);
SetFocusEventHandler(nil);
SetMenuEventHandler(nil);
SetResizeEventHandler(nil);
SetUnknownEventHandler(nil);
{ delete the critical section object }
DeleteCriticalSection(HandlerChanging);
end.
|