This works, but you may have to pick the bones out of it to understand it. It's commented but can still be a bit tricky. This does more than just clicks, it also does swipes which are also not registered on an Ultrabook with the current version.
My advice would be to simply call
ret = inputCheck()
each loop, and then check the results in ret, and also the gInput variable.
gInput.WindowTouchMode
must be set to 1 for clicks to work.
If anyone can simplify this, please do so and post your improved version here! This one kind of evolved as I tweaked it over time.
You can remove my debug lines, they will stop it compiling.
`*********************************************************
function checkInput()
`*********************************************************
ret = 0 : ` No change
gInput.x = getPointerX()
gInput.y = getPointerY()
`*** Window click mode
`*** once a "pressed" has been detected we know the user is actually swiping
` In this mode, the touch is always instantaneous, since the mouse moves
` but no events are detected. Therefore if the mouse moves, it's a swipe
if gInput.pressed = 0 and gInput.windowTouchMode
`*** If clicked, then unclick
if gInput.clicked = 1
gInput.clicked = 0
endif
if gInput.windowX <> gInput.x or gInput.WindowY <> gInput.y
debugWriteSave("Mouse Moved : " + str(gInput.windowX) + "," + str(gInput.windowY) + " => " + str(gInput.X) + "," + str(gInput.Y))
`*** Mouse has moved ***
if gInput.windowTouch = 0
`* If pointer moved and touch = 0, register potential click
gInput.windowTouch = g.time
debugWriteSave("Potential Click")
else
` Mouse moved further, not a click
windowPressed = gInput.windowTouch
gInput.windowTouch = 0
debugWriteSave("Moved again - no click")
endif
gInput.windowX = gInput.x
gInput.windowY = gInput.y
else
`***Mouse has not moved since a click 0.2 seconds ago***
if gInput.windowTouch > 0
if gInput.windowTouch < g.time - 0.200
setInput(1,0,0,0,0)
debugWriteSave("Windows Pointer Clicked")
gInput.windowTouch = 0
exitFunction cINPUT_CLICK
endif
endif
endif
endif
`If currently pressed, do updates
if gInput.pressed > 0
gInput.pressTime = g.time - gInput.pressed
endif
`Irrespective of previous state, if pressed then set
if getPointerPressed() = 1 or windowPressed > 0
if windowPressed > 0
gInput.pressed = windowPressed
else
gInput.pressed = g.time
endif
gInput.pressTime = 0
gInput.clicked = 0
gInput.swiped = 0
gInput.startX = gInput.x
gInput.startY = gInput.y
ret = 1
debugWriteSave("Pointer pressed : " + str(gInput.startX) + "," + str(gInput.startY))
exitfunction cINPUT_NONE
else
` If it was pressed but not now then tidy up
if getPointerReleased() = 1
gInput.pressed = 0
thisX = gInput.x
thisY = gInput.y
distX = thisX - gInput.startX
distY = thisY - gInput.startY
debugWriteSave("Input travel - " + str(distX) + "," + str(distY))
`Check if single click (less than minimum swipe, and less than 0.4 secs)
if gInput.pressTime < 0.4
if abs(distX) < gInput.minSwipe and abs(distY) < gInput.minSwipe
setInput(1,0,0,0,0)
debugWriteSave("Pointer Clicked")
exitfunction cINPUT_CLICK
endif
endif
`Check Swipes (must be a swipe if not a click). No time limit, swipes can be quick
` Horizontal angle : tan(angle) = y/x
` If within 60 degrees then swiped horizontally
dist# = sqrt((distX * distX) + (distY * distY)) //A ORC005
if dist# > gInput.minSwipe //A ORC005
angle# = aTanFull(distX, distY)
debugWriteSave("Angle of Swipe = " + str(angle#))
if (angle# < 60 or angle# > 300) then iUp = 1
if (angle# > 120 and angle# < 240) then iDown = 1
if (angle# > 30 and angle# < 150) then iRight = 1
if (angle# > 210 and angle# < 330) then iLeft = 1
setInput(0,iLeft, iRight, iUp, iDown)
debugWriteSave("Pointer Swiped")
exitfunction cINPUT_SWIPE
endif //A ORC005
endif
endif
` If Pointer state = 1...
` Because may not be checked every cycle, check pressed is registered
if getPointerState() = 1 and gInput.pressed = 0
gInput.pressed = g.time
gInput.pressTime = 0
gInput.startX = gInput.x
gInput.startY = gInput.y
ret = 1
exitfunction cINPUT_NONE
endif
endfunction cINPUT_NONE
It also uses this Typed variable:
type tInput
x as float
y as float
pressed
pressTime as float
clicked
minSwipe
swiped
swipeLeft
swipeRight
swipeUp
swipeDown
startX as float
startY as float
windowTouchMode
windowTouch as float
windowX as float
windowY as float
endtype
and these constants:
#constant cINPUT_NONE = 0
#constant cINPUT_CLICK = 1
#constant cINPUT_SWIPE = 2