Stop script from executing without altering stopping button Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Galvatron
Posts: 16
Joined: 11 Jun 2017, 09:52

Stop script from executing without altering stopping button

11 Jun 2017, 10:02

Hello

I do have a mini script that is part of bigger script. The part I am interested in:
NumpadDot::SendInput {s down}
NumpadDel::SendInput {s down}
It just holds button down. Currently to stop it I must press S button. What I want to do, is to be able to stop it being held down while pressing certain keyboard buttons like W or Numpad 0 without altering normal functions of those buttons. But I don't want it to be released when I press buttons like A or D. I would normally use "pause script" method but as it's part of much bigger script that does many other things I am unable to. Best thing that I could do would be to separate scripts and just pause this button hold one, but ain't there anything better to do?
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Stop script from executing without altering stopping button  Topic is solved

11 Jun 2017, 10:15

To keep normal functions of a key when making it a hotkey, use the ~ modifier. So you should make any of the keys you want to cause the s to stop being held down into a hotkey like this: ~w::SendInput {s up}
Galvatron
Posts: 16
Joined: 11 Jun 2017, 09:52

Re: Stop script from executing without altering stopping button

12 Jun 2017, 05:31

My sub-script looks like this:
NumpadDot::SendInput {s down}
NumpadDel::SendInput {s down}
~w::SendInput {s up}
~Numpad0::SendInput {s up}
~NumpadIns::SendInput {s up}
But there is a problem, somehow it interferes with Caps Lock. It's like when Caps Lock is enabled, pressing "w" button disables it(or taps again). Very annoying, is there anything to do?
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Stop script from executing without altering stopping button

12 Jun 2017, 11:08

So when AHK sees "Send s", it will try to send a lowercase s. It does this by releasing any modifier keys, and also changing the CapsLock state to off to produce the "correct" output. Normally it puts the CapsLock back on right afterward. You can avoid this effect by using the {Blind} "key". ~w::SendInput {Blind}{s up}, and you may also want it on NumpadDot::SendInput {Blind}{s down}.
Galvatron
Posts: 16
Joined: 11 Jun 2017, 09:52

Re: Stop script from executing without altering stopping button

13 Jun 2017, 16:55

Thanks. What about using this script when I have Num Lock enabled? It seems that it works well when it's disabled but does not work with it being on.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Stop script from executing without altering stopping button

13 Jun 2017, 17:44

That's interesting. Having defined both NumpadDot and NumpadDel, and Numpad0 and NumpadIns hotkey pairs, it should overcome any kind of NumLock state. When NumLock is on, the hotkeys activated should be NumpadDot and Numpad0, whereas if NumLock is off then NumpadDel and NumpadIns should be activated. But because those pairs do the same actions...

What does your current script look that is troublesome with the NumLock?
Galvatron
Posts: 16
Joined: 11 Jun 2017, 09:52

Re: Stop script from executing without altering stopping button

19 Jun 2017, 14:55

Those are the scripts, second one is MouseDelta.ahk. NumPadDot is not working as intended and numpad remap is on bottom of first script:
#SingleInstance force
#include MouseDelta.ahk

; ============= START USER-CONFIGURABLE SECTION =============
ShiftKey := "``" ; The key used to shift DPI. Can be any key name from the AHK Key list: https://autohotkey.com/docs/KeyList.htm
ShiftMode := 0 ; 0 for shift-while-held, 1 for toggle
ScaleFactor := 3 ; The amount to multiply movement by when not in Sniper Mode
; ============= END USER-CONFIGURABLE SECTION =============

; Adjust ScaleFactor. If the user wants 2x sensitivity, we only need to send 1x input...
; ... because the user already moved the mouse once, so we only need to send that input 1x more...
; ... to achieve 2x sensitivity
ScaleFactor -= 1
SniperMode := 0
md := new MouseDelta("MouseEvent").Start()

hotkey, % ShiftKey, ShiftPressed
if (!ShiftMode){
hotkey, % ShiftKey " up", ShiftReleased
}
return

ShiftPressed:
if (ShiftMode){
SniperMode := !SniperMode
} else {
SniperMode := 1
}
md.SetState(!SniperMode)
return

ShiftReleased:
if (!ShiftMode){
SniperMode := 0
}
md.SetState(!SniperMode)
return

; Gets called when mouse moves or stops
; x and y are DELTA moves (Amount moved since last message), NOT coordinates.
MouseEvent(MouseID, x := 0, y := 0){
global ScaleFactor

if (MouseID){
x *= ScaleFactor, y *= ScaleFactor
DllCall("mouse_event",uint,1,int, x ,int, y,uint,0,int,0)
}
}

