Reason for missing mouseclicks could be that your machine is not fast enough.
To explain the point very simply.
When program executes, it basically goes:
Code: Select all
DisplayGrid()
IsLeftMouse()
DisplayGrid()
IsLeftMouse()
DisplayGrid()
IsLeftMouse()
...
Now this leftmouse is checked only when it is executing
IsLeftMouse()
By other words, if rest of the stuff, in this case, DisplayGrid() execution takes long, then if you click your mouse and release it up before it ends executing DisplayGrid, it will miss the Click, because when it gets to
IsLeftMouse(), Mouse would already be up and therefore it wouldn't react to it.
Instead of using IsLeftMouse, you could use InstallEventHandler to assign Event Handler for Mouse Button being pressed and put
WaitEvent() to your program, this way it shouldn't miss a mouse click.
from manual:
Code: Select all
Function p_HandlerFunc(msg)
Switch(msg.action)
Case "ActiveWindow":
DebugPrint("Window has become active again!")
Case "InactiveWindow":
DebugPrint("Window has become inactive!")
Case "MoveWindow":
DebugPrint("User has moved the window to", msg.x, msg.y)
Case "OnKeyDown":
If msg.key = "ESC" Then End
EndSwitch
EndFunction
InstallEventHandler({ActiveWindow = p_HandlerFunc,
InactiveWindow = p_HandlerFunc,
MoveWindow = p_HandlerFunc,
OnKeyDown = p_HandlerFunc})
Repeat
WaitEvent
Forever
In your case:
Code: Select all
InstallEventHandler({OnMouseDown=Function() Debugprint("Mouse was put down") EndFunction})
Repeat
WaitEvent
Forever
Here is example code on how to make a draggable box that you could perhaps use to move your mahjong pieces around:
Code: Select all
MP = {X=100, Y=100, W=100, H=100, MouseXOffSet=0, MouseYOffSet=0} /* MP = Mahjong Piece */
Function UpdateScreen()
DrawStuff()
CreateButtons()
EndFunction
Function DrawStuff()
Cls(#BLACK)
SetFillStyle(#FILLCOLOR)
Box(MP.X, MP.Y, MP.W, MP.H, #RED)
EndFunction
Function CreateButtons()
DeleteButton(1)
MakeButton(1, #SIMPLEBUTTON, MP.X, MP.Y, MP.W, MP.H,
{OnMouseDown = Function()
MP.MouseXOffset = MouseX() - MP.X
MP.MouseYOffset = MouseY() - MP.Y
MouseIntervalID = SetInterval(Nil, Function()
Local EndInterval=False
If IsLeftMouse() = False Then EndInterval = True
If EndInterval = False
MP.X = MouseX() - MP.MouseXOffset
MP.Y = MouseY() - MP.MouseYOffset
DrawStuff()
Else
ClearInterval(MouseIntervalID)
UpdateScreen()
EndIf
EndFunction, 10
)
EndFunction
})
EndFunction
MakeButton(1, #SIMPLEBUTTON, 2, 2, 2, 2, {OnMouseDown=Function() EndFunction}) /* This is here only because I am calling DeleteButton at beginning of CreateButtons function*/
UpdateScreen()
Repeat
WaitEvent
Forever