fpkeys.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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,index : longint;
  81. l : byte;
  82. begin
  83. for i:=1 to NumWantedKeys do
  84. if KeyEscape[i]<>'' then
  85. begin
  86. { need temporary local var, because write has var argument }
  87. index:=i;
  88. S.Write(index,Sizeof(index));
  89. l:=Length(KeyEscape[i]);
  90. S.Write(l,sizeof(l));
  91. S.Write(KeyEscape[i][1],l);
  92. end;
  93. end;
  94. Procedure LoadKeys(var S : TStream);
  95. var
  96. i : longint;
  97. l : byte;
  98. begin
  99. While S.GetPos<S.GetSize do
  100. begin
  101. S.Read(i,Sizeof(i));
  102. S.Read(l,Sizeof(l));
  103. S.Read(KeyEscape[i][1],l);
  104. KeyEscape[i][0]:=chr(l);
  105. end;
  106. SetKnownKeys;
  107. end;
  108. Procedure SetKnownKeys;
  109. var
  110. i : longint;
  111. begin
  112. {$ifndef NotUseTree}
  113. for i:=1 to NumWantedKeys do
  114. if KeyEscape[i]<>'' then
  115. SetKey(KeyEscape[i],WantedKeys[i]);
  116. {$endif not NotUseTree}
  117. end;
  118. function NiceEscape(Const St : String) : String;
  119. var
  120. s : string;
  121. i : longint;
  122. begin
  123. s:='';
  124. for i:=1 to length(St) do
  125. case ord(St[i]) of
  126. 1..26 : s:=s+'^'+chr(ord('A')-1+Ord(St[i]));
  127. 27 : s:=s+'Esc';
  128. 0,28..31,127..255 : s:=s+'"#'+IntToStr(ord(St[i]))+'"';
  129. else
  130. s:=s+St[i];
  131. end;
  132. NiceEscape:=s;
  133. end;
  134. constructor TKeyDialog.Init(Const ATitle : String);
  135. var
  136. St : String;
  137. D : PCenterDialog;
  138. R : TRect;
  139. E : TEvent;
  140. i,hight,key : longint;
  141. begin
  142. Hight:=(NumWantedKeys + 2) div 3;
  143. R.Assign(0,0,63 + 4,Hight + 4);
  144. Inherited Init(R,ATitle);
  145. for i:=1 to NumWantedKeys do
  146. begin
  147. GetExtent(R);
  148. R.Grow(-1,-1);
  149. R.A.Y:=R.A.Y + ((i-1) mod Hight);
  150. R.A.X:=R.A.X + 21 * ((i-1) div Hight);
  151. R.B.Y:=R.A.Y+1;
  152. R.B.X:=R.A.X + 10;
  153. St:=WantedKeysLabels[i]+' key';
  154. KeyOK[i]:=false;
  155. New(PSTL[i],Init(R,St,nil));
  156. Insert(PSTL[i]);
  157. R.A.X:=R.B.X+1;
  158. R.B.X:=R.B.X+11;
  159. New(PL[i],Init(R,20));
  160. St:=NiceEscape(KeyEscape[i]);
  161. PL[i]^.SetData(St);
  162. Insert(PL[i]);
  163. PSTL[i]^.Link:=PL[i];
  164. end;
  165. GetExtent(R);
  166. R.Grow(-1,-1);
  167. Dec(R.B.Y);
  168. R.A.Y:=R.B.Y-1;
  169. New(PST,init(R,'Press all listed keys'));
  170. Insert(PST);
  171. GetExtent(R);
  172. R.Grow(-1,-1);
  173. R.A.Y:=R.B.Y-1;
  174. New(PST2,init(R,'Alt prefix "'+NiceEscape(chr(AltPrefix))+'" Shift prefix = "'+
  175. NiceEscape(chr(ShiftPrefix))+'" Ctrl prefix = "'+NiceEscape(chr(CtrlPrefix))+'"'));
  176. Insert(PST2);
  177. InsertButtons(@Self);
  178. end;
  179. function TKeyDialog.Execute : Word;
  180. var
  181. APL : PInputLine;
  182. i,j : longint;
  183. St : String;
  184. E : TEvent;
  185. OldKey : word;
  186. keyfound : boolean;
  187. begin
  188. {$ifndef NotUseTree}
  189. repeat
  190. EndState := 0;
  191. repeat
  192. if TypeOf(Current^)=Typeof(TInputLine) then
  193. APL:=PInputLine(Current)
  194. else if TypeOf(Current^)=Typeof(TLabel) then
  195. APL:=PInputLine(Plabel(Current)^.Link)
  196. else
  197. APL:=nil;
  198. FillChar(E,SizeOf(E),#0);
  199. if Keyboard.KeyPressed then
  200. St:=RawReadString
  201. else
  202. begin
  203. St:='';
  204. Application^.GetEvent(E);
  205. end;
  206. if E.What= evNothing then
  207. begin
  208. if St<>'' then
  209. begin
  210. if GetKey(St)<>0 then
  211. begin
  212. E.What:=evKeyDown;
  213. E.KeyCode:=GetKey(St);
  214. end
  215. else if St=#9 then
  216. begin
  217. E.What:=evKeyDown;
  218. E.KeyCode:=kbTab;
  219. end
  220. else if St=#27 then
  221. begin
  222. E.What:=evKeyDown;
  223. E.KeyCode:=kbEsc;
  224. end
  225. else if St=#13 then
  226. begin
  227. E.What:=evKeyDown;
  228. E.KeyCode:=kbEnter;
  229. end;
  230. end;
  231. end;
  232. keyFound:=false;
  233. if (E.What=evKeyDown) and not assigned(APL) then
  234. begin
  235. for i:=1 to NumWantedKeys do
  236. if E.Keycode=WantedKeys[i] then
  237. begin
  238. DisposeStr(PSTL[i]^.Text);
  239. PSTL[i]^.Text:=NewStr(WantedKeysLabels[i]+' OK ');
  240. keyFound:=true;
  241. keyOK[i]:=true;
  242. KeyEscape[i]:=St;
  243. St:=NiceEscape(St);
  244. PL[i]^.SetData(St);
  245. ClearEvent(E);
  246. ReDraw;
  247. end;
  248. end;
  249. if (St<>'') and not keyfound and
  250. ((E.What<>evKeyDown) or
  251. ((E.KeyCode<>kbTab) and (E.Keycode<>kbEnter) and (E.Keycode<>kbEsc))) then
  252. begin
  253. PST^.SetText('"'+NiceEscape(St)+'"');
  254. if Assigned(APL) then
  255. begin
  256. j:=-1;
  257. for i:=1 to NumWantedKeys do
  258. if APL=PL[i] then
  259. j:=i;
  260. if (j>0) and (j<=NumWantedKeys) then
  261. begin
  262. OldKey:=GetKey(St);
  263. if OldKey<>0 then
  264. begin
  265. for i:=1 to NumWantedKeys do
  266. if (OldKey=WantedKeys[i]) and (i<>j) then
  267. begin
  268. If ConfirmBox('"'+St+'" is used for'+#13+
  269. 'key $'+IntToHex(OldKey,4)+' '+WantedKeysLabels[i]+#13+
  270. 'Change it to '+WantedKeysLabels[j],nil,true)=cmYes then
  271. begin
  272. KeyEscape[i]:='';
  273. PL[i]^.SetData(KeyEscape[i]);
  274. end
  275. else
  276. begin
  277. St:='';
  278. end;
  279. end;
  280. end;
  281. if St<>'' then
  282. Begin
  283. SetKey(St,WantedKeys[j]);
  284. KeyEscape[j]:=St;
  285. St:=NiceEscape(St);
  286. APL^.SetData(St);
  287. end;
  288. end;
  289. ClearEvent(E);
  290. end;
  291. end;
  292. if (E.What<>evNothing) then
  293. HandleEvent(E);
  294. if E.What <> evNothing then EventError(E);
  295. until EndState <> 0;
  296. until Valid(EndState);
  297. Execute := EndState;
  298. {$else NotUseTree}
  299. Execute:=cmCancel;
  300. {$endif NotUseTree}
  301. end;
  302. procedure LearnKeysDialog;
  303. var
  304. D : PKeyDialog;
  305. begin
  306. {$ifdef NotUseTree}
  307. NotImplemented;
  308. {$else not NotUseTree}
  309. New(D,Init('Learn keys'));
  310. Application^.ExecuteDialog(D,nil);
  311. {$endif not NotUseTree}
  312. end;
  313. end.
  314. {
  315. $Log$
  316. Revision 1.4 2004-09-16 16:20:06 peter
  317. * illegal for-loop var assignemnt
  318. Revision 1.3 2002/09/07 15:40:43 peter
  319. * old logs removed and tabs fixed
  320. }