15.2 Functions and procedures

DetectMouse

Declaration:
Function DetectMouse:byte;
Description:
DetectMouse detects whether a mouse is attached to the system or not. If there is no mouse, then zero is returned. If a mouse is attached, then the number of mouse buttons is returned.

This function should be called after the mouse driver was initialized.

Errors:
None.
See also:
InitMouse (523),DoneMouse (505),

Listing: mouseex/ex1.pp


Program Example1;

{ Program to demonstrate the DetectMouse function. }

Uses mouse;

Var
  Buttons : Byte;

begin
  InitMouse;
  Buttons:=DetectMouse;
  If Buttons=0 then
    Writeln('No mouse present.')
  else
    Writeln('Found mouse with ',Buttons,' buttons.');
  DoneMouse;
end.

DoneMouse

Declaration:
Procedure DoneMouse;
Description:
DoneMouse De-initializes the mouse driver. It cleans up any memory allocated when the mouse was initialized, or removes possible mouse hooks from memory. The mouse functions will not work after DoneMouse was called. If DoneMouse is called a second time, it will exit at once. InitMouse should be called before DoneMouse can be called again.
Errors:
None.
See also:
DetectMouse (505), InitMouse (523)

For an example, see most other mouse functions.

GetMouseButtons

Declaration:
Function GetMouseButtons:word;
Description:
GetMouseButtons returns the current button state of the mouse, i.e. it returns a or-ed combination of the following constants:
MouseLeftButton
When the left mouse button is held down.
MouseRightButton
When the right mouse button is held down.
MouseMiddleButton
When the middle mouse button is held down.
Errors:
None.
See also:
GetMouseEvent (507), GetMouseX (507), GetMouseY (508)

Listing: mouseex/ex2.pp


Program Example2;

{ Program to demonstrate the GetMouseButtons function. }

Uses mouse;

begin
  InitMouse;
  Writeln('Press right mouse button to exit program');
  While (GetMouseButtons<>MouseRightButton) do ;
  DoneMouse;
end.

GetMouseDriver

Declaration:
Procedure GetMouseDriver(Var Driver : TMouseDriver);
Description:
GetMouseDriver returns the currently set mouse driver. It can be used to retrieve the current mouse driver, and override certain callbacks.

A more detailed explanation about getting and setting mouse drivers can be found in section 15.3, page 513.

Errors:
None.
See also:
SetMouseDriver (510)

For an example, see the section on writing a custom mouse driver, section 15.3, page 513

GetMouseEvent

Declaration:
Procedure GetMouseEvent(var MouseEvent:TMouseEvent);
Description:
GetMouseEvent returns the next mouse event (a movement, button press or button release), and waits for one if none is available in the queue.

Some mouse drivers can implement a mouse event queue which can hold multiple events till they are fetched.; Others don’t, and in that case, a one-event queue is implemented for use with PollMouseEvent (510).

Errors:
None.
See also:
GetMouseButtons (506), GetMouseX (507), GetMouseY (508)

GetMouseX

Declaration:
Function GetMouseX:word;
Description:
GetMouseX returns the current X position of the mouse. X is measured in characters, starting at 0 for the left side of the screen.
Errors:
None.
See also:
GetMouseButtons (506),GetMouseEvent (507), GetMouseY (508)

Listing: mouseex/ex4.pp


Program Example4;

{ Program to demonstrate the GetMouseX,GetMouseY functions. }

Uses mouse;

Var
  X,Y : Word;

begin
  InitMouse;
  Writeln('Move mouse cursor to square 10,10 to end');
  Repeat
    X:=GetMouseX;
    Y:=GetMouseY;
    Writeln('X,Y= (',X,',',Y,')');
  Until (X=9) and (Y=9);
  DoneMouse;
end.

GetMouseY

Declaration:
Function GetMouseY:word;
Description:
GetMouseY returns the current Y position of the mouse. Y is measured in characters, starting at 0 for the top of the screen.
Errors:
None.
See also:
GetMouseButtons (506),GetMouseEvent (507), GetMouseX (507)

For an example, see GetMouseX (507)

HideMouse

