I've made this function that will tell you whether the mouse or a key as been pressed, double pressed, press and hold or double press and hold. As stated above it works for both keyboard and mouse, and probably would work for a joypad.
I'd like some comments of it, and if there is another/better way of achiving this.
Here goes the code:
sync on
sync rate 60
n=13
dim Btm_Timer(n) as integer
dim btm(n) as integer
dim t$(n) as string
do
cls
if spacekey()=1 and none=1 and f=0 then none=0 : f=1
if spacekey()=1 and none=0 and f=0 then none=1 : f=1
if spacekey()=0 then f=0
for i=1 to 3
Btm_Prs(0,i,0,0)
if btm(i)=0 and none=0 then t$(i)="none"
if btm(i)=1 then t$(i)="click"
if btm(i)=2 then t$(i)="click hold"
if btm(i)=3 then t$(i)="double click"
if btm(i)=4 then t$(i)="double click hold"
next i
for i=4 to n
Btm_Prs(i,keystate(i-2),0,1)
if btm(i)=0 and none=0 then t$(i)="none"
if btm(i)=1 then t$(i)="click"
if btm(i)=2 then t$(i)="click hold"
if btm(i)=3 then t$(i)="double click"
if btm(i)=4 then t$(i)="double click hold"
next i
print btm(1)
for i=1 to n
print str$(Btm_Timer(i))+" "+"Buttom "+str$(i)+":"+t$(i)
next i
sync
loop
function Btm_Prs(b,m,t,flag_timer)
`b= where to save the data / enables mouse buttom recognition
`m= wich key to read (keystate()), on mouse mode, used to establish wich buttom 1=left ;
`2=right ; 3=middle
`t= time taken to know a click/double click happens
if t<10 then t=10
if b<>0
mc=1
r=1
else
if m<1 then m=1
if m>3 then m=3
b=m
r=m
mc=2^(m-1)
m=mouseclick()
endif
if flag_timer=1
if ((m ~~ 7) && r)=r and btm(b)=0 and Btm_Timer(b)>0 then btm(b)=10
if ((m ~~ 7) && r)=r and btm(b)=1 then btm(b)=0
endif
if (m && mc)=r and btm(b)=0 then btm(b)=10+flag_timer : Btm_Timer(b)=t
if (m && mc)=r and (btm(b)=11-flag_timer or btm(b)=1) then btm(b)=5 : Btm_Timer(b)=t
if (btm(b)=11) and ((m ~~ 7) && r)=r
if Btm_Timer(b)=0 or flag_timer=1
if btm(b)=11 then Btm_Timer(b)=1+t*flag_timer
btm(b)=1
`"click"
endif
endif
if (btm(b)=10+flag_timer or btm(b)=2) and (m && mc)=r
if Btm_Timer(b)<=t-10
btm(b)=2
Btm_Timer(b)=1
`"clickhold"
endif
endif
if (btm(b)=5) and ((m ~~ 7) && r)=r
if Btm_Timer(b)=0 or flag_timer=1
if btm(b)=5 then Btm_Timer(b)=1
btm(b)=3
`"double click"
endif
endif
if (btm(b)=5 or btm(b)=4) and (m && mc)=r
if Btm_Timer(b)<=t-10
btm(b)=4
Btm_Timer(b)=1
`"double click hold"
endif
endif
if ((m ~~ 7) && r)=r and Btm_Timer(b)=0 then btm(b)=0
if ((m ~~ 7) && r)=r and btm(b)=10 and flag_timer=0 then btm(b)=11
if ((m ~~ 7) && r)=r and btm(b)=5 then Btm_Timer(b)=0
if Btm_Timer(b)>0 then dec Btm_Timer(b)
endfunction
A quick review of what each parameter does:
- "b" you tell where to save it within the array btm(n). This is for keys only, for mouse you put 0 , and it will save the 3 mouse buttoms states on b=1, b=2 and b=3 , so if your to use both mouse and keys, save your keys begining 4.
- "m" wich key to read (ex: keystate(2)). If b=0 so to read mouse input, you state whether 1(left) , 2(right) or 3(center) buttom.
- "t" state the time taken between click/double-click recognition.
- "flag_timer" this one is tricky. A value of 0 states that you want to recognize a buttom once the timer has ended, a value of 1 will activate the state of the mouse either the timer has ended or not. This is preaty much like shooting a weapon once the buttom has gone from unpressed to a pressed state (flag_timer=1) or from a pressed state to a unpressed state (flag_timer=0).
Please do comment, and thanks for your time.
Further on my stuff at...