const
{ We have an errorcode base of 1030 }
errMouseBase = 1030;
errMouseInitError = errMouseBase + 0;
errMouseNotImplemented = errMouseBase + 1;
The following constants describe which action a mouse event describes
const
MouseActionDown = $0001; { Mouse down event }
MouseActionUp = $0002; { Mouse up event }
MouseActionMove = $0004; { Mouse move event }
The following constants describe the used buttons in a mouse event:
MouseLeftButton = $01; { Left mouse button }
MouseRightButton = $02; { Right mouse button }
MouseMiddleButton = $04; { Middle mouse button }
The mouse unit has a mechanism to buffer mouse events. The following
constant defines the size of the event buffer:
MouseEventBufSize = 16;
PMouseEvent=^TMouseEvent;
TMouseEvent=packed record { 8 bytes }
buttons : word;
x,y : word;
Action : word;
end;
The Buttons field describes which buttons were down when the event
occurred. The x,y fields describe where the event occurred on the
screen. The Action describes what action was going on when the event
occurred. The Buttons and Action field can be examined using the
above constants.
The following record is used to implement a mouse driver in the SetMouseDriver function:
TMouseDriver = Record UseDefaultQueue : Boolean; InitDriver : Procedure; DoneDriver : Procedure; DetectMouse : Function : Byte; ShowMouse : Procedure; HideMouse : Procedure; GetMouseX : Function : Word; GetMouseY : Function : Word; GetMouseButtons : Function : Word; SetMouseXY : procedure (x,y:word); GetMouseEvent : procedure (var MouseEvent:TMouseEvent); PollMouseEvent : function (var MouseEvent: TMouseEvent):boolean; PutMouseEvent : procedure (Const MouseEvent:TMouseEvent); end;Its fields will be explained in the section on writing a custom driver.
MouseIntFlag : Byte; { Mouse in int flag }
MouseButtons : Byte; { Mouse button state }
MouseWhereX,
MouseWhereY : Word; { Mouse position }
This function should be called after the mouse driver was initialized.
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.
For an example, see most other mouse functions.
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.
A more detailed explanation about getting and setting mouse drivers can be found in section mousedrv.
For an example, see the section on writing a custom mouse driver, section mousedrv
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.
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.
For an example, see GetMouseX
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.
A call to InitMouse must always be followed by a call to DoneMouse at program exit. Failing to do so may leave the mouse in an unusable state, or may result in memory leaks.
For an example, see most other functions.
Note that after a call to PollMouseEvent, the event should still be removed from the mouse event queue with a call to GetMouseEvent.
Please note that depending on the implementation the mouse event queue can hold only one value.
For more information on setting the mouse driver, section mousedrv.
For an example, see section mousedrv
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.
For an example, see HideMouse
The following unit shows how a mouse driver can be enhanced by adding some
logging capabilities to the driver.