wincrt.pp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. var
  20. directvideo : boolean;
  21. implementation
  22. uses
  23. windows,graph;
  24. const
  25. keybuffersize = 16;
  26. var
  27. keyboardhandling : TCriticalSection;
  28. keybuffer : array[1..keybuffersize] of char;
  29. nextfree,nexttoread : longint;
  30. procedure inccyclic(var i : longint);
  31. begin
  32. inc(i);
  33. if i>keybuffersize then
  34. i:=1;
  35. end;
  36. procedure addchar(c : char);
  37. begin
  38. EnterCriticalSection(keyboardhandling);
  39. keybuffer[nextfree]:=c;
  40. inccyclic(nextfree);
  41. { skip old chars }
  42. if nexttoread=nextfree then
  43. inccyclic(nexttoread);
  44. LeaveCriticalSection(keyboardhandling);
  45. end;
  46. function readkey : char;
  47. begin
  48. while true do
  49. begin
  50. EnterCriticalSection(keyboardhandling);
  51. if nexttoread<>nextfree then
  52. begin
  53. readkey:=keybuffer[nexttoread];
  54. inccyclic(nexttoread);
  55. LeaveCriticalSection(keyboardhandling);
  56. exit;
  57. end;
  58. LeaveCriticalSection(keyboardhandling);
  59. { give other threads a chance }
  60. Windows.Sleep(0);
  61. end;
  62. end;
  63. function keypressed : boolean;
  64. begin
  65. EnterCriticalSection(keyboardhandling);
  66. keypressed:=nexttoread<>nextfree;
  67. LeaveCriticalSection(keyboardhandling);
  68. end;
  69. procedure delay(ms : word);
  70. begin
  71. Sleep(ms);
  72. end;
  73. function msghandler(Window: hwnd; AMessage, WParam,
  74. LParam: Longint): Longint;
  75. begin
  76. case amessage of
  77. WM_CHAR:
  78. begin
  79. addchar(chr(wparam));
  80. writeln('got char message: ',wparam);
  81. end;
  82. WM_KEYDOWN:
  83. begin
  84. writeln('got key message');
  85. end;
  86. end;
  87. msghandler:=0;
  88. end;
  89. var
  90. oldexitproc : pointer;
  91. procedure myexitproc;
  92. begin
  93. exitproc:=oldexitproc;
  94. charmessagehandler:=nil;
  95. DeleteCriticalSection(keyboardhandling);
  96. end;
  97. begin
  98. charmessagehandler:=@msghandler;
  99. nextfree:=1;
  100. nexttoread:=1;
  101. InitializeCriticalSection(keyboardhandling);
  102. oldexitproc:=exitproc;
  103. exitproc:=@myexitproc;
  104. end.
  105. {
  106. $Log$
  107. Revision 1.3 2000-01-07 16:41:53 daniel
  108. * copyright 2000
  109. Revision 1.2 1999/11/29 22:03:39 florian
  110. * first implementation of winmouse unit
  111. Revision 1.1 1999/11/24 22:33:15 florian
  112. + created from extgraph
  113. }