fpkeys.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. {
  2. $Id$
  3. This file is part of the Free Pascal Integrated Development Environment
  4. Copyright (c) 1998-2000 by Pierre Muller
  5. Learn keys routines for the IDE
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit fpkeys;
  13. interface
  14. uses
  15. keyboard, Objects, Drivers, Dialogs, App,
  16. FPViews, FPString, WViews;
  17. procedure LearnKeysDialog;
  18. Const
  19. NumWantedKeys = 24;
  20. WantedKeys : Array[1..NumWantedKeys] of word =
  21. (kbF1,kbF2,kbF3,kbF4,
  22. kbF5,kbF6,kbF7,kbF8,
  23. kbF9,kbF10,kbF11,kbF12,
  24. kbLeft,kbRight,kbUp,kbDown,
  25. kbPgUp,kbPgDn,kbIns,kbDel,
  26. kbEnd,kbHome,kbBack,kbShiftTab);
  27. type
  28. PKeyDialog = ^TKeyDialog;
  29. TKeyDialog = object(TCenterDialog)
  30. Constructor Init(Const ATitle : String);
  31. {Procedure HandleEvent(var E : TEvent);virtual;}
  32. function Execute : Word;Virtual;
  33. PSTL : Array [1..NumWantedKeys] of PLabel;
  34. PL : Array [1..NumWantedKeys] of PInputLine;
  35. KeyOK : Array [1..NumWantedKeys] of boolean;
  36. PST,PST2 : PAdvancedStaticText;
  37. end;
  38. Procedure LoadKeys(var S : TStream);
  39. Procedure StoreKeys(var S : TStream);
  40. Procedure SetKnownKeys;
  41. implementation
  42. uses
  43. {$ifdef FVISION}
  44. FVConsts,
  45. {$else}
  46. Commands,
  47. {$endif}
  48. WUtils;
  49. {$ifndef NotUseTree}
  50. function GetKey(Const St : String) : word;
  51. var
  52. AChar,AScan : byte;
  53. begin
  54. If FindSequence(St,AChar,Ascan) then
  55. GetKey:=Ascan*$100+AChar
  56. else
  57. GetKey:=0;
  58. end;
  59. Procedure SetKey(Const St : String;key :word);
  60. var
  61. AChar,AScan : byte;
  62. begin
  63. AChar:=key and $ff;
  64. AScan:=key shr 8;
  65. AddSequence(St,AChar,Ascan);
  66. end;
  67. {$endif not NotUseTree}
  68. Const
  69. WantedKeysLabels : Array[1..NumWantedKeys] of String[5] =
  70. ('F1 ','F2 ','F3 ','F4 ',
  71. 'F5 ','F6 ','F7 ','F8 ',
  72. 'F9 ','F10 ','F11 ','F12 ',
  73. 'Left ','Right','Up ','Down ',
  74. 'PgUp ','PgDn ','Ins ','Del ',
  75. 'End ','Home ','Back ','ShTab');
  76. var
  77. KeyEscape : Array[1..NumWantedKeys] of String[10];
  78. Procedure StoreKeys(var S : TStream);
  79. var
  80. i : longint;
  81. l : byte;
  82. begin
  83. for i:=1 to NumWantedKeys do
  84. if KeyEscape[i]<>'' then
  85. begin
  86. S.Write(i,Sizeof(i));
  87. l:=Length(KeyEscape[i]);
  88. S.Write(l,sizeof(l));
  89. S.Write(KeyEscape[i][1],l);
  90. end;
  91. end;
  92. Procedure LoadKeys(var S : TStream);
  93. var
  94. i : longint;
  95. l : byte;
  96. begin
  97. While S.GetPos<S.GetSize do
  98. begin
  99. S.Read(i,Sizeof(i));
  100. S.Read(l,Sizeof(l));
  101. S.Read(KeyEscape[i][1],l);
  102. KeyEscape[i][0]:=chr(l);
  103. end;
  104. SetKnownKeys;
  105. end;
  106. Procedure SetKnownKeys;
  107. var
  108. i : longint;
  109. begin
  110. {$ifndef NotUseTree}
  111. for i:=1 to NumWantedKeys do
  112. if KeyEscape[i]<>'' then
  113. SetKey(KeyEscape[i],WantedKeys[i]);
  114. {$endif not NotUseTree}
  115. end;
  116. function NiceEscape(Const St : String) : String;
  117. var
  118. s : string;
  119. i : longint;
  120. begin
  121. s:='';
  122. for i:=1 to length(St) do
  123. case ord(St[i]) of
  124. 1..26 : s:=s+'^'+chr(ord('A')-1+Ord(St[i]));
  125. 27 : s:=s+'Esc';
  126. 0,28..31,127..255 : s:=s+'"#'+IntToStr(ord(St[i]))+'"';
  127. else
  128. s:=s+St[i];
  129. end;
  130. NiceEscape:=s;
  131. end;
  132. constructor TKeyDialog.Init(Const ATitle : String);
  133. var
  134. St : String;
  135. D : PCenterDialog;
  136. R : TRect;
  137. E : TEvent;
  138. i,hight,key : longint;
  139. begin
  140. Hight:=(NumWantedKeys + 2) div 3;
  141. R.Assign(0,0,63 + 4,Hight + 4);
  142. Inherited Init(R,ATitle);
  143. for i:=1 to NumWantedKeys do
  144. begin
  145. GetExtent(R);
  146. R.Grow(-1,-1);
  147. R.A.Y:=R.A.Y + ((i-1) mod Hight);
  148. R.A.X:=R.A.X + 21 * ((i-1) div Hight);
  149. R.B.Y:=R.A.Y+1;
  150. R.B.X:=R.A.X + 10;
  151. St:=WantedKeysLabels[i]+' key';
  152. KeyOK[i]:=false;
  153. New(PSTL[i],Init(R,St,nil));
  154. Insert(PSTL[i]);
  155. R.A.X:=R.B.X+1;
  156. R.B.X:=R.B.X+11;
  157. New(PL[i],Init(R,20));
  158. St:=NiceEscape(KeyEscape[i]);
  159. PL[i]^.SetData(St);
  160. Insert(PL[i]);
  161. PSTL[i]^.Link:=PL[i];
  162. end;
  163. GetExtent(R);
  164. R.Grow(-1,-1);
  165. Dec(R.B.Y);
  166. R.A.Y:=R.B.Y-1;
  167. New(PST,init(R,'Press all listed keys'));
  168. Insert(PST);
  169. GetExtent(R);
  170. R.Grow(-1,-1);
  171. R.A.Y:=R.B.Y-1;
  172. New(PST2,init(R,'Alt prefix "'+NiceEscape(chr(AltPrefix))+'" Shift prefix = "'+
  173. NiceEscape(chr(ShiftPrefix))+'" Ctrl prefix = "'+NiceEscape(chr(CtrlPrefix))+'"'));
  174. Insert(PST2);
  175. InsertButtons(@Self);
  176. end;
  177. function TKeyDialog.Execute : Word;
  178. var
  179. APL : PInputLine;
  180. i,j : longint;
  181. St : String;
  182. E : TEvent;
  183. OldKey : word;
  184. keyfound : boolean;
  185. begin
  186. {$ifndef NotUseTree}
  187. repeat
  188. EndState := 0;
  189. repeat
  190. if TypeOf(Current^)=Typeof(TInputLine) then
  191. APL:=PInputLine(Current)
  192. else if TypeOf(Current^)=Typeof(TLabel) then
  193. APL:=PInputLine(Plabel(Current)^.Link)
  194. else
  195. APL:=nil;
  196. FillChar(E,SizeOf(E),#0);
  197. if Keyboard.KeyPressed then
  198. St:=RawReadString
  199. else
  200. begin
  201. St:='';
  202. Application^.GetEvent(E);
  203. end;
  204. if E.What= evNothing then
  205. begin
  206. if St<>'' then
  207. begin
  208. if GetKey(St)<>0 then
  209. begin
  210. E.What:=evKeyDown;
  211. E.KeyCode:=GetKey(St);
  212. end
  213. else if St=#9 then
  214. begin
  215. E.What:=evKeyDown;
  216. E.KeyCode:=kbTab;
  217. end
  218. else if St=#27 then
  219. begin
  220. E.What:=evKeyDown;
  221. E.KeyCode:=kbEsc;
  222. end
  223. else if St=#13 then
  224. begin
  225. E.What:=evKeyDown;
  226. E.KeyCode:=kbEnter;
  227. end;
  228. end;
  229. end;
  230. keyFound:=false;
  231. if (E.What=evKeyDown) and not assigned(APL) then
  232. begin
  233. for i:=1 to NumWantedKeys do
  234. if E.Keycode=WantedKeys[i] then
  235. begin
  236. DisposeStr(PSTL[i]^.Text);
  237. PSTL[i]^.Text:=NewStr(WantedKeysLabels[i]+' OK ');
  238. keyFound:=true;
  239. keyOK[i]:=true;
  240. KeyEscape[i]:=St;
  241. St:=NiceEscape(St);
  242. PL[i]^.SetData(St);
  243. ClearEvent(E);
  244. ReDraw;
  245. end;
  246. end;
  247. if (St<>'') and not keyfound and
  248. ((E.What<>evKeyDown) or
  249. ((E.KeyCode<>kbTab) and (E.Keycode<>kbEnter) and (E.Keycode<>kbEsc))) then
  250. begin
  251. PST^.SetText('"'+NiceEscape(St)+'"');
  252. if Assigned(APL) then
  253. begin
  254. j:=-1;
  255. for i:=1 to NumWantedKeys do
  256. if APL=PL[i] then
  257. j:=i;
  258. if (j>0) and (j<=NumWantedKeys) then
  259. begin
  260. OldKey:=GetKey(St);
  261. if OldKey<>0 then
  262. begin
  263. for i:=1 to NumWantedKeys do
  264. if (OldKey=WantedKeys[i]) and (i<>j) then
  265. begin
  266. If ConfirmBox('"'+St+'" is used for'+#13+
  267. 'key $'+IntToHex(OldKey,4)+' '+WantedKeysLabels[i]+#13+
  268. 'Change it to '+WantedKeysLabels[j],nil,true)=cmYes then
  269. begin
  270. KeyEscape[i]:='';
  271. PL[i]^.SetData(KeyEscape[i]);
  272. end
  273. else
  274. begin
  275. St:='';
  276. end;
  277. end;
  278. end;
  279. if St<>'' then
  280. Begin
  281. SetKey(St,WantedKeys[j]);
  282. KeyEscape[j]:=St;
  283. St:=NiceEscape(St);
  284. APL^.SetData(St);
  285. end;
  286. end;
  287. ClearEvent(E);
  288. end;
  289. end;
  290. if (E.What<>evNothing) then
  291. HandleEvent(E);
  292. if E.What <> evNothing then EventError(E);
  293. until EndState <> 0;
  294. until Valid(EndState);
  295. Execute := EndState;
  296. {$else NotUseTree}
  297. Execute:=cmCancel;
  298. {$endif NotUseTree}
  299. end;
  300. procedure LearnKeysDialog;
  301. var
  302. D : PKeyDialog;
  303. begin
  304. {$ifdef NotUseTree}
  305. NotImplemented;
  306. {$else not NotUseTree}
  307. New(D,Init('Learn keys'));
  308. Application^.ExecuteDialog(D,nil);
  309. {$endif not NotUseTree}
  310. end;
  311. end.
  312. {
  313. $Log$
  314. Revision 1.3 2002-09-07 15:40:43 peter
  315. * old logs removed and tabs fixed
  316. }