This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
ptccrt.pp
418 lines (386 loc) · 13.4 KB
/
ptccrt.pp
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
{
This file is part of the Free Pascal run time library.
Copyright (c) 2010, 2011, 2013 by Nikolay Nikolov (nickysn@users.sourceforge.net)
This file implements keyboard input support for ptcgraph
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 ptccrt;
{$MODE objfpc}
{$DEFINE HasCRT}
{$IFDEF WinCE}
{$UNDEF HasCRT}
{$ENDIF WinCE}
interface
{$IFDEF HasCRT}
uses
crt;
{$ENDIF HasCRT}
type
{$IFDEF HasCRT}
tcrtcoord = crt.tcrtcoord;
{$ELSE HasCRT}
tcrtcoord = 1..255;
{$ENDIF HasCRT}
var
DirectVideo: Boolean {$IFDEF HasCRT}absolute crt.DirectVideo{$ENDIF HasCRT};
TextAttr: Byte {$IFDEF HasCRT}absolute crt.TextAttr{$ENDIF HasCRT};
function KeyPressed: Boolean;
function ReadKey: Char;
procedure ClrScr;
procedure ClrEol;
procedure GotoXY(X, Y: tcrtcoord);
procedure TextColor(Color: Byte);
procedure TextBackground(Color: Byte);
procedure Delay(MS: Word);
procedure Sound(HZ: Word);
procedure NoSound;
implementation
uses
ptcgraph, ptc, ptcwrapper
{$IFDEF UNIX}
, baseunix
{$ENDIF UNIX}
{$IF defined(Win32) or defined(Win64) or defined(WinCE)}
, windows
{$ENDIF defined(Win32) or defined(Win64) or defined(WinCE)}
;
function InGraphMode: Boolean;
begin
Result := (PTCWrapperObject <> nil) and (PTCWrapperObject.IsOpen);
end;
var
KeyBuffer: array[0..64] of Char;
KeyBufHead, KeyBufTail: Integer;
function KeyBufEmpty: Boolean;
begin
Result := KeyBufHead = KeyBufTail;
end;
procedure KeyBufAdd(Ch: Char);
begin
{ do nothing, if the buffer is full }
if ((KeyBufTail + 1) = KeyBufHead) or
((KeyBufTail = High(KeyBuffer)) and (KeyBufHead = Low(KeyBuffer))) then
exit;
KeyBuffer[KeyBufTail] := Ch;
Inc(KeyBufTail);
if KeyBufTail > High(KeyBuffer) then
KeyBufTail := Low(KeyBuffer);
end;
procedure KeyBufAdd(S: String);
var
I: Integer;
begin
for I := 1 to Length(S) do
KeyBufAdd(S[I]);
end;
function KeyBufGet: Char;
begin
if KeyBufHead <> KeyBufTail then
begin
Result := KeyBuffer[KeyBufHead];
Inc(KeyBufHead);
if KeyBufHead > High(KeyBuffer) then
KeyBufHead := Low(KeyBuffer);
end;
end;
procedure GetKeyEvents;
var
ev: IPTCEvent;
KeyEv: IPTCKeyEvent;
begin
repeat
PTCWrapperObject.NextEvent(ev, False, [PTCKeyEvent, PTCCloseEvent]);
if ev <> nil then
begin
case ev.EventType of
PTCCloseEvent:
begin
{ emulate Ctrl-C/Ctrl-Break, when the user
presses the [X] button to close the window }
KeyBufAdd(#3);
end;
PTCKeyEvent:
begin
KeyEv := ev as IPTCKeyEvent;
if KeyEv.Press then
begin
if KeyEv.Alt then
begin
case KeyEv.Code of
PTCKEY_F1: KeyBufAdd(#0#104);
PTCKEY_F2: KeyBufAdd(#0#105);
PTCKEY_F3: KeyBufAdd(#0#106);
PTCKEY_F4: KeyBufAdd(#0#107);
PTCKEY_F5: KeyBufAdd(#0#108);
PTCKEY_F6: KeyBufAdd(#0#109);
PTCKEY_F7: KeyBufAdd(#0#110);
PTCKEY_F8: KeyBufAdd(#0#111);
PTCKEY_F9: KeyBufAdd(#0#112);
PTCKEY_F10: KeyBufAdd(#0#113);
PTCKEY_ONE: KeyBufAdd(#0#120);
PTCKEY_TWO: KeyBufAdd(#0#121);
PTCKEY_THREE: KeyBufAdd(#0#122);
PTCKEY_FOUR: KeyBufAdd(#0#123);
PTCKEY_FIVE: KeyBufAdd(#0#124);
PTCKEY_SIX: KeyBufAdd(#0#125);
PTCKEY_SEVEN: KeyBufAdd(#0#126);
PTCKEY_EIGHT: KeyBufAdd(#0#127);
PTCKEY_NINE: KeyBufAdd(#0#128);
PTCKEY_ZERO: KeyBufAdd(#0#129);
PTCKEY_MINUS: KeyBufAdd(#0#130);
PTCKEY_EQUALS: KeyBufAdd(#0#131);
PTCKEY_Q: KeyBufAdd(#0#16);
PTCKEY_W: KeyBufAdd(#0#17);
PTCKEY_E: KeyBufAdd(#0#18);
PTCKEY_R: KeyBufAdd(#0#19);
PTCKEY_T: KeyBufAdd(#0#20);
PTCKEY_Y: KeyBufAdd(#0#21);
PTCKEY_U: KeyBufAdd(#0#22);
PTCKEY_I: KeyBufAdd(#0#23);
PTCKEY_O: KeyBufAdd(#0#24);
PTCKEY_P: KeyBufAdd(#0#25);
PTCKEY_A: KeyBufAdd(#0#30);
PTCKEY_S: KeyBufAdd(#0#31);
PTCKEY_D: KeyBufAdd(#0#32);
PTCKEY_F: KeyBufAdd(#0#33);
PTCKEY_G: KeyBufAdd(#0#34);
PTCKEY_H: KeyBufAdd(#0#35);
PTCKEY_J: KeyBufAdd(#0#36);
PTCKEY_K: KeyBufAdd(#0#37);
PTCKEY_L: KeyBufAdd(#0#38);
PTCKEY_Z: KeyBufAdd(#0#44);
PTCKEY_X: KeyBufAdd(#0#45);
PTCKEY_C: KeyBufAdd(#0#46);
PTCKEY_V: KeyBufAdd(#0#47);
PTCKEY_B: KeyBufAdd(#0#48);
PTCKEY_N: KeyBufAdd(#0#49);
PTCKEY_M: KeyBufAdd(#0#50);
end;
end
else
if KeyEv.Control then
begin
case KeyEv.Code of
PTCKEY_ESCAPE: KeyBufAdd(#27);
PTCKEY_F1: KeyBufAdd(#0#94);
PTCKEY_F2: KeyBufAdd(#0#95);
PTCKEY_F3: KeyBufAdd(#0#96);
PTCKEY_F4: KeyBufAdd(#0#97);
PTCKEY_F5: KeyBufAdd(#0#98);
PTCKEY_F6: KeyBufAdd(#0#99);
PTCKEY_F7: KeyBufAdd(#0#100);
PTCKEY_F8: KeyBufAdd(#0#101);
PTCKEY_F9: KeyBufAdd(#0#102);
PTCKEY_F10: KeyBufAdd(#0#103);
PTCKEY_TWO: KeyBufAdd(#0#3);
PTCKEY_BACKSPACE: KeyBufAdd(#127);
PTCKEY_A: KeyBufAdd(#1);
PTCKEY_B: KeyBufAdd(#2);
PTCKEY_C: KeyBufAdd(#3);
PTCKEY_D: KeyBufAdd(#4);
PTCKEY_E: KeyBufAdd(#5);
PTCKEY_F: KeyBufAdd(#6);
PTCKEY_G: KeyBufAdd(#7);
PTCKEY_H: KeyBufAdd(#8);
PTCKEY_I: KeyBufAdd(#9);
PTCKEY_J: KeyBufAdd(#10);
PTCKEY_K: KeyBufAdd(#11);
PTCKEY_L: KeyBufAdd(#12);
PTCKEY_M: KeyBufAdd(#13);
PTCKEY_N: KeyBufAdd(#14);
PTCKEY_O: KeyBufAdd(#15);
PTCKEY_P: KeyBufAdd(#16);
PTCKEY_Q: KeyBufAdd(#17);
PTCKEY_R: KeyBufAdd(#18);
PTCKEY_S: KeyBufAdd(#19);
PTCKEY_T: KeyBufAdd(#20);
PTCKEY_U: KeyBufAdd(#21);
PTCKEY_V: KeyBufAdd(#22);
PTCKEY_W: KeyBufAdd(#23);
PTCKEY_X: KeyBufAdd(#24);
PTCKEY_Y: KeyBufAdd(#25);
PTCKEY_Z: KeyBufAdd(#26);
PTCKEY_OPENBRACKET: KeyBufAdd(#27);
PTCKEY_BACKSLASH: KeyBufAdd(#28);
PTCKEY_CLOSEBRACKET: KeyBufAdd(#29);
PTCKEY_SIX: KeyBufAdd(#30);
PTCKEY_MINUS: KeyBufAdd(#31);
PTCKEY_ENTER: KeyBufAdd(#10);
PTCKEY_LEFT: KeyBufAdd(#0#115);
PTCKEY_RIGHT: KeyBufAdd(#0#116);
PTCKEY_HOME: KeyBufAdd(#0#119);
PTCKEY_END: KeyBufAdd(#0#117);
PTCKEY_PAGEUP: KeyBufAdd(#0#132);
PTCKEY_PAGEDOWN: KeyBufAdd(#0#118);
end;
end
else
if KeyEv.Shift then
begin
case KeyEv.Code of
PTCKEY_ESCAPE: KeyBufAdd(#27);
PTCKEY_F1: KeyBufAdd(#0#84);
PTCKEY_F2: KeyBufAdd(#0#85);
PTCKEY_F3: KeyBufAdd(#0#86);
PTCKEY_F4: KeyBufAdd(#0#87);
PTCKEY_F5: KeyBufAdd(#0#88);
PTCKEY_F6: KeyBufAdd(#0#89);
PTCKEY_F7: KeyBufAdd(#0#90);
PTCKEY_F8: KeyBufAdd(#0#91);
PTCKEY_F9: KeyBufAdd(#0#92);
PTCKEY_F10: KeyBufAdd(#0#93);
PTCKEY_BACKSPACE: KeyBufAdd(#8);
PTCKEY_TAB: KeyBufAdd(#0#15);
PTCKEY_ENTER: KeyBufAdd(#13);
PTCKEY_INSERT: KeyBufAdd(#0#82);
PTCKEY_DELETE: KeyBufAdd(#0#83);
PTCKEY_LEFT: KeyBufAdd(#0#75);
PTCKEY_UP: KeyBufAdd(#0#72);
PTCKEY_RIGHT: KeyBufAdd(#0#77);
PTCKEY_DOWN: KeyBufAdd(#0#80);
PTCKEY_HOME: KeyBufAdd(#0#71);
PTCKEY_END: KeyBufAdd(#0#79);
PTCKEY_PAGEUP: KeyBufAdd(#0#73);
PTCKEY_PAGEDOWN: KeyBufAdd(#0#81);
else
if (KeyEv.Unicode >= 32) and (KeyEv.Unicode <= 127) then
KeyBufAdd(Chr(KeyEv.Unicode));
end;
end
else
begin
case KeyEv.Code of
PTCKEY_ESCAPE: KeyBufAdd(#27);
PTCKEY_F1: KeyBufAdd(#0#59);
PTCKEY_F2: KeyBufAdd(#0#60);
PTCKEY_F3: KeyBufAdd(#0#61);
PTCKEY_F4: KeyBufAdd(#0#62);
PTCKEY_F5: KeyBufAdd(#0#63);
PTCKEY_F6: KeyBufAdd(#0#64);
PTCKEY_F7: KeyBufAdd(#0#65);
PTCKEY_F8: KeyBufAdd(#0#66);
PTCKEY_F9: KeyBufAdd(#0#67);
PTCKEY_F10: KeyBufAdd(#0#68);
PTCKEY_BACKSPACE: KeyBufAdd(#8);
PTCKEY_TAB: KeyBufAdd(#9);
PTCKEY_ENTER: KeyBufAdd(#13);
PTCKEY_INSERT: KeyBufAdd(#0#82);
PTCKEY_DELETE: KeyBufAdd(#0#83);
PTCKEY_LEFT: KeyBufAdd(#0#75);
PTCKEY_UP: KeyBufAdd(#0#72);
PTCKEY_RIGHT: KeyBufAdd(#0#77);
PTCKEY_DOWN: KeyBufAdd(#0#80);
PTCKEY_HOME: KeyBufAdd(#0#71);
PTCKEY_END: KeyBufAdd(#0#79);
PTCKEY_PAGEUP: KeyBufAdd(#0#73);
PTCKEY_PAGEDOWN: KeyBufAdd(#0#81);
else
if (KeyEv.Unicode >= 32) and (KeyEv.Unicode <= 127) then
KeyBufAdd(Chr(KeyEv.Unicode));
end;
end;
end;
end;
end;
end;
until ev = nil;
end;
function KeyPressed: Boolean;
begin
if not InGraphMode then
begin
{$IFDEF HasCRT}
Result := crt.KeyPressed
{$ELSE HasCRT}
Result := False;
{$ENDIF HasCRT}
end
else
begin
GetKeyEvents;
Result := not KeyBufEmpty;
end;
end;
function ReadKey: Char;
{$IFDEF UNIX}
var
req, rem: TTimeSpec;
{$ENDIF UNIX}
begin
if not InGraphMode then
begin
{$IFDEF HasCRT}
Result := crt.ReadKey;
{$ELSE HasCRT}
Result := #0;
{$ENDIF HasCRT}
end
else
begin
while not KeyPressed do
begin
{$IFDEF UNIX}
req.tv_sec := 0;
req.tv_nsec := 1000000;
fpnanosleep(@req, @rem);
{$ENDIF UNIX}
{$IF defined(Win32) or defined(Win64) or defined(WinCE)}
Sleep(1);
{$ENDIF defined(Win32) or defined(Win64) or defined(WinCE)}
end;
Result := KeyBufGet;
end;
end;
procedure ClrScr;
begin
{$IFDEF HasCRT}
crt.ClrScr;
{$ENDIF HasCRT}
end;
procedure ClrEol;
begin
{$IFDEF HasCRT}
crt.ClrEol;
{$ENDIF HasCRT}
end;
procedure GotoXY(X, Y: tcrtcoord);
begin
{$IFDEF HasCRT}
crt.GotoXY(X, Y);
{$ENDIF HasCRT}
end;
procedure TextColor(Color: Byte);
begin
{$IFDEF HasCRT}
crt.TextColor(Color);
{$ENDIF HasCRT}
end;
procedure TextBackground(Color: Byte);
begin
{$IFDEF HasCRT}
crt.TextBackground(Color);
{$ENDIF HasCRT}
end;
procedure Delay(MS: Word);
begin
{$IFDEF HasCRT}
crt.Delay(MS);
{$ENDIF HasCRT}
end;
procedure Sound(HZ: Word);
begin
{$IFDEF HasCRT}
crt.Sound(HZ);
{$ENDIF HasCRT}
end;
procedure NoSound;
begin
{$IFDEF HasCRT}
crt.NoSound;
{$ENDIF HasCRT}
end;
end.