Mouse to WASD Topic is solved

Ask gaming related questions (AHK v1.1 and older)
Douglas Renato
Posts: 7
Joined: 29 Nov 2021, 01:02

Mouse to WASD

Post by Douglas Renato » 29 Nov 2021, 01:27

Hello people, I'm new at this, I don't know how to script... I need help.
I need a script that when I hold the mouse's side button or M2 it behaves like a WASD based on the coordinates, when I hold the button and if I tilt the mouse up, it sends W input, to the left the input is A, and if possible with the inputs combined as if placed between D and S the diagonal movement of the two pressed keys.

that way i can play the game i want just by mouse with just one hand... can someone help me please?

ludamo
Posts: 44
Joined: 25 Mar 2015, 02:21

Re: Mouse to WASD  Topic is solved

Post by ludamo » 29 Nov 2021, 02:21

You may have to polish this code a bit to get rid of some rough edges or to adapt it to your particular use.

Code: Select all

XButton1::
	; requires AHK_L version 1
	
	MouseGetPos, Xi, Yi		; inital values

	hRgn_0 := DllCall("CreateRectRgn", "int", Xi-4, "int", Yi-4, "int", Xi+4, "int", Yi+4)	; area around initial mouse position 
	
   VarSetCapacity( Points, 32, 0 )		; 4 sectors based on 200px x 200px rectangle
   NumPut( Xi-200, Points, 0, "int" ),	NumPut( Yi-200, Points, 4, "int" )
   NumPut( Xi-4, Points, 8, "int" ), NumPut( Yi-4, Points, 12, "int" )
   NumPut( Xi+4, Points, 16, "int" ), NumPut( Yi-4, Points, 20, "int" ) 
   NumPut( Xi+200, Points, 24, "int" ), NumPut( Yi-200, Points, 28, "int" ) 
	hRgn_N := DllCall("CreatePolygonRgn", "uint", &Points, "int", 4, "int", 1)		; North	ALTERNATE=1, WINDING=2

   NumPut( Xi+200, Points, 0, "int" ),	NumPut( Yi-200, Points, 4, "int" )
   NumPut( Xi+4, Points, 8, "int" ), NumPut( Yi-4, Points, 12, "int" )
   NumPut( Xi+4, Points, 16, "int" ), NumPut( Yi+4, Points, 20, "int" ) 
   NumPut( Xi+200, Points, 24, "int" ), NumPut( Yi+200, Points, 28, "int" ) 
	hRgn_E := DllCall("CreatePolygonRgn", "uint", &Points, "int", 4, "int", 1)		; East
	
   NumPut( Xi-200, Points, 0, "int" ),	NumPut( Yi+200, Points, 4, "int" )
   NumPut( Xi-4, Points, 8, "int" ), NumPut( Yi+4, Points, 12, "int" )
   NumPut( Xi+4, Points, 16, "int" ), NumPut( Yi+4, Points, 20, "int" ) 
   NumPut( Xi+200, Points, 24, "int" ), NumPut( Yi+200, Points, 28, "int" ) 
	hRgn_S := DllCall("CreatePolygonRgn", "uint", &Points, "int", 4, "int", 1)		; South
	
   NumPut( Xi-200, Points, 0, "int" ),	NumPut( Yi-200, Points, 4, "int" )
   NumPut( Xi-4, Points, 8, "int" ), NumPut( Yi-4, Points, 12, "int" )
   NumPut( Xi-4, Points, 16, "int" ), NumPut( Yi+4, Points, 20, "int" ) 
   NumPut( Xi-200, Points, 24, "int" ), NumPut( Yi+200, Points, 28, "int" ) 
	hRgn_W := DllCall("CreatePolygonRgn", "uint", &Points, "int", 4, "int", 1)		; West
	
	KeyWait, XButton1		; move mouse to new position and release button
	MouseGetPos, mX, mY		; new position
	If DllCall("PtInRegion", "uint", hRgn_N, "int", mX, "int", mY)
	{	
		Send W
		Tooltip, North
	}
	Else If DllCall("PtInRegion", "uint", hRgn_W, "int", mX, "int", mY)
	{
		Send A
		Tooltip, West
	}
	Else If DllCall("PtInRegion", "uint", hRgn_S, "int", mX, "int", mY)
	{
		Send S
		Tooltip, South
	}
	Else If DllCall("PtInRegion", "uint", hRgn_E, "int", mX, "int", mY)
	{
		Send D
		Tooltip, East
	}
	Else
		Tooltip, outside
		
	Return
