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
|
unit KeyTable;
{$I-,V-,X+}
interface
uses Dos, Drivers, Views, Editors;
const
N = 33;
evInvalidKey = 1024; { entspricht /$2^{10}$/ }
edNoKeyTab = 11;
kbBlank = $3920;
function ReadKeyTable: boolean;
function EditKeyTable: boolean;
procedure ShowKeyTable;
procedure GetComm (var Pos: Word; var E: TEvent);
implementation
var T: array [0..199] of record
k1,k2,cm: word
end;
top: integer;
ShiftState: byte absolute $40:$17;
const Commands: array [0..N-1] of
record
s: string [12];
v: word
end=
((S:'Cut'; v:Cmcut),
(s:'Copy'; v:cmcopy),
(s:'Paste'; v:cmPaste),
(s:'Undo'; v:cmUndo),
(s:'Clear'; v:cmClear),
(s:'Save'; v:cmSave),
(s:'SaveAs'; v:cmSaveAs),
(s:'Find'; v:cmFind),
(s:'Replace'; v:cmReplace),
(s:'SearchAgain'; v:cmSearchAgain),
(s:'CharLeft'; v:cmCharLeft),
(s:'CharRight'; v:cmCharRight),
(s:'WordLeft'; v:cmWordLeft),
(s:'WordRight'; v:cmWordRight),
(s:'LineStart'; V:cmLineStart),
(s:'LineEnd'; v:cmLineEnd),
(s:'LineUp'; v:cmLineup),
(s:'LineDown'; v:cmLineDown),
(s:'PageUp'; v:cmPageUp),
(s:'PageDown'; V:cmPageDown),
(s:'TextStart'; v:cmTextStart),
(s:'TextEnd'; v:cmTextEnd),
(s:'NewLine'; v:cmNewLine),
(s:'BackSpace'; v:cmBackSpace),
(s:'DelChar'; V:cmDelChar),
(s:'DelWord'; v:cmDelWord),
(s:'DelStart'; v:cmDelStart),
(s:'DelEnd'; v:cmDelEnd),
(s:'DelLine'; V:cmDelLine),
(s:'InsMode'; v:cmInsMode),
(s:'StartSelect'; v:cmStartSelect),
(s:'HideSelect'; v:cmHideSelect),
(s:'IndentMode'; v:CmIndentMode));
KVal: array [$47..$53] of word= (1,2,3,4,5,0,6,7,8,9,10,11,12);
KName: array [0..13] of String[6]=
('???','Home','UP','Pgup','Minus',
'Left','Right','Plus','End',
'Down','Pgdn','Ins','Del',
'PrtSrc');
MName: array [0..3] of String [5]= ('','Shift','Ctrl','Alt');
SName: array [$72..$77] of word= (13,5,6,8,10,1);
type Str2=String[2];
function Num(b: byte): Str2;
begin
if b=0 then Num:='10'
else Num:=Chr(b+$30);
end;
function Conv(key: word): String;
var cc: char;
scan,cval: byte;
begin
scan:=Hi(key);
cval:=Lo(key);
case scan of
$00: Conv:='';
$01: Conv:='ESC';
$04..$07: Conv:=MName[scan mod 2 xor 1+1]+' '+KName[scan div 2+9];
$0E: if cval=8 then Conv:='Bksp'
else Conv:='Ctrl Bksp';
$0F: if cval=9 then Conv:='Tab'
else Conv:='Shift Tab';
$1C: if cval=13 then Conv:='Cr'
else Conv:='Ctrl Cr';
$10..$32: begin
cc:=GetAltChar(scan*256);
case cval of
0: Conv:='Alt '+cc;
1..$1B: Conv:='Ctrl '+Cc;
$41..$5B,
$61..$7B: Conv:=cc;
end;
end;
$39: Conv:='';
$3B..$44: Conv:='F'+Num(scan-$3A);
$47..$53: Conv:=KName[KVal[scan]];
$54..$71: Conv:=MName[(scan-$49) div 10]+' '+'F'+Num((scan-$49) Mod 10);
$72..$77: Conv:='Ctrl '+KName[SName[scan]];
$84: Conv:='Ctrl Pgup';
else Conv:='???'
end;
end;
function find(key: word): integer;
var i,j,m: integer;
found: boolean;
begin
i:=-1;
j:=top;
found:=false;
while (i<j-1) do
begin
m:=(i+j) div 2;
if T[m].k1<key then i:=m
else if T[m].k1>=key then j:=m
end;
while (j>0) and (T[j-1].k1=key) do
Dec(j);
find:=j
end;
function insert(k1,k2,cm:word): integer;
var i: integer;
begin
insert:=-1;
if k1=0 then Exit;
insert:=-2;
if (k1=kbEsc) and (k2=kbEsc) then Exit;
insert:=0;
i:=find (k1);
if (T[i].k1=k1) and (k2=0) or
(T[i].k2=0) then Exit;
while (T[i].k1=k1) and (T[i].k2<k2) do
Inc(i);
if (T[i].k1=k1) and (T[i].k2=k2) then
Exit;
move(T[i],T[i+1],(top-i+1)*sizeof(T[0]));
T[i].k1:=k1;
T[i].k2:=k2;
T[i].cm:=cm;
Inc(top);
insert:=1;
end;
function ReadKbd: word;
var E: TEvent;
begin
repeat GetKeyEvent(E);
until E.What<>evNothing;
if (ShiftState and 3 <> 0) and
(E.ScanCode in [$47..$51]) then
E.CharCode:=#0;
Write(Conv(E.KeyCode));
if E.KeyCode=kbBlank then ReadKbd:=0
else ReadKbd:=E.KeyCode;
end;
function EditKeyTable;
var state,k: integer;
f: File;
begin
top:=0;
T[0].k1:=$FFFF;
T[0].k2:=$FFFF;
for k:=0 to N-1 do
begin
repeat
write(Commands[k].s,': ');
state:=insert(ReadKbd,ReadKbd,k);
writeln;
if state=0 then
writeln('Key(s) already defined');
until (state<0);
if state=-2 then
begin
EditKeyTable:=ReadKeyTable;
Exit;
end;
end;
EditKeyTable:=True;
Assign(f,'edit.cfg');
Rewrite(f,1);
blockwrite(f,top,2);
blockwrite(f,T,top*sizeof(T[0]));
close(f)
end;
function ReadKeyTable;
var f: file;
Result: word;
begin
top:=0;
ReadKeyTable:=False;
assign(f,'edit.cfg');
reset(f,1);
if IOResult<>0 then
begin
EditorDialog(edNoKeyTab,NiL);
Exit
end;
blockread(f,top,2,Result);
if Result<>2 then
begin
EditorDialog(edNoKeyTab,Nil);
Close(f);
Exit;
end;
blockread(f,T,sizeof(T[0])*top);
cloSe(f);
ReadKeyTable:=True;
T[top].k1:=$FFFF;
T[top].k2:=$FFFF
end;
procedure GetComm;
begin
if (ShiftState and 3<>0) and (E.ScanCode in [$47..$51])
then E.CharCode:=#0;
if Pos=0 then
begin
Pos:=find(E.KeyCode);
if T[Pos].k1<>E.KeyCode then Pos:=0
else if T[Pos].k2=0 then
begin
E.Command:=Commands[T[pos].cm].v;
E.What:=evCommand;
Pos:=0;
end
else E.What:=evNothing;
end
else
begin
while (T[Pos+1].k1=T[Pos].k1) and
(T[Pos+1].k2<=E.KeyCode) do Inc(Pos);
if T[POS].k2=E.KeyCode then
begin
E.What:=evCommand;
E.Command:=Commands[T[pos].cm].v;
end
else E.What:=evInvalidKey;
Pos:=0;
end;
end;
procedure ShowKeyTable;
var k: integer;
f: text;
begin
Assign(f,'out.$$$');
Rewrite(f);
for k:=0 to top-1 do
writeln(f,Commands[T[k].cm].s,' : ',Conv(T[k].k1),Conv(T[k].k2));
Close(f)
end;
end.
|