Triggering Autokey Script via Mouse Button - How To

Triggering AutoKey Script via Mouse Button - How To?

Take a look at xbindkeys. I use it to make single modifier keys to hotkeys (such as RCtrl), but I read it is also capable of using mouse buttons as trigger.
https://www.linux.com/news/start-programs-pro-xbindkeys

Then you can assign a command like "bash -c 'xdotool key Ctrl+Shift+Super+Alt+1'" to the mouse button, and make a Hotkey-Script in Autokey waiting for this combination.

Trigger AHK script based on the position of the mouse

Yes. Easiest way I can think of to do what you are asking is to use SetTimer and MouseGetPos to evaluate the position and if it matches than trigger your script.

The 2nd example code on the MouseGetPos is using SetTimer. This should get you headed in the right direction.

Autohotkey - Trigger script when a certain button was clicked in a window

You are right. You can use this instead of ImgSearch.

ControlGetPos, x, y, w, h, button1, ahk_class CalcFrame
MsgBox, %x% %y% %w% %h%
return

So you would have to run the ControlGetPos after each mouse click (only when the target window title is active) and then compare the actual mouse coordinates with the button click area.

Here is some code for the calculator:

#SingleInstance Force
#Persistent

#IfWinActive, ahk_class CalcFrame
~LButton::
MouseGetPos, MouseX, MouseY
ControlGetPos, ButtonX, ButtonY, ButtonW, ButtonH, button1, ahk_class CalcFrame
ButtonX2:=ButtonX + ButtonW
ButtonY2:=ButtonY + ButtonH
if MouseX between %ButtonX% and %ButtonX2%
{
if MouseY between %ButtonY% and %ButtonY2%
{
MsgBox, You pressed the MC button
}
}
Return
#IfWinActive

AHK fails to execute when the trigger is used in the window that is in scope. Is there a way to make AHK greedier?

You can define custom combinations for those two keys in your script
e.g.

XButton1 & LButton::return ; do nothing
; or another action:
; XButton1 & LButton:: Run notepad
XButton1::Send ^#{Left}

XButton2 & a::return
XButton2::Send ^#{Right}

This way the keys lose their native function in the programs.

For details, see Custom Combinations.

EDIT:

If a program is running with admin privileges, then AHK won't intercept the key presses, and that could very well be the reason behind this problem.

If that is the case, try to run the AHK script as administrator by adding this to the auto-execute section (top of the script):

; If the script is not elevated, relaunch as administrator and kill current instance:

full_command_line := DllCall("GetCommandLine", "str")

if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
try ; leads to having the script re-launching itself as administrator
{
if A_IsCompiled
Run *RunAs "%A_ScriptFullPath%" /restart
else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
}
ExitApp
}

https://autohotkey.com/docs/commands/Run.htm#RunAs.

AutoHotKey on right mouse click get a keyboard button pressed as well

OK it looks like you want to toggle the behaviour.

#SingleInstance Force
#installKeybdHook
#Persistent
SetTitleMatchMode, 2 ; Make search title in #IfWinActive more flexible

#IfWinActive, Title of your game as found by AHK Windows Spy
RButton::
If (Toggler := !Toggler )
{
ToolTip, RButton Down
Send, {RButton Down} q
Return
}
ToolTip, RButton Up
Send {RButton Up} q
Return
#IfWinActive
Return

*x::
ExitApp

Update

After reading your text (and ignoring your script where you want to toggle the behaviour), I think that you want this! RightClick will send RightClick Down + q, then nothing, until you let go of RightClick, which will then send a RightClick Up + q. I added the ToolTips, so you can see if the script is activated correctly by #IfWinActive.

#SingleInstance Force
#installKeybdHook
#Persistent
SetTitleMatchMode, 2 ; Make search title in #IfWinActive more flexible

#IfWinActive, Title of your game as found by AHK Windows Spy

RButton::
ToolTip, RButton Down + q
Send, {RButton Down} q
Return

RButton Up::
ToolTip, RButton Up + q
Send, {RButton Up} q
Return

#IfWinActive
Return

*x::
ExitApp

If this is indeed what you want, then the final solution is coming pretty close to the original tip from Armin!

Update 2, Alternative way

#SingleInstance Force
#installKeybdHook
#Persistent
SetTitleMatchMode, 2 ; Make search title in #IfWinActive more flexible

#IfWinActive, Title of your game as found by AHK Windows Spy
*~RButton:: ; ~ passes Rbutton through
ToolTip, RButton Down + q
Send, q
KeyWait, RButton ; Wait for RButton to be released
ToolTip, RButton Up + q
Send, q
Return
#IfWinActive
Return

*x::
ExitApp

Autohotkey: Send mouse-click (or key-press) without triggering handler

From the AutoHotkey manual:

$ - This is usually only necessary if the script uses the Send command to send the keys that comprise the hotkey itself, which might otherwise cause it to trigger itself.

That does the trick:

$LButton:: Trap


Related Topics



Leave a reply



Submit