Last edited by ludamo on 29 Nov 2021, 02:45, edited 1 time in total.

Douglas Renato
Posts: 7
Joined: 29 Nov 2021, 01:02

Re: Mouse to WASD

Post by Douglas Renato » 29 Nov 2021, 02:27

ludamo wrote:
29 Nov 2021, 02:21
You may have to polish this code a bit to get rid of some rough edges or to adapt it to your particular use.

Code: Select all

XButton1::
	; requires AHK_L version 1
	
	MouseGetPos, Xi, Yi		; inital values

	hRgn_0 := DllCall("CreateRectRgn", "int", Xi-4, "int", Yi-4, "int", Xi+4, "int", Yi+4)	; area around initial mouse position 
	
   VarSetCapacity( Points, 32, 0 )		; 4 sectors based on 200px x 200px rectangle
   NumPut( Xi-200, Points, 0, "int" ),	NumPut( Yi-200, Points, 4, "int" )
   NumPut( Xi-4, Points, 8, "int" ), NumPut( Yi-4, Points, 12, "int" )
   NumPut( Xi+4, Points, 16, "int" ), NumPut( Yi-4, Points, 20, "int" ) 
   NumPut( Xi+200, Points, 24, "int" ), NumPut( Yi-200, Points, 28, "int" ) 
	hRgn_N := DllCall("CreatePolygonRgn", "uint", &Points, "int", 4, "int", 1)		; North	ALTERNATE=1, WINDING=2

   NumPut( Xi+200, Points, 0, "int" ),	NumPut( Yi-200, Points, 4, "int" )
   NumPut( Xi+4, Points, 8, "int" ), NumPut( Yi-4, Points, 12, "int" )
   NumPut( Xi+4, Points, 16, "int" ), NumPut( Yi+4, Points, 20, "int" ) 
   NumPut( Xi+200, Points, 24, "int" ), NumPut( Yi+200, Points, 28, "int" ) 
	hRgn_E := DllCall("CreatePolygonRgn", "uint", &Points, "int", 4, "int", 1)		; East
	
   NumPut( Xi-200, Points, 0, "int" ),	NumPut( Yi+200, Points, 4, "int" )
   NumPut( Xi-4, Points, 8, "int" ), NumPut( Yi+4, Points, 12, "int" )
   NumPut( Xi+4, Points, 16, "int" ), NumPut( Yi+4, Points, 20, "int" ) 
   NumPut( Xi+200, Points, 24, "int" ), NumPut( Yi+200, Points, 28, "int" ) 
	hRgn_S := DllCall("CreatePolygonRgn", "uint", &Points, "int", 4, "int", 1)		; South
	
   NumPut( Xi-200, Points, 0, "int" ),	NumPut( Yi-200, Points, 4, "int" )
   NumPut( Xi-4, Points, 8, "int" ), NumPut( Yi-4, Points, 12, "int" )
   NumPut( Xi-4, Points, 16, "int" ), NumPut( Yi+4, Points, 20, "int" ) 
   NumPut( Xi-200, Points, 24, "int" ), NumPut( Yi+200, Points, 28, "int" ) 
	hRgn_W := DllCall("CreatePolygonRgn", "uint", &Points, "int", 4, "int", 1)		; West
	
	KeyWait, XButton1		; move mouse to new position and release button
	MouseGetPos, mX, mY		; new position
	If DllCall("PtInRegion", "uint", hRgn_N, "int", mX, "int", mY)
	{	
		Send W
		Tooltip, North
	}
	Else If DllCall("PtInRegion", "uint", hRgn_W, "int", mX, "int", mY)
	{
		Send A
		Tooltip, West
	Else If DllCall("PtInRegion", "uint", hRgn_S, "int", mX, "int", mY)
	{
		Send S
		Tooltip, South
	}
	Else If DllCall("PtInRegion", "uint", hRgn_E, "int", mX, "int", mY)
	{
		Send D
		Tooltip, East
	}
	Else
		Tooltip, outside
		
	Return
when i run the script says: Error: ELSE with no matching IF.
what i do?

ludamo
Posts: 44
Joined: 25 Mar 2015, 02:21

Re: Mouse to WASD

Post by ludamo » 29 Nov 2021, 02:44

Sorry there was a missing close bracket } after West

