how can I assign more keys to this script? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
rainrain
Posts: 45
Joined: 20 Jan 2022, 00:17

how can I assign more keys to this script?

Post by rainrain » 05 Oct 2022, 02:36

I'm getting errors when adding the 3rd key.. not sure the right way to add a 3rd else.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future releases.
SendMode Input  ; Recommended due to its superior speed and reliability.

GroupAdd, gw2, Guild Wars 2
GroupAdd, gw2, `.0 0 0


#IfWinActive ahk_group gw2


RButton::evaluateButton("RButton", "F1")
Wheelup::evaluateButton("Wheelup", "8")
Wheeldown::evaluateButton("Wheelup", "7")

evaluateButton(Button1, Button2, Button3)
{
   if (isMouseShown()) {
      send {%Button1% Down}
      KeyWait, RButton
      send {%Button1% Up}
   } else {
      send {%Button2% Down}
      KeyWait, Wheelup
      send {%Button2% Up}
   } else {
      send {%Button3% Down}
      KeyWait, Wheeldown
      send {%Button3% Up}
   }
}

isMouseShown()
{
   StructSize := A_PtrSize + 16
   VarSetCapacity(InfoStruct, StructSize)
   NumPut(StructSize, InfoStruct)
   DllCall("GetCursorInfo", UInt, &InfoStruct)
   Result := NumGet(InfoStruct, 8)

   return (Result != 0)
}

Pause::ExitApp
Last edited by rainrain on 05 Oct 2022, 05:43, edited 1 time in total.

rainrain
Posts: 45
Joined: 20 Jan 2022, 00:17

Re: how can I assign more keys to this script?

Post by rainrain » 05 Oct 2022, 03:43

wrong code
Last edited by rainrain on 05 Oct 2022, 05:43, edited 2 times in total.

rainrain
Posts: 45
Joined: 20 Jan 2022, 00:17

Re: how can I assign more keys to this script?

Post by rainrain » 05 Oct 2022, 03:58

wrong code
Last edited by rainrain on 05 Oct 2022, 05:42, edited 1 time in total.

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

Re: how can I assign more keys to this script?

Post by Rohwedder » 05 Oct 2022, 05:04

Hallo,
I refer to your 2nd request. Try:

Code: Select all

#NoEnv
SendMode Input
RButton::evaluateButton("RButton", "F1")
LButton::evaluateButton("LButton", "F2")
evaluateButton(Button1, Button2)
{
	if (isMouseShown())
	{
		send {%Button1% Down}
		KeyWait, %Button1%
		send {%Button1% Up}
	}
	else
	{
		send {%Button2% Down}
		KeyWait, %Button1%
		send {%Button2% Up}
	}
}
isMouseShown()
{
	StructSize := A_PtrSize + 16
	VarSetCapacity(InfoStruct, StructSize)
	NumPut(StructSize, InfoStruct)
	DllCall("GetCursorInfo", UInt, &InfoStruct)
	Result := NumGet(InfoStruct, 8)
	return (Result != 0)
}
Pause::ExitApp
With Wheelup or Wheeldown this can't run because you can't release them!

Tensai
Posts: 29
Joined: 12 Dec 2019, 14:15

Re: how can I assign more keys to this script?

Post by Tensai » 05 Oct 2022, 05:06

Hi, welcome to the forums!
Try using else if for the 2nd else. Alternatively consider using switch. Hope this helps :)

rainrain
Posts: 45
Joined: 20 Jan 2022, 00:17

Re: how can I assign more keys to this script?

Post by rainrain » 05 Oct 2022, 05:22

It didn't work
Error Requires at least 1 parameter
021:else

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future releases.
SendMode Input  ; Recommended due to its superior speed and reliability.

GroupAdd, gw2, Guild Wars 2
GroupAdd, gw2, `.0 0 0


#IfWinActive ahk_group gw2


RButton::evaluateButton("RButton", "F1")
Wheelup::evaluateButton("Wheelup", "8")
Wheeldown::evaluateButton("Wheelup", "7")

evaluateButton(Button1, Button2, Button3)
{
   if (isMouseShown()) {
      send {%Button1% Down}
      KeyWait, RButton
      send {%Button1% Up}
   } else if {
      send {%Button2% Down}
      KeyWait, Wheelup
      send {%Button2% Up}
   } else {
      send {%Button3% Down}
      KeyWait, Wheeldown
      send {%Button3% Up}
   }
}

isMouseShown()
{
   StructSize := A_PtrSize + 16
   VarSetCapacity(InfoStruct, StructSize)
   NumPut(StructSize, InfoStruct)
   DllCall("GetCursorInfo", UInt, &InfoStruct)
   Result := NumGet(InfoStruct, 8)

   return (Result != 0)
}

Pause::ExitApp

Tensai
Posts: 29
Joined: 12 Dec 2019, 14:15

Re: how can I assign more keys to this script?  Topic is solved

Post by Tensai » 05 Oct 2022, 06:42

The way your function is currently defined requires 3 parameters but you are only passing 2. This should get it working:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future releases.
SendMode Input  ; Recommended due to its superior speed and reliability.

