[Library] LLMouse for AHK v2

Post gaming related scripts
Japadapapapa
Posts: 2
Joined: 12 Dec 2023, 17:35

[Library] LLMouse for AHK v2

13 Dec 2023, 11:26

This is a modification of LLMouse by evilC for use with AutoHotkey v2.

Code: Select all

; =========== Sample script ============================================================
#Requires AutoHotkey v2.0
#SingleInstance Force
SetWorkingDir A_InitialWorkingDir


; Send 2x mouse wheel down rolls at rate of 100ms 
F11::
{
	LLMouse.Wheel(-1, 2, 100)
}

; Send 100x 10 unit mouse moves for the x axis at a rate of 2ms
F12::
{
    LLMouse.Move(0, 10, 100, 2)
}

; =======================================================================================
; LLMouse - A library to send Low Level Mouse input

; Note that many functions have time and rate parameters.
; These all work the same way:
; times	- How many times to send the requested action. Optional, default is 1
; rate	- The rate (in ms) to send the action at. Optional, default rate varies
; Note that if you use a value for rate of less than 10, special code will kick in.
; QPX is used for rates of <10ms as the AHK Sleep command does not support sleeps this short
; More CPU will be used in this mode.
class LLMouse {
	static MOUSEEVENTF_MOVE := 0x1
	static MOUSEEVENTF_WHEEL := 0x800
	
	; ======================= Functions for the user to call ============================
	; Move the mouse
	; All values are Signed Integers (Whole numbers, Positive or Negative)
	; x		- How much to move in the x axis. + is right, - is left
	; y		- How much to move in the y axis. + is down, - is up
	static Move(x, y, times := 1, rate := 1){
		this._MouseEvent(times, rate, this.MOUSEEVENTF_MOVE, x, y)
	}
	
	; Move the wheel
	; dir	- Which direction to move the wheel. 1 is up, -1 is down
	static Wheel(dir, times := 1, rate := 10){
		static WHEEL_DELTA := 120
		this._MouseEvent(times, rate, this.MOUSEEVENTF_WHEEL, , , dir * WHEEL_DELTA)
	}
	
	; ============ Internal functions not intended to be called by end-users ============
	static _MouseEvent(times, rate, dwFlags := 0, dx := 0, dy := 0, dwData := 0){
		Loop times {
			DllCall("mouse_event", "uint", dwFlags, "int", dx ,"int", dy, "uint", dwData, "int", 0)
			if (A_Index != times){	; Do not delay after last send, or if rate is 0
				if (rate >= 10){
					Sleep rate
				} else {
					this._Delay(rate * 0.001)
				}
			}
		}
	}
	
	static _Delay( D:=0.001 ) { ; High Resolution Delay ( High CPU Usage ) by SKAN | CD: 13/Jun/2009
		Static F ; www.autohotkey.com/forum/viewtopic.php?t=52083 | LM: 13/Jun/2009
		Critical
		IsSet(F) ? F : DllCall( "QueryPerformanceFrequency", "Int64P", &F :=0 )
		DllCall( "QueryPerformanceCounter", "Int64P", &pTick := 0 ), cTick := pTick
		While( ( (Tick:=(pTick-cTick)/F)) <D ) {
			DllCall( "QueryPerformanceCounter", "Int64P", &pTick )
			Sleep -1
		}
		Return Round( Tick,3 )
	}
}
vmech
Posts: 361
Joined: 25 Aug 2019, 13:03

Re: [Library] LLMouse for AHK v2

14 Dec 2023, 20:09

@Japadapapapa
Wow, what kind of antiquity did you unearth? :lol:

Code: Select all

; =========== Sample script ============================================================
#Requires AutoHotkey v2.0
#SingleInstance Force
SetWorkingDir A_InitialWorkingDir

; Send mouse wheel random rolls at rate of 100ms 
F11::
{
  static dir := 1
  dir -= ((dir > 0) - (dir < 0)) * 2
  LLMouse.Wheel(dir, random(1,5), 100)
}

