mouse.tex 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. %
  2. % $Id$
  3. % This file is part of the FPC documentation.
  4. % Copyright (C) 1997, by Michael Van Canneyt
  5. %
  6. % The FPC documentation is free text; you can redistribute it and/or
  7. % modify it under the terms of the GNU Library General Public License as
  8. % published by the Free Software Foundation; either version 2 of the
  9. % License, or (at your option) any later version.
  10. %
  11. % The FPC Documentation is distributed in the hope that it will be useful,
  12. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. % Library General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU Library General Public
  17. % License along with the FPC documentation; see the file COPYING.LIB. If not,
  18. % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. % Boston, MA 02111-1307, USA.
  20. %
  21. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  22. %
  23. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  24. % The Mouse unit
  25. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  26. \chapter{The MOUSE unit}
  27. \FPCexampledir{mouseex}
  28. The \var{Mouse} unit implements a platform independent mouse handling
  29. interface. It is implemented identically on all platforms supported by
  30. \fpc{} and can be enhanced with custom drivers, should this be needed.
  31. It is intended to be used only in text-based screens, for instance in
  32. conjunction with the keyboard and video unit. No support for graphical
  33. screens is implemented, and there are (currently) no plans to implement
  34. this.
  35. \section{Constants, Types and Variables}
  36. \subsection{Constants}
  37. The following constants can be used when mouse drivers need to report
  38. errors:
  39. \begin{verbatim}
  40. const
  41. { We have an errorcode base of 1030 }
  42. errMouseBase = 1030;
  43. errMouseInitError = errMouseBase + 0;
  44. errMouseNotImplemented = errMouseBase + 1;
  45. \end{verbatim}
  46. The following constants describe which action a mouse event describes
  47. \begin{verbatim}
  48. const
  49. MouseActionDown = $0001; { Mouse down event }
  50. MouseActionUp = $0002; { Mouse up event }
  51. MouseActionMove = $0004; { Mouse move event }
  52. \end{verbatim}
  53. The following constants describe the used buttons in a mouse event:
  54. \begin{verbatim}
  55. MouseLeftButton = $01; { Left mouse button }
  56. MouseRightButton = $02; { Right mouse button }
  57. MouseMiddleButton = $04; { Middle mouse button }
  58. \end{verbatim}
  59. The mouse unit has a mechanism to buffer mouse events. The following
  60. constant defines the size of the event buffer:
  61. \begin{verbatim}
  62. MouseEventBufSize = 16;
  63. \end{verbatim}
  64. \subsection{Types}
  65. The \var{TMouseEvent} is the central type of the mouse unit, it is used
  66. to describe the mouse events:
  67. \begin{verbatim}
  68. PMouseEvent=^TMouseEvent;
  69. TMouseEvent=packed record { 8 bytes }
  70. buttons : word;
  71. x,y : word;
  72. Action : word;
  73. end;
  74. \end{verbatim}
  75. The \var{Buttons} field describes which buttons were down when the event
  76. occurred. The \var{x,y} fields describe where the event occurred on the
  77. screen. The \var{Action} describes what action was going on when the event
  78. occurred. The \var{Buttons} and \var{Action} field can be examined using the
  79. above constants.
  80. The following record is used to implement a mouse driver in the
  81. \seep{SetMouseDriver} function:
  82. \begin{verbatim}
  83. TMouseDriver = Record
  84. UseDefaultQueue : Boolean;
  85. InitDriver : Procedure;
  86. DoneDriver : Procedure;
  87. DetectMouse : Function : Byte;
  88. ShowMouse : Procedure;
  89. HideMouse : Procedure;
  90. GetMouseX : Function : Word;
  91. GetMouseY : Function : Word;
  92. GetMouseButtons : Function : Word;
  93. SetMouseXY : procedure (x,y:word);
  94. GetMouseEvent : procedure (var MouseEvent:TMouseEvent);
  95. PollMouseEvent : function (var MouseEvent: TMouseEvent):boolean;
  96. PutMouseEvent : procedure (Const MouseEvent:TMouseEvent);
  97. end;
  98. \end{verbatim}
  99. Its fields will be explained in the section on writing a custom driver.
  100. \subsection{Variables}
  101. The following variables are used to keep the current position and state of
  102. the mouse.
  103. \begin{verbatim}
  104. MouseIntFlag : Byte; { Mouse in int flag }
  105. MouseButtons : Byte; { Mouse button state }
  106. MouseWhereX,
  107. MouseWhereY : Word; { Mouse position }
  108. \end{verbatim}
  109. \section{Functions and procedures}
  110. \begin{function}{DetectMouse}
  111. \Declaration
  112. Function DetectMouse:byte;
  113. \Description
  114. \var{DetectMouse} detects whether a mouse is attached to the system or not.
  115. If there is no mouse, then zero is returned. If a mouse is attached, then
  116. the number of mouse buttons is returned.
  117. This function should be called after the mouse driver was initialized.
  118. \Errors
  119. None.
  120. \SeeAlso
  121. \seep{InitMouse},\seep{DoneMouse},
  122. \end{function}
  123. \FPCexample{ex1}
  124. \begin{procedure}{DoneMouse}
  125. \Declaration
  126. Procedure DoneMouse;
  127. \Description
  128. \var{DoneMouse} De-initializes the mouse driver. It cleans up any memory
  129. allocated when the mouse was initialized, or removes possible mouse hooks
  130. from memory. The mouse functions will not work after \var{DoneMouse} was
  131. called. If \var{DoneMouse} is called a second time, it will exit at once.
  132. \var{InitMouse} should be called before \var{DoneMouse} can be called again.
  133. \Errors
  134. None.
  135. \SeeAlso
  136. \seef{DetectMouse}, \seep{InitMouse}
  137. \end{procedure}
  138. For an example, see most other mouse functions.
  139. \begin{function}{GetMouseButtons}
  140. \Declaration
  141. Function GetMouseButtons:word;
  142. \Description
  143. \var{GetMouseButtons} returns the current button state of the mouse, i.e.
  144. it returns a or-ed combination of the following constants:
  145. \begin{description}
  146. \item[MouseLeftButton] When the left mouse button is held down.
  147. \item[MouseRightButton] When the right mouse button is held down.
  148. \item[MouseMiddleButton] When the middle mouse button is held down.
  149. \end{description}
  150. \Errors
  151. None.
  152. \SeeAlso
  153. \seep{GetMouseEvent}, \seef{GetMouseX}, \seef{GetMouseY}
  154. \end{function}
  155. \FPCexample{ex2}
  156. \begin{procedure}{GetMouseDriver}
  157. \Declaration
  158. Procedure GetMouseDriver(Var Driver : TMouseDriver);
  159. \Description
  160. \var{GetMouseDriver} returns the currently set mouse driver. It can be used
  161. to retrieve the current mouse driver, and override certain callbacks.
  162. A more detailed explanation about getting and setting mouse drivers can be found in
  163. \sees{mousedrv}.
  164. \Errors
  165. None.
  166. \SeeAlso
  167. \seep{SetMouseDriver}
  168. \end{procedure}
  169. For an example, see the section on writing a custom mouse driver,
  170. \sees{mousedrv}
  171. \begin{procedure}{GetMouseEvent}
  172. \Declaration
  173. Procedure GetMouseEvent(var MouseEvent:TMouseEvent);
  174. \Description
  175. \var{GetMouseEvent} returns the next mouse event (a movement, button press or
  176. button release), and waits for one if none is available in the queue.
  177. Some mouse drivers can implement a mouse event queue which can hold multiple
  178. events till they are fetched.; Others don't, and in that case, a one-event
  179. queue is implemented for use with \seef{PollMouseEvent}.
  180. \Errors
  181. None.
  182. \SeeAlso
  183. \seef{GetMouseButtons}, \seef{GetMouseX}, \seef{GetMouseY}
  184. \end{procedure}
  185. \begin{function}{GetMouseX}
  186. \Declaration
  187. Function GetMouseX:word;
  188. \Description
  189. \var{GetMouseX} returns the current \var{X} position of the mouse. \var{X} is
  190. measured in characters, starting at 0 for the left side of the screen.
  191. \Errors
  192. None.
  193. \SeeAlso
  194. \seef{GetMouseButtons},\seep{GetMouseEvent}, \seef{GetMouseY}
  195. \end{function}
  196. \FPCexample{ex4}
  197. \begin{function}{GetMouseY}
  198. \Declaration
  199. Function GetMouseY:word;
  200. \Description
  201. \var{GetMouseY} returns the current \var{Y} position of the mouse. \var{Y} is
  202. measured in characters, starting at 0 for the top of the screen.
  203. \Errors
  204. None.
  205. \SeeAlso
  206. \seef{GetMouseButtons},\seep{GetMouseEvent}, \seef{GetMouseX}
  207. \end{function}
  208. For an example, see \seef{GetMouseX}
  209. \begin{procedure}{HideMouse}
  210. \Declaration
  211. Procedure HideMouse;
  212. \Description
  213. \var{HideMouse} hides the mouse cursor. This may or may not be implemented
  214. on all systems, and depends on the driver.
  215. \Errors
  216. None.
  217. \SeeAlso
  218. \seep{ShowMouse}
  219. \end{procedure}
  220. \FPCexample{ex5}
  221. \begin{procedure}{InitMouse}
  222. \Declaration
  223. Procedure InitMouse;
  224. \Description
  225. \var{InitMouse} Initializes the mouse driver. This will allocate any data
  226. structures needed for the mouse to function. All mouse functions can be
  227. used after a call to \var{InitMouse}.
  228. A call to \var{InitMouse} must always be followed by a call to \seep{DoneMouse}
  229. at program exit. Failing to do so may leave the mouse in an unusable state,
  230. or may result in memory leaks.
  231. \Errors
  232. None.
  233. \SeeAlso
  234. \seep{DoneMouse}, \seef{DetectMouse}
  235. \end{procedure}
  236. For an example, see most other functions.
  237. \begin{function}{PollMouseEvent}
  238. \Declaration
  239. Function PollMouseEvent(var MouseEvent: TMouseEvent):boolean;
  240. \Description
  241. \var{PollMouseEvent} checks whether a mouse event is available, and
  242. returns it in \var{MouseEvent} if one is found. The function result is
  243. \var{True} in that case. If no mouse event is pending, the function result
  244. is \var{False}, and the contents of \var{MouseEvent} is undefined.
  245. Note that after a call to \var{PollMouseEvent}, the event should still
  246. be removed from the mouse event queue with a call to \var{GetMouseEvent}.
  247. \Errors
  248. None.
  249. \SeeAlso
  250. \seep{GetMouseEvent}, \seep{PutMouseEvent}
  251. \end{function}
  252. \begin{procedure}{PutMouseEvent}
  253. \Declaration
  254. Procedure PutMouseEvent(const MouseEvent: TMouseEvent);
  255. \Description
  256. \var{PutMouseEvent} adds \var{MouseEvent} to the input queue. The next
  257. call to \seep{GetMouseEvent} or \var{PollMouseEvent} will then return
  258. \var{MouseEvent}.
  259. Please note that depending on the implementation the mouse event queue
  260. can hold only one value.
  261. \Errors
  262. None.
  263. \SeeAlso
  264. \seep{GetMouseEvent}, \seef{PollMouseEvent}
  265. \end{procedure}
  266. \begin{procedure}{SetMouseDriver}
  267. \Declaration
  268. Procedure SetMouseDriver(Const Driver : TMouseDriver);
  269. \Description
  270. \var{SetMouseDriver} sets the mouse driver to \var{Driver}. This function
  271. should be called before \seep{InitMouse} is called, or after \var{DoneMouse}
  272. is called. If it is called after the mouse has been initialized, it does
  273. nothing.
  274. For more information on setting the mouse driver, \sees{mousedrv}.
  275. \Errors
  276. \SeeAlso
  277. \seep{InitMouse}, \seep{DoneMouse}, \seep{GetMouseDriver}
  278. \end{procedure}
  279. For an example, see \sees{mousedrv}
  280. \begin{procedure}{SetMouseXY}
  281. \Declaration
  282. Procedure SetMouseXY(x,y:word);
  283. \Description
  284. \var{SetMouseXY} places the mouse cursor on \var{X,Y}. X and Y are zero
  285. based character coordinates: \var{0,0} is the top-left corner of the screen,
  286. and the position is in character cells (i.e. not in pixels).
  287. \Errors
  288. None.
  289. \SeeAlso
  290. \seef{GetMouseX}, \seef{GetMouseY}
  291. \end{procedure}
  292. \FPCexample{ex7}
  293. \begin{procedure}{ShowMouse}
  294. \Declaration
  295. Procedure ShowMouse;
  296. \Description
  297. \var{ShowMouse} shows the mouse cursor if it was previously hidden. The
  298. capability to hide or show the mouse cursor depends on the driver.
  299. \Errors
  300. None.
  301. \SeeAlso
  302. \seep{HideMouse}
  303. \end{procedure}
  304. For an example, see \seep{HideMouse}
  305. \section{Writing a custom mouse driver}
  306. \label{se:mousedrv}
  307. The \file{mouse} has support for adding a custom mouse driver. This can be
  308. used to add support for mouses not supported by the standard \fpc{} driver,
  309. but also to enhance an existing driver for instance to log mouse events or
  310. to implement a record and playback function.
  311. The following unit shows how a mouse driver can be enhanced by adding some
  312. logging capabilities to the driver.
  313. \FPCexample{logmouse}