GroupAdd, gw2, Guild Wars 2
GroupAdd, gw2, `.0 0 0


#IfWinActive ahk_group gw2


RButton::evaluateButton("RButton", "F1")
Wheelup::evaluateButton("Wheelup", "8")
Wheeldown::evaluateButton("Wheelup", "7")	; do you mean Wheeldown here?


evaluateButton(Button1, Button2)
{
   if (isMouseShown()) {
      send {%Button1% Down}
      KeyWait, RButton
      send {%Button1% Up}
   }
   else {
      send {%Button2% Down}
      KeyWait, % "{" A_ThisHotkey "}"
      send {%Button2% Up}
   }
}


isMouseShown()
{
   StructSize := A_PtrSize + 16
   VarSetCapacity(InfoStruct, StructSize)
   NumPut(StructSize, InfoStruct)
   DllCall("GetCursorInfo", UInt, &InfoStruct)
   Result := NumGet(InfoStruct, 8)

   return (Result != 0)
}

Pause::ExitApp


If you're just trying to send Button2 depending on isMouseShown() then this should be a cleaner and easier to maintain:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future releases.
SendMode Input  ; Recommended due to its superior speed and reliability.

GroupAdd, gw2, Guild Wars 2
GroupAdd, gw2, `.0 0 0

#IfWinActive ahk_group gw2 and isMouseShown()
RButton::send {F1}
#IfWinActive ahk_group gw2
Wheelup::8
Wheeldown::7

isMouseShown()
{
   StructSize := A_PtrSize + 16
   VarSetCapacity(InfoStruct, StructSize)
   NumPut(StructSize, InfoStruct)
   DllCall("GetCursorInfo", UInt, &InfoStruct)
   Result := NumGet(InfoStruct, 8)

   return (Result != 0)

}
#If
Pause::ExitApp

rainrain
Posts: 45
Joined: 20 Jan 2022, 00:17

Re: how can I assign more keys to this script?

Post by rainrain » 05 Oct 2022, 12:10

Tensai wrote:
The reaction time for the above script is kind of slow, so I'm returning to my old script.. If you have got the time could you please review it, and let me know if I did it correctly, I added mouse_wheel_up:="8" and mouse_wheel_down:="7", and added the same lines from the above. Although I'm not sure if those lines are necessary for wheel keys. Was hoping you could help me to optimize the script and remove unneeded lines, Thank you!

Code: Select all


Critical 500
#NoEnv
#MaxThreads 20
#SingleInstance force
#MaxHotkeysPerInterval 200



VarSetCapacity(active_cursor,A_PtrSize+16)
GMWI := "ahk_class ArenaNet_Gr_Window_Class"


mouse_button_left  := "1"
mouse_button_right := "f1"
mouse_wheel_up:="8"
mouse_wheel_down:="7"

#If WinActive(GMWI)

LButton::
  if GetKeyState("RButton","P") and !Action_Camera_RMB
    allow_key("LButton","down")
  else if Check_Cursor(active_cursor) = 0 {
    allow_key(mouse_button_left,"down")
    Action_Camera_LMB := true
  }
  else
    allow_key("LButton","down")
return

LButton up::
  if Action_Camera_LMB {
    allow_key(mouse_button_left,"up")
    Action_Camera_LMB := false
  }
  else
    allow_key("LButton","up")
return

RButton::
  if GetKeyState("LButton","P") and !Action_Camera_LMB
    allow_key("RButton","down")
  else if Check_Cursor(active_cursor) = 0 {
    allow_key(mouse_button_right,"down")
    Action_Camera_RMB := true
  }
  else
    allow_key("RButton","down")
return

RButton up::
  if Action_Camera_RMB {
    allow_key(mouse_button_right,"up")
    Action_Camera_RMB := false
  }
  else
    allow_key("RButton","up")
return

WheelUp::
if GetKeyState("LButton","P") and !Action_Camera_LMB
allow_key("WheelUp","down")
else if Check_Cursor(active_cursor) = 0 {
Send, {%mouse_wheel_up%}
Action_Camera_RMB := true
}
else
Send, {WheelUp}
return

WheelDown::
if GetKeyState("LButton","P") and !Action_Camera_LMB
allow_key("WheelDown","down")
else if Check_Cursor(active_cursor) = 0 {
Send, {%mouse_wheel_down%}
Action_Camera_RMB := true
}
else
Send, {WheelDown}
return



allow_key(key,state) {
  global
  Send,{Blind}{%key% %state%}
  if !WinActive(GMWI) {
    Critical,Off
    KeyWait,%key%
    Send,{Blind}{%key% up}
  }
  return
}

Check_Cursor(ByRef cursor) {
  NumPut(A_PtrSize+16,cursor)
  DllCall("GetCursorInfo","uint",&cursor)
  return NumGet(cursor,4)
}



Tensai
Posts: 29
Joined: 12 Dec 2019, 14:15

Re: how can I assign more keys to this script?

Post by Tensai » 05 Oct 2022, 14:27

rainrain wrote:
@rainrain Happy to help! ;)
Perhaps KeyWait is causing a sluggish response? You could give it a timeout.
Overall it looks fine. What's most important is that it is readable and maintainable for you.
Try adding SetBatchLines, -1 to the top of your script, which will "have the script run at maximum speed". It is the single most important line for performance.
Also have a look at How to optimize the speed of a script as much as possible.
Best of luck! :thumbup:

Post Reply

Return to “Ask for Help (v1)”