wincrt.pp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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, WParam,
  108. LParam: Longint): Longint;
  109. begin
  110. case amessage of
  111. WM_CHAR:
  112. begin
  113. addchar(chr(wparam));
  114. end;
  115. WM_KEYDOWN:
  116. begin
  117. case wparam of
  118. VK_LEFT:
  119. addextchar(#75);
  120. VK_RIGHT:
  121. addextchar(#77);
  122. VK_DOWN:
  123. addextchar(#80);
  124. VK_UP:
  125. addextchar(#72);
  126. VK_INSERT:
  127. addextchar(#82);
  128. VK_DELETE:
  129. addextchar(#83);
  130. VK_END:
  131. addextchar(#79);
  132. VK_HOME:
  133. addextchar(#71);
  134. VK_PRIOR:
  135. addextchar(#73);
  136. VK_NEXT:
  137. addextchar(#81);
  138. VK_F1..VK_F10:
  139. begin
  140. if ctrlkey then
  141. addextchar(chr(wparam+24))
  142. else if altkey then
  143. addextchar(chr(wparam+34))
  144. else
  145. addextchar(chr(wparam-11));
  146. end;
  147. VK_CONTROL:
  148. ctrlkey:=true;
  149. VK_MENU:
  150. altkey:=true;
  151. VK_SHIFT:
  152. shiftkey:=true;
  153. end;
  154. end;
  155. WM_KEYUP:
  156. begin
  157. case wparam of
  158. VK_CONTROL:
  159. ctrlkey:=false;
  160. VK_MENU:
  161. altkey:=false;
  162. VK_SHIFT:
  163. shiftkey:=false;
  164. end;
  165. end;
  166. end;
  167. msghandler:=0;
  168. end;
  169. var
  170. oldexitproc : pointer;
  171. procedure myexitproc;
  172. begin
  173. exitproc:=oldexitproc;
  174. charmessagehandler:=nil;
  175. DeleteCriticalSection(keyboardhandling);
  176. end;
  177. begin
  178. charmessagehandler:=@msghandler;
  179. nextfree:=1;
  180. nexttoread:=1;
  181. InitializeCriticalSection(keyboardhandling);
  182. oldexitproc:=exitproc;
  183. exitproc:=@myexitproc;
  184. lastmode:=0;
  185. end.
  186. {
  187. $Log$
  188. Revision 1.2 2000-07-13 11:33:58 michael
  189. + removed logs
  190. }