; Send random unit mouse moves at a rate of 2ms
F12::
{
  static dir := -1
  dir += ((dir < 0) - (dir > 0)) * 2
  LLMouse.Move(random(1,5)*dir, random(-5,-1)*dir, 50, 2)
}

; =======================================================================================
; LLMouse - A library to send Low Level Mouse input

; Note that many functions have time and rate parameters.
; These all work the same way:
; times	- How many times to send the requested action. Optional, default is 1
; rate	- The rate (in ms) to send the action at. Optional, default rate varies
; Note that if you use a value for rate of less than 10, special code will kick in.
; QPX is used for rates of <10ms as the AHK Sleep command does not support sleeps this short
; More CPU will be used in this mode.
class LLMouse
{
  static  MOUSEEVENTF_MOVE := 0x1, MOUSEEVENTF_WHEEL := 0x800, WHEEL_DELTA := 120,
          LIMIT := SubStr(A_OSVersion,1,InStr(A_OSVersion,'.')) < 8 ? 10 : 15

  ; ======================= Functions for the user to call ============================
  ; Move the mouse
  ; All values are Signed Integers (Whole numbers, Positive or Negative)
  ; x		- How much to move in the x axis. + is right, - is left
  ; y		- How much to move in the y axis. + is down, - is up
  static Move(x, y, times := 1, rate := 1) => this._MouseEvent(times, rate, this.MOUSEEVENTF_MOVE, x, y)

  ; Move the wheel
  ; dir	- Which direction to move the wheel. 1 is up, -1 is down
  static Wheel(dir, times := 1, rate := 10) => this._MouseEvent(times, rate, this.MOUSEEVENTF_WHEEL, , , dir * this.WHEEL_DELTA)

  ; ============ Internal functions not intended to be called by end-users ============
  static _MouseEvent(times, rate, dwFlags := 0, dx := 0, dy := 0, dwData := 0)
  {
    Loop times
    {
      DllCall("mouse_event", "uint", dwFlags, "int", dx ,"int", dy, "uint", dwData, "int", 0)
      if (A_Index - times) ; Do not delay after last send, or if rate is 0
      {
        if (rate < this.LIMIT)
          this._Delay(rate)
        else
          Sleep(rate)
      }
    }
  }

  static _Delay( D := 1 ) ; High Resolution Delay ( High CPU Usage ) by SKAN | CD: 13/Jun/2009
  {
    ; www.autohotkey.com/forum/viewtopic.php?t=52083 | LM: 13/Jun/2009
    Static F := ( DllCall( "QueryPerformanceFrequency", "Int64P", &F:=0 ), F//1000 )
    Critical
    DllCall( "QueryPerformanceCounter", "Int64P", &cTick:=0 ), pTick := cTick
    ; While (Tick := (pTick - cTick) // F) < D
    While ((pTick - cTick) // F) < D
      DllCall( "QueryPerformanceCounter", "Int64P", &pTick:=0 ), Sleep(-1)
    ; Return Round( Tick,3 )
  }
}
Alternative _MouseEvent function

Code: Select all

static _MouseEvent(times, rate, dwFlags := 0, dx := 0, dy := 0, dwData := 0)
{
  DelayFN := (rate < this.LIMIT) ? this._Delay.Bind(rate) : Sleep.Bind(rate)
  while times
  {
    DllCall("mouse_event", "uint", dwFlags, "int", dx ,"int", dy, "uint", dwData, "int", 0)
    if (times -= 1) ; Do not delay after last send, or if rate is 0
      DelayFN
  }
}
Please post your script code inside [code] ... [/code] block. Thank you.
Japadapapapa
Posts: 2
Joined: 12 Dec 2023, 17:35

Re: [Library] LLMouse for AHK v2

20 Mar 2024, 08:32

Needed something that could move the mouse at a very low level for a project I'm working on. Found this for AHK1 and decided that while I'm at it I might as well try to make it work with AHK2.

Return to “Gaming”

Who is online

Users browsing this forum: No registered users and 2 guests