Code: Select all

	Tooltip, West
	}
	Else If DllCall("PtInRegion", "uint", hRgn_S, "int", mX, "int", mY)

Rohwedder
Posts: 7509
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Mouse to WASD

Post by Rohwedder » 29 Nov 2021, 05:20

Hallo,
or (also diagonal):

Code: Select all

Directions := ["sd","s","as","a","wa","w","wd","d"]
; down+right,right,up+right,up,... counterclockwise
XButton1::
MouseGetPos, X2, Y2
KeyWait, XButton1
MouseGetPos, X1, Y1
Send,% Directions[1+(Round(3+Atan2(X2-X1,Y2-Y1)/Atan(1))&7)]
Return
Atan2(x,y)
{
	return dllcall("msvcrt\atan2","Double",y,"Double",x,"CDECL Double")
}

Douglas Renato
Posts: 7
Joined: 29 Nov 2021, 01:02

Mouse to WASD 2

Post by Douglas Renato » 29 Nov 2021, 22:30

hello people, before I created the Mouse to WASD Post and the users Ludam and Rohwedder were generous and sent me scripts so that I can get the script to walk with WASD with the mouse side button with Mouse coordinates,
for some reason my replies aren't getting approved in that post so i'm being forced to create another one.
and the last one they sent me was this

Code: Select all

Directions := ["sd","s","as","a","wa","w","wd","d"]
; down+right,right,up+right,up,... counterclockwise
XButton1::
MouseGetPos, X2, Y2
KeyWait, XButton1
MouseGetPos, X1, Y1
Send,% Directions[1+(Round(3+Atan2(X2-X1,Y2-Y1)/Atan(1))&7)]
Return
Atan2(x,y)
{
	return dllcall("msvcrt\atan2","Double",y,"Double",x,"CDECL Double")
}
[Mod edit: [code][/code] tags added. Please use code tags. Thank you!]

this one is almost perfect, I just need the key to keep being pressed and not pressed just once for the script to work as it should, can someone help me?
Last edited by gregster on 29 Nov 2021, 22:52, edited 1 time in total.
Reason: Post was merged from another topic.

Douglas Renato
Posts: 7
Joined: 29 Nov 2021, 01:02

Re: Mouse to WASD

Post by Douglas Renato » 29 Nov 2021, 22:30

this one is almost perfect, I just need the key to keep being pressed and not pressed just once for the script to work as it should, can help me friend?

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Mouse to WASD

Post by gregster » 29 Nov 2021, 22:50

Sometimes it takes a little time, before a post gets approved. Please be patient, if that happens.
I merged your topics.

btw, please use code tags. Thank you! (If you have questions how to do this, please ask!)

Douglas Renato
Posts: 7
Joined: 29 Nov 2021, 01:02

Re: Mouse to WASD

Post by Douglas Renato » 29 Nov 2021, 23:03

gregster wrote:
29 Nov 2021, 22:50
Sometimes it takes a little time, before a post gets approved. Please be patient, if that happens.
I merged your topics.

btw, please use code tags. Thank you! (If you have questions how to do this, please ask!)
sorry man i completely noob sorry for the inconvenience,
i wait for a replay to finish the script here, thanks for helping me anyway.