CapsLock::0
NumpadDot::SendInput {s down}
NumpadDel::SendInput {s down}
~w::SendInput {s up}
~Numpad0::SendInput {s up}
~NumpadIns::SendInput {s up}
; Instantiate this class and pass it a func name or a Function Object
; The specified function will be called with the delta move for the X and Y axes
; Normally, there is no windows message "mouse stopped", so one is simulated.
; After 10ms of no mouse movement, the callback is called with 0 for X and Y
Class MouseDelta {
State := 0
__New(callback){
;~ this.TimeoutFn := this.TimeoutFunc.Bind(this)
this.MouseMovedFn := this.MouseMoved.Bind(this)

this.Callback := callback
}

Start(){
static DevSize := 8 + A_PtrSize, RIDEV_INPUTSINK := 0x00000100
; Register mouse for WM_INPUT messages.
VarSetCapacity(RAWINPUTDEVICE, DevSize)
NumPut(1, RAWINPUTDEVICE, 0, "UShort")
NumPut(2, RAWINPUTDEVICE, 2, "UShort")
NumPut(RIDEV_INPUTSINK, RAWINPUTDEVICE, 4, "Uint")
; WM_INPUT needs a hwnd to route to, so get the hwnd of the AHK Gui.
; It doesn't matter if the GUI is showing, it still exists
Gui +hwndhwnd
NumPut(hwnd, RAWINPUTDEVICE, 8, "Uint")

this.RAWINPUTDEVICE := RAWINPUTDEVICE
DllCall("RegisterRawInputDevices", "Ptr", &RAWINPUTDEVICE, "UInt", 1, "UInt", DevSize )
OnMessage(0x00FF, this.MouseMovedFn)
this.State := 1
return this ; allow chaining
}

Stop(){
static RIDEV_REMOVE := 0x00000001
static DevSize := 8 + A_PtrSize
OnMessage(0x00FF, this.MouseMovedFn, 0)
RAWINPUTDEVICE := this.RAWINPUTDEVICE
NumPut(RIDEV_REMOVE, RAWINPUTDEVICE, 4, "Uint")
DllCall("RegisterRawInputDevices", "Ptr", &RAWINPUTDEVICE, "UInt", 1, "UInt", DevSize )
this.State := 0
return this ; allow chaining
}

SetState(state){
if (state && !this.State)
this.Start()
else if (!state && this.State)
this.Stop()
return this ; allow chaining
}

Delete(){
this.Stop()
;~ this.TimeoutFn := ""
this.MouseMovedFn := ""
}

; Called when the mouse moved.
; Messages tend to contain small (+/- 1) movements, and happen frequently (~20ms)
MouseMoved(wParam, lParam){
Critical
; RawInput statics
static DeviceSize := 2 * A_PtrSize, iSize := 0, sz := 0, pcbSize:=8+2*A_PtrSize, offsets := {x: (20+A_PtrSize*2), y: (24+A_PtrSize*2)}, uRawInput

static axes := {x: 1, y: 2}

; Get hDevice from RAWINPUTHEADER to identify which mouse this data came from
VarSetCapacity(header, pcbSize, 0)
If (!DllCall("GetRawInputData", "UPtr", lParam, "uint", 0x10000005, "UPtr", &header, "Uint*", pcbSize, "Uint", pcbSize) or ErrorLevel)
Return 0
ThisMouse := NumGet(header, 8, "UPtr")

; Find size of rawinput data - only needs to be run the first time.
if (!iSize){
r := DllCall("GetRawInputData", "UInt", lParam, "UInt", 0x10000003, "Ptr", 0, "UInt*", iSize, "UInt", 8 + (A_PtrSize * 2))
VarSetCapacity(uRawInput, iSize)
}
sz := iSize ; param gets overwritten with # of bytes output, so preserve iSize
; Get RawInput data
r := DllCall("GetRawInputData", "UInt", lParam, "UInt", 0x10000003, "Ptr", &uRawInput, "UInt*", sz, "UInt", 8 + (A_PtrSize * 2))

x := 0, y := 0 ; Ensure we always report a number for an axis. Needed?
x := NumGet(&uRawInput, offsets.x, "Int")
y := NumGet(&uRawInput, offsets.y, "Int")

this.Callback.(ThisMouse, x, y)

;~ ; There is no message for "Stopped", so simulate one
;~ fn := this.TimeoutFn
;~ SetTimer, % fn, -50
}

;~ TimeoutFunc(){
;~ this.Callback.("", 0, 0)
;~ }

}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Kodakku and 357 guests