Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple mouse gestures
4/18/2009 - fixed: could not right-drag a file on timeout.

Function SimpleMouseGestures
Copy      Help
function# [$macroList] [flags] [minDistance] [^timeout_s] ;;flags: 4 four gestures

;Detects 8 simple mouse gestures: right, right-up, up, ...
;Call this function from a function that has trigger:
;;;;Mouse right or middle button.
;;;;"Eat" checked, "When release" unchecked.
;Waits until you release mouse button, detects drag direction, draws blue line, and optionally launches a macro.
;Returns drag direction, from 0 (~0 degrees) to 7 (~360-45 degrees).
;Fails if you did not release button within timeout_s seconds, or did not drag >= minDistance.
;If fails, clicks in the initial place and returns -1.

;macroList - list of macros.
;;;;Example: "m0[]m1[]m2[]m3[]m4[]m5[]m6[]m7"
;;;;First macro will run on gesture rigt (~0 degrees), second - right-up (~ 45 degrees), and so on.
;;;;Can be less than 8 lines. Some lines can be empty. Not error if a macro does not exist.
;;;;If macroList is empty, does not run a macro.
;;;;4 - detect 4 gestures. Return: 0 rigt, 1 up, 2 left, 3 down.
;minDistance - fail if dragged by less pixels. Default: 10.
;timeout_s - fail after timeout_s seconds. Default: 2.

QMITEM q; qmitem(getopt(itemid 3) 0 q)
int mb=q.tkey-7
if(q.ttype!2 or mb<0 or mb>1) end "must be mouse right or middle button trigger"
if(q.itype=0) end "must be function, not macro"

if(!minDistance) minDistance=10
if(!timeout_s) timeout_s=2

POINT p0; xm p0

int e
opt waitmsg 1
SMG_Draw 0 1 0 p0
wait timeout_s M; err e=1
SMG_Draw 0 2 0 0
if e
,goto g1

POINT p; xm p
int xd(p.x-p0.x) yd(p0.y-p.y)

if(_hypot(xd yd)<minDistance) e=2; goto g1

int d=AngleDegrees(xd yd)
if(flags&4) d=Round(d/90.0)&3; else d=Round(d/45.0)&7

,ARRAY(str) a=macroList
,,if(_s.len) mac _s; err

ret d

spe 10
if e=1
,if(mb) mid+ p0.x p0.y 0 4
,else rig+ p0.x p0.y 0 4
,if(mb) mid p0.x p0.y 0 4
,else rig p0.x p0.y 0 4
ret -1

Function SMG_Draw
Copy      Help
function a b c POINT&p0

int-- t_smg_h
POINT-- t_smg_pp

sel b
,case 1 ;;start
,t_smg_h=CreateWindowEx(WS_EX_TOOLWINDOW|WS_EX_TOPMOST|WS_EX_LAYERED|WS_EX_TRANSPARENT|WS_EX_NOACTIVATE "#32770" 0 WS_POPUP p0.x-200 p0.y-200 400 400 0 0 _hinst 0)
,SetLayeredWindowAttributes(t_smg_h GetSysColor(COLOR_BTNFACE) 200 3)
,hid- t_smg_h
,t_smg_pp.x=200; t_smg_pp.y=200
,SetTimer(t_smg_h 1 10 &SMG_Draw)
,case 2 ;;stop
,KillTimer t_smg_h 1
,DestroyWindow t_smg_h
,case else ;;timer
,xm p2 t_smg_h 0
,if(!memcmp(&t_smg_pp &p2 sizeof(POINT))) ret
,int hdc=GetDC(t_smg_h)
,RECT r.right=400; r.bottom=400; FillRect(hdc &r COLOR_BTNFACE+1)
,int hpen(CreatePen(0 8 0xff0000)) oldpen(SelectObject(hdc hpen))
,MoveToEx(hdc 200 200 0); LineTo(hdc p2.x p2.y)
,DeleteObject SelectObject(hdc oldpen)
,ReleaseDC(t_smg_h hdc)

Function AngleDegrees
Copy      Help
function# x y

;Returns angle in degrees, 0-359.

int d=Round(atan2(y x)*57.2957795130786) ;;*(180/pi)
if(d<0) d=360+d
ret d


Sample function for right button gestures. Recognizes 8 gestures. Just displays recognized gesture number, from 0 to 7.
Function Simple_mouse_gestures
Trigger #R 0x8     Help - how to add the trigger to the macro
Copy      Help
out SimpleMouseGestures()

Sample function for middle button gestures. Recognizes 4 gestures. Launches macros m1-m4, if exist.
Function Simple_mouse_gestures2
Trigger #M 0x8     Help - how to add the trigger to the macro
Copy      Help
SimpleMouseGestures "m1[]m2[]m3[]m4" 4

i copied all code into a folder.
the code looks quite interesting.

i have to test it on a virtual machine before giving valuable feedback.

Forum Jump:

Users browsing this thread: 1 Guest(s)