wincrt.pp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. This is unit implements some of the crt functionality
  7. for the gui win32 graph unit implementation
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. unit wincrt;
  15. interface
  16. function readkey : char;
  17. function keypressed : boolean;
  18. procedure delay(ms : word);
  19. { dummy }
  20. procedure textmode(mode : integer);
  21. { plays the windows standard sound }
  22. { hz is ignored (at least on win95 }
  23. procedure sound(hz : word);
  24. { dummy }
  25. procedure nosound;
  26. var
  27. directvideo : boolean;
  28. { dummy }
  29. lastmode : word;
  30. implementation
  31. uses
  32. windows,graph;
  33. const
  34. keybuffersize = 32;
  35. var
  36. keyboardhandling : TCriticalSection;
  37. keybuffer : array[1..keybuffersize] of char;
  38. nextfree,nexttoread : longint;
  39. procedure inccyclic(var i : longint);
  40. begin
  41. inc(i);
  42. if i>keybuffersize then
  43. i:=1;
  44. end;
  45. procedure addchar(c : char);
  46. begin
  47. EnterCriticalSection(keyboardhandling);
  48. keybuffer[nextfree]:=c;
  49. inccyclic(nextfree);
  50. { skip old chars }
  51. if nexttoread=nextfree then
  52. begin
  53. // special keys are started by #0
  54. // so we've to remove two chars
  55. if keybuffer[nexttoread]=#0 then
  56. inccyclic(nexttoread);
  57. inccyclic(nexttoread);
  58. end;
  59. LeaveCriticalSection(keyboardhandling);
  60. end;
  61. function readkey : char;
  62. begin
  63. while true do
  64. begin
  65. EnterCriticalSection(keyboardhandling);
  66. if nexttoread<>nextfree then
  67. begin
  68. readkey:=keybuffer[nexttoread];
  69. inccyclic(nexttoread);
  70. LeaveCriticalSection(keyboardhandling);
  71. exit;
  72. end;
  73. LeaveCriticalSection(keyboardhandling);
  74. { give other threads a chance }
  75. Windows.Sleep(10);
  76. end;
  77. end;
  78. function keypressed : boolean;
  79. begin
  80. EnterCriticalSection(keyboardhandling);
  81. keypressed:=nexttoread<>nextfree;
  82. LeaveCriticalSection(keyboardhandling);
  83. end;
  84. procedure delay(ms : word);
  85. begin
  86. Sleep(ms);
  87. end;
  88. procedure textmode(mode : integer);
  89. begin
  90. end;
  91. procedure sound(hz : word);
  92. begin
  93. Windows.Beep(hz,500);
  94. end;
  95. procedure nosound;
  96. begin
  97. end;
  98. procedure addextchar(c : char);
  99. begin
  100. addchar(#0);
  101. addchar(c);
  102. end;
  103. const
  104. altkey : boolean = false;
  105. ctrlkey : boolean = false;
  106. shiftkey : boolean = false;
  107. function msghandler(Window: HWnd; AMessage:UInt; WParam : WParam; LParam: LParam): Longint; stdcall;
  108. begin
  109. case amessage of
  110. WM_CHAR:
  111. begin
  112. addchar(chr(wparam));
  113. end;
  114. WM_KEYDOWN:
  115. begin
  116. case wparam of
  117. VK_LEFT:
  118. addextchar(#75);
  119. VK_RIGHT:
  120. addextchar(#77);
  121. VK_DOWN:
  122. addextchar(#80);
  123. VK_UP:
  124. addextchar(#72);
  125. VK_INSERT:
  126. addextchar(#82);
  127. VK_DELETE:
  128. addextchar(#83);
  129. VK_END:
  130. addextchar(#79);
  131. VK_HOME:
  132. addextchar(#71);
  133. VK_PRIOR:
  134. addextchar(#73);
  135. VK_NEXT:
  136. addextchar(#81);
  137. VK_F1..VK_F10:
  138. begin
  139. if ctrlkey then
  140. addextchar(chr(wparam+24))
  141. else if altkey then
  142. addextchar(chr(wparam+34))
  143. else
  144. addextchar(chr(wparam-11));
  145. end;
  146. VK_CONTROL:
  147. ctrlkey:=true;
  148. VK_MENU:
  149. altkey:=true;
  150. VK_SHIFT:
  151. shiftkey:=true;
  152. end;
  153. end;
  154. WM_KEYUP:
  155. begin
  156. case wparam of
  157. VK_CONTROL:
  158. ctrlkey:=false;
  159. VK_MENU:
  160. altkey:=false;
  161. VK_SHIFT:
  162. shiftkey:=false;
  163. end;
  164. end;
  165. end;
  166. msghandler:=0;
  167. end;
  168. var
  169. oldexitproc : pointer;
  170. procedure myexitproc;
  171. begin
  172. exitproc:=oldexitproc;
  173. charmessagehandler:=nil;
  174. DeleteCriticalSection(keyboardhandling);
  175. end;
  176. begin
  177. charmessagehandler:=@msghandler;
  178. nextfree:=1;
  179. nexttoread:=1;
  180. InitializeCriticalSection(keyboardhandling);
  181. oldexitproc:=exitproc;
  182. exitproc:=@myexitproc;
  183. lastmode:=0;
  184. end.
  185. {
  186. $Log$
  187. Revision 1.5 2003-04-23 11:35:00 peter
  188. * wndproc definition fixed
  189. Revision 1.4 2003/04/23 11:22:12 peter
  190. * fixed msghandler declarations
  191. Revision 1.3 2002/09/07 16:01:29 peter
  192. * old logs removed and tabs fixed
  193. }