Is there a way to simulate continuous pressing of a key based on mouse motion? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
tobindax
Posts: 6
Joined: 21 Oct 2016, 07:02

Is there a way to simulate continuous pressing of a key based on mouse motion?

30 May 2017, 01:48

For instance, if shift is pressed (or another key/modifier), by moving the mouse upwards/forwards the keyboard key "A" is continuously pressed, respectively the letters S, A, D for the other directions. The idea is to temporarily replace the ASDF keys of movement in certain games.

I realize that it might need some magic that checks whether the mouse is moved continuously or with stops.
neomulemi6
Posts: 216
Joined: 30 Jun 2016, 06:01

Re: Is there a way to simulate continuous pressing of a key based on mouse motion?

30 May 2017, 02:33

Code: Select all

SetTitleMatchMode, 2
#IfWinActive, window title ; replace this with your window title
#Persistent

SetTimer, Timer1, 10

LButton::Send a
RButton::Send b

Timer1:
Padding = 25 ; number of pixels the mouse must be moved to activate the script.
MouseGetPos, MouseX, MouseY
If (GetKeyState("Shift", "p"))
{
If (MouseX <> LastX && MouseY <> LastY)
{
	If (MouseX > LastX + Padding)
	{
		Send {d down}
	}
	Else If (MouseX < LastX - Padding)
	{
		send {a down}
	}
	If (MouseY > LastY + Padding)
	{
		Send {s down}
	}
	Else If (MouseY < LastY - Padding)
	{
		send {w down}
	}
	LastX := MouseX
	LastY := MouseY
}
}
Return

~Shift up::Send {w up}{a up}[s up}{d up}
neomulemi6
Posts: 216
Joined: 30 Jun 2016, 06:01

Re: Is there a way to simulate continuous pressing of a key based on mouse motion?  Topic is solved

30 May 2017, 02:58

If that doesn't work for you, try this. My original script worked fine in notepad, but when I tried it in an actual game, not so much.

Code: Select all

SetTimer, Timer1, 10

Timer1:
Padding = 25 ; number of pixels the mouse must be moved to activate the script.
If (GetKeyState("Shift", "p"))
{
	MouseGetPos, MouseX, MouseY
	If (MouseX <> LastX && MouseY <> LastY)
	{
		If (LastX && LastY)
		{
		If (MouseX > LastX + Padding)
		{
			Release()
			Send {d down}
		}
		Else If (MouseX < LastX - Padding)
		{
			Release()
			send {a down}
		}
		If (MouseY > LastY + Padding)
		{
			Release()
			Send {s down}
		}
		Else If (MouseY < LastY - Padding)
		{
			Release()
			send {w down}
		}
		}
		LastX := MouseX
		LastY := MouseY
	}
}
Return

Release()
{
	Send {w up}{a up}{s up}{d up}
}

Shift up::Release()

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Bing [Bot], boiler, Saarthak Kamra, TAC109, zKade and 32 guests