Declaration:
Procedure HideMouse;
Description:
HideMouse hides the mouse cursor. This may or may not be implemented on all systems, and depends on the driver.
Errors:
None.
See also:
ShowMouse (532)

Listing: mouseex/ex5.pp


Program Example5;

{ Program to demonstrate the HideMouse function. }

Uses mouse;

Var
  Event : TMouseEvent;
  Visible: Boolean;

begin
  InitMouse;
  ShowMouse;
  Visible:=True;
  Writeln('Press left mouse button to hide/show, right button quits');
  Repeat
   GetMouseEvent(Event);
   With Event do
     If (Buttons=MouseLeftbutton) and
        (Action=MouseActionDown) then
       begin
       If Visible then
         HideMouse
       else
         ShowMouse;
       Visible:=Not Visible;
       end;
  Until (Event.Buttons=MouseRightButton) and
        (Event.Action=MouseActionDown);
  DoneMouse;
end.

InitMouse

Declaration:
Procedure InitMouse;
Description:
InitMouse Initializes the mouse driver. This will allocate any data structures needed for the mouse to function. All mouse functions can be used after a call to InitMouse.

A call to InitMouse must always be followed by a call to DoneMouse (505) at program exit. Failing to do so may leave the mouse in an unusable state, or may result in memory leaks.

Errors:
None.
See also:
DoneMouse (505), DetectMouse (505)

For an example, see most other functions.

PollMouseEvent

Declaration:
Function PollMouseEvent(var MouseEvent: TMouseEvent):boolean;
Description:
PollMouseEvent checks whether a mouse event is available, and returns it in MouseEvent if one is found. The function result is True in that case. If no mouse event is pending, the function result is False, and the contents of MouseEvent is undefined.

Note that after a call to PollMouseEvent, the event should still be removed from the mouse event queue with a call to GetMouseEvent.

Errors:
None.
See also:
GetMouseEvent (507), PutMouseEvent (510)

PutMouseEvent

Declaration:
Procedure PutMouseEvent(const MouseEvent: TMouseEvent);
Description:
PutMouseEvent adds MouseEvent to the input queue. The next call to GetMouseEvent (507) or PollMouseEvent will then return MouseEvent.

Please note that depending on the implementation the mouse event queue can hold only one value.

Errors:
None.
See also:
GetMouseEvent (507), PollMouseEvent (510)

SetMouseDriver

Declaration:
Procedure SetMouseDriver(Const Driver : TMouseDriver);
Description:
SetMouseDriver sets the mouse driver to Driver. This function should be called before InitMouse (523) is called, or after DoneMouse is called. If it is called after the mouse has been initialized, it does nothing.

For more information on setting the mouse driver, section 15.3, page 513.

Errors:
See also:
InitMouse (523), DoneMouse (505), GetMouseDriver (506)

For an example, see section 15.3, page 513

SetMouseXY

Declaration:
Procedure SetMouseXY(x,y:word);
Description:
SetMouseXY places the mouse cursor on X,Y. X and Y are zero based character coordinates: 0,0 is the top-left corner of the screen, and the position is in character cells (i.e. not in pixels).
Errors:
None.
See also:
GetMouseX (507), GetMouseY (508)

Listing: mouseex/ex7.pp


Program Example7;

{ Program to demonstrate the SetMouseXY function. }

Uses mouse;

Var
  Event : TMouseEvent;

begin
  InitMouse;
  Writeln('Click right mouse button to quit.');
  SetMouseXY(40,12);
  Repeat
    If (GetMouseX>70) then
      SetMouseXY(10,GetMouseY);
    If (GetMouseY>20) then
      SetMouseXY(GetMouseX,5);
    GetMouseEvent(Event);
  Until (Event.Buttons=MouseRightButton) and
        (Event.Action=MouseActionDown);
  DoneMouse;
end.

ShowMouse

Declaration:
Procedure ShowMouse;
Description:
ShowMouse shows the mouse cursor if it was previously hidden. The capability to hide or show the mouse cursor depends on the driver.
Errors:
None.
See also:
HideMouse (522)

For an example, see HideMouse (522)