mouse.tex 11 KB

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