Rohwedder
Posts: 7509
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Mouse to WASD

Post by Rohwedder » 30 Nov 2021, 04:20

Then try:

Code: Select all

Directions := ["sd","s","as","a","wa","w","wd","d"]
; down+right,right,up+right,up,... counterclockwise
XButton1::
MouseGetPos, X2, Y2
KeyWait, XButton1
MouseGetPos, X1, Y1
Direction := Directions[1+(Round(3+Atan2(X2-X1,Y2-Y1)/Atan(1))&7)]
Loop, Parse,% "wasd"
{
    Is := GetKeyState(A_LoopField) ;Is pressed
    Should := InStr(Direction, A_LoopField) ;Should be pressed
    IF (Is And !Should)
        SendInput, {%A_LoopField% Up}
    Else IF (!Is And Should)
        SendInput, {%A_LoopField% Down}
}
Return
Atan2(x,y)
{
    return dllcall("msvcrt\atan2","Double",y,"Double",x,"CDECL Double")
}

Douglas Renato
Posts: 7
Joined: 29 Nov 2021, 01:02

Re: Mouse to WASD

Post by Douglas Renato » 30 Nov 2021, 17:26

Rohwedder wrote:
30 Nov 2021, 04:20
Then try:

Code: Select all

Directions := ["sd","s","as","a","wa","w","wd","d"]
; down+right,right,up+right,up,... counterclockwise
XButton1::
MouseGetPos, X2, Y2
KeyWait, XButton1
MouseGetPos, X1, Y1
Direction := Directions[1+(Round(3+Atan2(X2-X1,Y2-Y1)/Atan(1))&7)]
Loop, Parse,% "wasd"
{
    Is := GetKeyState(A_LoopField) ;Is pressed
    Should := InStr(Direction, A_LoopField) ;Should be pressed
    IF (Is And !Should)
        SendInput, {%A_LoopField% Up}
    Else IF (!Is And Should)
        SendInput, {%A_LoopField% Down}
}
Return
Atan2(x,y)
{
    return dllcall("msvcrt\atan2","Double",y,"Double",x,"CDECL Double")
}
it's not working as it should :/
so... i found a similar post with this script

Code: Select all

 ; using-mouse-position-as-hotkey.ahk
; ver1.1 ... added 8-directions support.

; get current mouse area and send correspond key
; press F1 and move mouse

CoordMode, Mouse, Screen
CoordMode, ToolTip, Screen

; for debug
SetTimer, tm_get_area, 100

; center position define(memo: my machine is 1920/1080)
pixx := 1600
pixy := 900
basex := pixx/2
basey := pixy/2
return

tm_get_area:
    get_area()
return

get_area() {
    Global basex, basey

    MouseGetPos, cx, cy
    cur_degree := Atan((basey-cy)/(basex-cx)) * 180 / 3.141592653589793
    if (cx >= basex) {
        cur_degree360 := cur_degree + 90
    } else {
        cur_degree360 := cur_degree + 270
    }
   ;ToolTip, degree: %cur_degree360%, basex, basey+20, 1

    dx := (basex-cx)
    dy := (basey-cy)
    cur_area := get_area_id(cur_degree360)
    ToolTip, Area: %cur_area%, basex, basey, 2
    return cur_area
}

; area  area degree total degree
;-------------------------------
;  1      50          50
; 12      20          70
;  2      40          110
; 23      20          130
;  3      100         230
; 34      20          250
;  4      40          290
; 41      20          310
;  1      50          360
get_area_id(degree) {
    return degree < 50 ? 1
        : degree < 70 ?  12
        : degree < 110 ? 2
        : degree < 130 ? 23
        : degree < 230 ? 3
        : degree < 250 ? 34
        : degree < 290 ? 4
        : degree < 310 ? 41
        : 1 ; 310 to 360 degree
}

