Diagonal movement

Get help with using AutoHotkey and its commands and hotkeys
haeri
Posts: 1
Joined: 03 Feb 2020, 11:01

Diagonal movement

03 Feb 2020, 11:45

So I've been trying to piece together a code that would emulate top-down game movement for mouse position.
The idea is that the mouse cursor is bound to character movement hotkeys and always points towards the direction the character is facing, and if no directional keys are pressed, the cursor centers on character.
The following script gives the rough idea

Code: Select all

SetDefaultMouseSpeed, 0

;UP
~o::
if (GetKeyState("o", "P"))
   MouseMove, 640, 200

;DOWN
~l::
if (GetKeyState("l", "P"))
   MouseMove, 640, 500

;RIGHT
~`;::
if (GetKeyState("`;", "P"))
   MouseMove, 930, 360

;LEFT
~k::
if (GetKeyState("k", "P"))
   MouseMove, 350, 360

;COMBOS

;UP + RIGHT

~o & ~`;::
if GetKeyState("o", "P") and GetKeyState("`;", "P")
   MouseMove, 900, 200
   KeyWait, o
   if (GetKeyState("`;", "P"))
      MouseMove, 930, 360
   KeyWait, `;
   if (GetKeyState("o", "P"))
      MouseMove, 640, 200
    
~`; & ~o::
if GetKeyState("`;", "P") and GetKeyState("o", "P")
   MouseMove, 900, 200
   KeyWait, `;
   if (GetKeyState("o", "P"))
      MouseMove, 640, 200
   KeyWait, o
   if (GetKeyState("`;", "P"))
      MouseMove, 930, 360

;UP + LEFT

~o & ~k::
if GetKeyState("o", "P") and GetKeyState("k", "P")
   MouseMove, 380, 200
   KeyWait, o
   if (GetKeyState("k", "P"))
      MouseMove, 350, 360
   KeyWait, k
   if (GetKeyState("o", "P"))
      MouseMove, 640, 200
   

~k & ~o::
if GetKeyState("k", "P") and GetKeyState("o", "P")
   MouseMove, 380, 200
   KeyWait, k
   if (GetKeyState("o", "P"))
      MouseMove, 640, 200
   KeyWait, o
   if (GetKeyState("k", "P"))
      MouseMove, 350, 360

;DOWN + LEFT

~l & ~k::
if GetKeyState("l", "P") and GetKeyState("k", "P")
   MouseMove, 380, 500
   KeyWait, l
   if (GetKeyState("k", "P"))
      MouseMove, 350, 360
   KeyWait, k
   if (GetKeyState("l", "P"))
      MouseMove, 640, 500

~k & ~l::
if GetKeyState("k", "P") and GetKeyState("l", "P")
   MouseMove, 380, 500
   KeyWait, k
   if (GetKeyState("l", "P"))
      MouseMove, 640, 500
   KeyWait, l
   if (GetKeyState("k", "P"))
      MouseMove, 350, 360

;DOWN + RIGHT

~l & ~`;::
if GetKeyState("l", "P") and GetKeyState("`;", "P")
   MouseMove, 900, 500
   KeyWait, l
   if (GetKeyState("`;", "P"))
      MouseMove, 930, 360
   KeyWait, `;
   if (GetKeyState("l", "P"))
      MouseMove, 640, 500
   

~`; & ~l::
if GetKeyState("`;", "P") and GetKeyState("l", "P")
   MouseMove, 900, 500
   KeyWait, `;
   if (GetKeyState("l", "P"))
      MouseMove, 640, 500
   KeyWait, l
   if (GetKeyState("`;", "P"))
      MouseMove, 930, 360

;NO BUTTONS PRESSED

KeyWait, o
KeyWait, l
KeyWait, k
KeyWait, `;
MouseMove, 640, 360
Return
It's more or less working fine for horizontal and vertical movement (up, down, left, right) but can not handle the diagonal movement (up+right, up+left, down+right, down+left). Specifically, when pressing two buttons for diagonal movement and then releasing one, the cursor stays in place. I tried fixing it with

Code: Select all

   KeyWait, o
   if (GetKeyState("`;", "P"))
      MouseMove, 930, 360
   KeyWait, `;
   if (GetKeyState("o", "P"))
      MouseMove, 640, 200
but it works only once and in 1 direction only. I want it to jump smoothly depending on what keys are being pressed right now (basically an intuitive WASD movement behavior).
I heard it might be tricky and requires timers and stuff.
Any help appreciated.
Thanks!

Return to “Ask For Help”

Who is online

Users browsing this forum: aldrinjohnom, Bing [Bot], bushez, mgroen, Rohwedder, RubbeH and 51 guests