 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
rodfell
Joined: 05 Oct 2007 Posts: 57 Location: Bundaberg (Bundy), Qld, Australia
|
Posted: Tue Nov 06, 2007 12:18 pm Post subject: simple mouse gesture with rocker |
|
|
Simple Mouse Gestures with Rocker
Latest version is posted here
Simple Mouse Gestures with Rocker call subroutines based on a gesture made when the right(r), middle(m), 4th(x) or fifth(f) mouse button is held down.
It can be easily modified to accept other keys your mouse may have (see logitech mx620 example)
the script tracks up (u) down (d) left (l) right (r). There is a tolerance of +/- 45 degrees, so it is quite simple and accurate
subroutines will be called if labelled with the following syntax:
gabcde-z:
"a" is the first mouse button clicked (left right or 4th mouse button - l, r,x or f)
"b" is the first mouse movement (if any) (u, d, l or r)
"c" is the second mouse movement (if any) "d" is the third etc. Any number of movements is permitted including none.
"-z" is appended if the gesture is terminated with another mouse click (l, r,x or f but "a" cannot be the same as "z")
"a" can only be left(l) in a pure rocker where there is no gesture tracking (no gesture tracking while left mouse button down)
ga:
simple mouse click. "a" can be r,m,x or f (easily modified to accept other keys your mouse may have)
gabc:
pure gesture with no second mouse click. "a" cannot be left(l). any number of movements allowed ie gab: , gabc: , gabcd: etc
ga-z:
pure rocker (no gesture tracking). Any combination of l, r,x or f is allowed except "a" and "z" must but be different (can't rock with only one button:)
after a rocking action is called, if "a" remains down, another rocking action can be called by clicking "z" again (or another mouse button can be used)
gabcdewheelup:
gabcdewheeldown:
a,b,c,d,e etc same as above
b,c,d,e etc all optional
wheel movement can be repeated while mouse button held down
examples
gx: 4th mouse button clicked and released
grur-x: right button held down, mouse moved up then right and then the 4th mouse button is clicked
gr-l: right mouse button is held down and the left mouse button clicked
grwheelup: right mouse button held down and mouse wheel is moved upwards. Wheel movement can be
repeated while mouse button held down
grl: right mouse button held down, mouse moved left and right button released
by not allowing diagonals, gestures can be drawn without abrupt angles. eg a circle drawn from the top and clockwise is registered as grrdlu: (right down left up)
if a right mouse button gesture is down (d) and right (r), like an "L", a subroutine called grdr (for gesture with right mouse button, tracking down then right) will be called
the script also pays attention to the window under the gesture and notes if the gesture is done over the desktop
Below is the latest version.
| Code: | coordmode,mouse,screen
winget,window0,id,ahk_class Progman ;window0 is the desktop
hotkey,lbutton,off ;normal left click function only affected during gesture
browser_search:: ;"find" key on logitech mx620 mouse, used here as an example
lbutton::
rbutton::
mbutton::
xbutton1::
xbutton2::
thishotkey:=gesturebutton()
winget,windowA,id,A ;windowA is the active window, needed for some gestures
if not buttonsdown
mousetrack= ;clear previous tracking, but not if rocking
wheel=
gestmode=true
buttonsdown.=thishotkey ;tracks which mouse buttons down
rocker:=(getkeystate("lbutton","p") ? "l" : "") . buttonsdown
rocker:=regexreplace(rocker,thishotkey,"")
rocker.=(if rocker ? "-" : "") . thishotkey
hotkey,lbutton,on ;disables normal left click during gesture
if (window1<>window0)
window2:=window1 ;used in some gestures
mousegetpos xpos1,ypos1,window1 ;window1 is the window underneath the current gesture
if strlen(rocker)=1
settimer,mousetrack,10
else
gosub rbutton up ;rocker, no (further) tracking needed
return
mousetrack:
mousegetpos xpos2,ypos2
track:=(abs(ypos1-ypos2)>=abs(xpos1-xpos2)) ? (ypos1>ypos2 ? "u" : "d") : (xpos1>xpos2 ? "l" : "r") ;determines up down left right
if (abs(ypos1-ypos2)>4 or abs(xpos1-xpos2)>4) and (track<>SubStr(mousetrack, 0, 1)) ;track if x or y changing and direction changing
mousetrack.=track
xpos1:=xpos2
ypos1:=ypos2
return
browser_search up::
rbutton up::
mbutton up::
xbutton1 up::
xbutton2 up::
thishotkey:=gesturebutton()
buttonsdown:=regexreplace(buttonsdown,thishotkey,"")
if not buttonsdown
hotkey,lbutton,off ;if no button held down, restore normal left mouse button function
if not gestmode
return
gestmode:=false
if wheel
return
callgesture: ;entry label for wheel gestures
settimer,mousetrack,off
gesture:="g" . substr(rocker,1,1) . mousetrack . substr(rocker,2) . wheel
if islabel(gesture) ;checks if gesture is labelled
gosub %gesture%
else ;delete after familiar with use
msgbox,,,%gesture%,1 ;delete after familiar with use
return
wheelup::
wheeldown::
wheel:=a_thishotkey="wheelup" ? "wheelup" : "wheeldown"
if buttonsdown
{
rocker:=buttonsdown
gosub callgesture
}
else
sendinput {%wheel%}
return
gesturebutton()
{
thishotkey:=substr(a_thishotkey,1,1)
if A_thishotkey contains xbutton2 ;needed as xbutton1 and xbutton2 share the same first letter
thishotkey=f
return thishotkey
}
gr: ;normal right click
click right,xpos1,ypos1
exit
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;; Examples ;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; wheel examples ;;;;;;;;;;
gr-wheelup: ;change volume with wheel - hold right button down and use wheel
Send {Volume_Up}
return
gr-wheeldown:
Send {Volume_Down}
return
;;;;;;;; rocker examples ;;;;;;;;;;
gr-l:
msgbox,,,keep right held down and click left will repeat action,5
return
gl-r:
msgbox,,,rocker left then right. careful where you click,5
return
grl-l:
msgbox,,,you gestured left and clicked,5
return
;;;;;;;;;;;; 4th mouse button example ;;;;;;;;;;
gx-l:
msgbox,,,you've gestures left with the 4th mouse button,5
return
gx-r:
msgbox,,,rocker 4th button then right,5
return
gr-x:
msgbox,,,rocker right then 4th button,5
return
;;;;;;;; other examples ;;;;;;;;;;;;;;
gru: ;; gesture up ;; maximize window under cursor. maximize last window if cursor over desktop
if (window1=window0)
{
if (window2<>"")
winmaximize, ahk_id %window2%
}
else
winmaximize ahk_id %window1%
return
grd: ;minimize window under cursor
if (window1<>window0) ;don't minimize the desktop
winminimize, ahk_id %window1%
return
grl: ;browser back
winactivate ahk_id %window1%
sendinput {Browser_Back}
return
grr: ;browser forward
winactivate ahk_id %window1%
sendinput {Browser_Forward}
return
grdr: ;restore last window.
winrestore ahk_id %window2%
return
grldr: ;close window under cursor (draw a C)
if (window1<>window0)
winclose ahk_id %window1%
return |
Last edited by rodfell on Sat Aug 30, 2008 7:16 am; edited 39 times in total |
|
| Back to top |
|
 |
ManaUser
Joined: 24 May 2007 Posts: 906
|
Posted: Tue Nov 06, 2007 9:24 pm Post subject: |
|
|
Nice! It's simple and easy to use, but quite serviceable.
I made a slight change to your script though (hope you don't mind). Instead of using five variables, it just adds progressively adds letters to one. This way it can handle any number of strokes, and it actually simplifies the code a bit (no more loops). I added a new six-stroke gesture, M, as an example.
| Code: | ;this script tracks up (u) down (d) left (l) and right (r). There is a tolerance of +/- 45 degrees, so it is quite simple and accurate
;gestures can have curves. eg a circle drawn from the top and clockwise is registered as rdlu (right down left up)
;if a gesture is down (d) and right (r), like an "L", a subroutine called gdr ( for gesture down right) will be called
;if anything is labelled as gdr:, the action will be run
;as a further example, if an "S" is drawn (from top to bottom), gldrdl: (gesture left down right down left) will be called
;these 2 examples are included, as is "M" (U R D U R D)
CoordMode, Mouse, Screen
rbutton::
mousegetpos,xpos1, ypos1
gesture= ;clear previous tracking results, variable array gesture1 to gesture5
settimer, gesture, 10
return
rbutton up::
settimer, gesture, off
gesture := "g" . gesture ;eg. gdr = gesture down right, gestures 3,4 and 5 did not occur
if islabel(gesture)<>0 ;checks if gesture is labelled
gosub, %gesture% ; eg. gosub, gdr
;msgbox,,, %gesture% not yet defined,4 ;delete this line once set up
return
gesture:
mousegetpos,xpos2, ypos2
dx:=xpos2-xpos1
dy:=ypos1-ypos2
if (abs(dy)>=abs(dx))
{
if (dy>0)
track=u ;track is up
else
track=d ;down
}
else
{
if (dx>0)
track=r ;right
else
track=l ;left
}
if (abs(dy)<4 and abs(dx)<4)
track= ;not tracking at all if no significant change in x or y
xpos1:=xpos2
ypos1:=ypos2
if (track<>SubStr(gesture, 0, 1)) ;ignore track if not changing since previous track
gesture := gesture . track
return
g: ;normal right click
click, right, xpos1, ypos1
exit
gdr: ;delete examples when set up
msgbox, you drew an L
exit
gldrdl:
msgbox, you drew an S
exit
gurdurd:
msgbox, you drew an M
exit |
|
|
| Back to top |
|
 |
rodfell
Joined: 05 Oct 2007 Posts: 57 Location: Bundaberg (Bundy), Qld, Australia
|
Posted: Wed Nov 07, 2007 11:01 am Post subject: substr |
|
|
yeah i like that. i haven't got around to working out the substr function yet (still a bit of a noob). However, I would draw an M as (from bottom left) up down up down. As long as all angles are less than 45 degrees (fairly hard to make any greater than 45), the gesture for M would be gudud. N would be gudu. C would be gldr etc.
On my 24/7 ahk file, gu is maximize, gd is minimize, gl is back gr is forward. |
|
| Back to top |
|
 |
rodfell
Joined: 05 Oct 2007 Posts: 57 Location: Bundaberg (Bundy), Qld, Australia
|
Posted: Sun Nov 11, 2007 12:21 am Post subject: added a few more examples and tweaks |
|
|
added a few more examples and tweaks
| Code: |
;simple mouse gestures call subroutines based on a gesture (right button)
;the script tracks up (u) down (d) left (l) and right (r). There is a tolerance of +/- 45 degrees, so it is quite simple and accurate
;gestures can have curves. eg a circle drawn from the top and clockwise is registered as rdlu (right down left up)
;if a gesture is down (d) and right (r), like an "L", a subroutine called gdr ( for gesture down right) will be called
;if anything is labelled as gdr:, the action will be run
;as a further example, if an "S" is drawn (from top to bottom), gldrdl: (gesture left down right down left) will be called
;these 2 examples are included. See below for further examples
CoordMode, Mouse, Screen
winget, window0, id, ahk_class Progman ;not needed to make a gesture, window0 is used in subroutines called by gestures used on the desktop. see gu: subroutine example
rbutton::
if (window1<>window0) ;these 2 lines do not help the creation of gestures. The variable window2 can be used by subroutines called
window2:=window1 ;by the gestures eg. maximize previous window
mousegetpos,xpos1, ypos1,window1 ;window1 can be used by subroutines eg maximize window under cursor
gesture= ;clear previous tracking results, variable array gesture1 to gesture5
settimer, gesture, 10
return
rbutton up::
settimer, gesture, off
gesture := "g" . gesture ;eg. gdr = gesture down right
if islabel(gesture)<>0 ;checks if gesture is labelled
gosub, %gesture% ; eg. gosub, gdr
msgbox,,, %gesture% not yet defined,4
return
gesture:
mousegetpos,xpos2, ypos2
dx:=xpos2-xpos1
dy:=ypos1-ypos2
if (abs(dy)>=abs(dx))
{
if (dy>0)
track=u ;track is up
else
track=d ;down
}
else
{
if (dx>0)
track=r ;right
else
track=l ;left
}
if (abs(dy)<4 and abs(dx)<4)
track= ;not tracking at all if no significant change in x or y
xpos1:=xpos2
ypos1:=ypos2
if (track<>SubStr(gesture, 0, 1)) ;ignore track if not changing since previous track
gesture := gesture . track
return
g: ;normal right click
click, right, xpos1, ypos1
exit
;;;;;;; Examples ;;;;;;;;;;;;;;;;;;;;;;;;;;
gu: ;; gesture up ;; maximize window under cursor. maximize last window if cursor over desktop
if (window1=window0)
{
if (window2<>"")
winmaximize, ahk_id %window2%
}
else
winmaximize, ahk_id %window1%
exit
gd: ;minimize window under cursor
if (window1<>window0)
winminimize, ahk_id %window1%
exit
gl: ;browser back
winactivate, ahk_id %window1%
sendinput, {Browser_Back}
exit
gr: ;browser forward
winactivate, ahk_id %window1%
sendinput, {Browser_Forward}
exit
gdr: ;restore last window.
winrestore, ahk_id %window2%
exit
gldr: ;close window under cursor (draw a C)
if (window1<>window0)
winclose, ahk_id %window1%
exit
gldrdl:
msgbox, you drew an S
exit
gurdurd:
msgbox, you drew an M
exit
|
|
|
| Back to top |
|
 |
MarkyMark
Joined: 28 Sep 2007 Posts: 26
|
Posted: Sat Jan 19, 2008 3:30 pm Post subject: |
|
|
| Thanks rodfell for the script. Was looking for something lean and easy. Your works well. |
|
| Back to top |
|
 |
Marky Guest
|
Posted: Tue Jan 22, 2008 2:45 pm Post subject: |
|
|
| Is it possible to do a mouse gesture on a per application (ifwinactive) basis? |
|
| Back to top |
|
 |
ManaUser
Joined: 24 May 2007 Posts: 906
|
Posted: Tue Jan 22, 2008 5:27 pm Post subject: |
|
|
You could just do this:
| Code: | gldrdl:
IfWinActive ahk_class MozillaUIWindowClass
msgbox, You drew an S, in Firefox!
Else IfWinActive ahk_class Notepad
msgbox, You drew an S, in notepad!
Else
msgbox, You drew an S, somewhere else!
exit |
That's probably the best way actually. |
|
| Back to top |
|
 |
MarkyMark
Joined: 28 Sep 2007 Posts: 26
|
Posted: Tue Jan 22, 2008 10:31 pm Post subject: |
|
|
ManaUser
Thank you. I appreciate it.  |
|
| Back to top |
|
 |
lilljimpa
Joined: 18 Apr 2007 Posts: 95
|
Posted: Wed Jan 23, 2008 11:39 am Post subject: |
|
|
how do i add a weelup and weeldown to this gesture?
ex:
gweelup:
msgbox, you scroll up
exit _________________ you'll have to excuse me...I'm from Sweden, so my English is not that good...(but now it's better cuz JSLover/Guest is helping me)... |
|
| Back to top |
|
 |
ManaUser
Joined: 24 May 2007 Posts: 906
|
Posted: Wed Jan 23, 2008 6:28 pm Post subject: |
|
|
| lilljimpa wrote: | | how do i add a weelup and weeldown to this gesture? |
Heh, this is getting not-so-simple but here goes: (magenta parts are new)
| Code: | ;simple mouse gestures call subroutines based on a gesture (right button)
;the script tracks up (u) down (d) left (l) and right (r). There is a tolerance of +/- 45 degrees, so it is quite simple and accurate
;gestures can have curves. eg a circle drawn from the top and clockwise is registered as rdlu (right down left up)
;if a gesture is down (d) and right (r), like an "L", a subroutine called gdr ( for gesture down right) will be called
;if anything is labelled as gdr:, the action will be run
;as a further example, if an "S" is drawn (from top to bottom), gldrdl: (gesture left down right down left) will be called
;these 2 examples are included. See below for further examples
CoordMode, Mouse, Screen
winget, window0, id, ahk_class Progman ;not needed to make a gesture, window0 is used in subroutines called by gestures used on the desktop. see gu: subroutine example
rbutton::
if (window1<>window0) ;these 2 lines do not help the creation of gestures. The variable window2 can be used by subroutines called
window2:=window1 ;by the gestures eg. maximize previous window
mousegetpos,xpos1, ypos1,window1 ;window1 can be used by subroutines eg maximize window under cursor
gesture= ;clear previous tracking results, variable array gesture1 to gesture5
settimer, gesture, 10
GestureMode = 1
return
rbutton up::
settimer, gesture, off
GestureMode = 0
gesture := "g" . gesture ;eg. gdr = gesture down right
if islabel(gesture)<>0 ;checks if gesture is labelled
gosub, %gesture% ; eg. gosub, gdr
msgbox,,, %gesture% not yet defined,4
return
gesture:
mousegetpos,xpos2, ypos2
dx:=xpos2-xpos1
dy:=ypos1-ypos2
if (abs(dy)>=abs(dx))
{
if (dy>0)
track=u ;track is up
else
track=d ;down
}
else
{
if (dx>0)
track=r ;right
else
track=l ;left
}
if (abs(dy)<4 and abs(dx)<4)
track= ;not tracking at all if no significant change in x or y
xpos1:=xpos2
ypos1:=ypos2
if (track<>SubStr(gesture, 0, 1)) ;ignore track if not changing since previous track
gesture := gesture . track
return
WheelUp:: ;Since u and d are taken, we'll use + and - for the mouse wheel.
If GestureMode ;we're currently recording a gesture
{
If (SubStr(gesture, 0) != "+") ;Don't record two wheelups in a row
gesture := gesture . "+"
}
Else ;we're not recording a gesture so just do a wheelup
Send {WheelUp}
Return
WheelDown:: ;ditto for down
If GestureMode
{
If (SubStr(gesture, 0) != "-")
gesture := gesture . "-"
}
Else
Send {WheelDown}
Return
g: ;normal right click
click, right, xpos1, ypos1
exit
;;;;;;; Examples ;;;;;;;;;;;;;;;;;;;;;;;;;;
gu: ;; gesture up ;; maximize window under cursor. maximize last window if cursor over desktop
if (window1=window0)
{
if (window2<>"")
winmaximize, ahk_id %window2%
}
else
winmaximize, ahk_id %window1%
exit
gd: ;minimize window under cursor
if (window1<>window0)
winminimize, ahk_id %window1%
exit
gl: ;browser back
winactivate, ahk_id %window1%
sendinput, {Browser_Back}
exit
gr: ;browser forward
winactivate, ahk_id %window1%
sendinput, {Browser_Forward}
exit
gdr: ;restore last window.
winrestore, ahk_id %window2%
exit
gldr: ;close window under cursor (draw a C)
if (window1<>window0)
winclose, ahk_id %window1%
exit
gldrdl:
msgbox, you drew an S
exit
gurdurd:
msgbox, you drew an M
exit
g+:
MsgBox WheelUp
exit
g-:
MsgBox WheelDown
exit
gl+:
MsgBox Left-WheelUp
exit |
|
|
| Back to top |
|
 |
rodfell
Joined: 05 Oct 2007 Posts: 57 Location: Bundaberg (Bundy), Qld, Australia
|
Posted: Sat Jan 26, 2008 4:13 am Post subject: wheel up down |
|
|
| why do you need wheelup and wheel down? I tried for simplicity. If there's a good reason for it, i'll try to incorporate it. |
|
| Back to top |
|
 |
lilljimpa
Joined: 18 Apr 2007 Posts: 95
|
Posted: Sat Jan 26, 2008 3:15 pm Post subject: Re: wheel up down |
|
|
| rodfell wrote: | | why do you need wheelup and wheel down? I tried for simplicity. If there's a good reason for it, i'll try to incorporate it. |
i have my reason i have add a trancparent to the gesture, not as good i hope for but it's working nice _________________ you'll have to excuse me...I'm from Sweden, so my English is not that good...(but now it's better cuz JSLover/Guest is helping me)... |
|
| Back to top |
|
 |
rodfell
Joined: 05 Oct 2007 Posts: 57 Location: Bundaberg (Bundy), Qld, Australia
|
Posted: Sun Jan 27, 2008 3:44 am Post subject: |
|
|
| i get it now. i can see when the wheelup and down would be useful eg volume up and down. I played around with other ways of doing what you wanted but no success. the trouble is getkeystate doesn't work with wheelup and wheeldown. i have incorporated your code into my 24/7 ahk file |
|
| Back to top |
|
 |
Icarus
Joined: 24 Nov 2005 Posts: 507
|
Posted: Sun Jan 27, 2008 2:43 pm Post subject: |
|
|
A very nice and smart script.
I noticed there is one problem when gesturing diagonals - the script continuously records "dldldldldl".
I would also suggest to include diagonal as a valid gesture - this will allow to have things not possible now - for example, an "R" shaped gesture is not logged correctly now, since the leg of the R is sometimes recorded as R and sometimes as D (and sometimes as both).
I may try to do the change, if I do, I will post here. |
|
| Back to top |
|
 |
ManaUser
Joined: 24 May 2007 Posts: 906
|
Posted: Sun Jan 27, 2008 6:18 pm Post subject: |
|
|
I think some of you are missing the point. ^_^ This was supposed to be simple, simple.
There are other more complex/advanced gestures scripts already.
Last edited by ManaUser on Tue Jan 29, 2008 11:27 pm; edited 1 time in total |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|