F1::
    area := get_area()
    if (area=1) {
        key1=w
        key2=
    }
    if (area=2) {
        key1=d
        key2=
    }
    if (area=3) {
        key1=s
        key2=
    }
    if (area=4) {
        key1=a
        key2=
    }
    if (area=12) {
        key1=w
        ; Second key is d
        key2=d
    }
    if (area=23) {
        key1=d
        ; Second key is s
        key2=s
    }
    if (area=34) {
        key1=s
        ; Second key is a
        key2=a
    }
    if (area=41) {
        key1=a
        ; Second key is w
        key2=w
    }

    ; other changes??? ... need to proceed 2nd key.

    ; Release keys that were previous pressed
    if (prevkey1 != key1) {
        if (prevkey1) {
            Send, {%prevkey1% Up}
        }
        prevkey1 := key1
    }
    if (prevkey2 != key2) {
        if (prevkey2) {
            Send, {%prevkey2% Up}
        }
        prevkey2 := key2
    }

    Send, {%key1% Down}
    if (key2) {
        Send, {%key2% Down}
    }
return  ; replece to Keywait, F1 if you need

F1 Up::
    Send, {%prevkey1% Up}
    if (prevkey2) {
        Send, {%prevkey2% Up}
    }
return
[Mod edit: [code][/code] tags added.]

this one is almost perfect i just need it to work with the side mouse button and the axis reset when i hold the button again and not based on the center of the screen like this.
is it possible to do that?
Last edited by gregster on 30 Nov 2021, 17:27, edited 1 time in total.
Reason: Please use code tags. Thank you!

Rohwedder
Posts: 7509
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Mouse to WASD

Post by Rohwedder » 01 Dec 2021, 02:19

… and not based on the center of the screen like this.
is it possible to do that?

As long as the input of the function is the vector distance of two points, you need a second point for reference.
Have fun programming!

VarnKail
Posts: 2
Joined: 04 Jun 2023, 08:38

Re: Mouse to WASD

Post by VarnKail » 04 Jun 2023, 08:46

I came across this post because I was too lazy to play game with both of my hand XD so here we go, this should work perfect, just hold ur right mouse and drag it to the dir that you want.
p/s: the code and this post were write with one hand and touch keyboard :) hope this help someone as lazy as me

Code: Select all

Directions := ["sd","s","as","a","wa","w","wd","d"]
; down+right,right,up+right,up,... counterclockwise
RButton::
MouseGetPos, X2, Y2
Loop
{
	Sleep, 100
	if !GetKeyState("RButton", "P")
		break
	MouseGetPos, X1, Y1
	Send,% Directions[1+(Round(3+Atan2(X2-X1,Y2-Y1)/Atan(1))&7)]
}
Return
Atan2(x,y)
{
	return dllcall("msvcrt\atan2","Double",y,"Double",x,"CDECL Double")
}

VarnKail
Posts: 2
Joined: 04 Jun 2023, 08:38

Re: Mouse to WASD

Post by VarnKail » 04 Jun 2023, 09:50

VarnKail wrote:
04 Jun 2023, 08:46
I came across this post because I was too lazy to play game with both of my hand XD so here we go, this should work perfect, just hold ur right mouse and drag it to the dir that you want.
p/s: the code and this post were write with one hand and touch keyboard :) hope this help someone as lazy as me

Code: Select all

Directions := ["sd","s","as","a","wa","w","wd","d"]
; down+right,right,up+right,up,... counterclockwise
keypress := ""
RButton::
MouseGetPos, X2, Y2
Loop
{
	Sleep, 100
	if !GetKeyState("RButton", "P")
	{
		Send, {%keypress% up}
		keypress := ""
		break
	}
	MouseGetPos, X1, Y1
	k := Directions[1+(Round(3+Atan2(X2-X1,Y2-Y1)/Atan(1))&7)]
	if(keypress != k)
	{
		Send, {%keypress% up}
		keypress := k
		Send, {%keypress% down}
	}
}
Return
Atan2(x,y)
{
	return dllcall("msvcrt\atan2","Double",y,"Double",x,"CDECL Double")
}
uhhh I forgot to hold down the key xD fixed code

Post Reply

Return to “Gaming Help (v1)”