Change script triggered by my mouse's position and exclude applications Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
yejin
Posts: 21
Joined: 10 Mar 2022, 14:44

Change script triggered by my mouse's position and exclude applications

22 Mar 2022, 04:54

This is a script from a big shot.I want to change it triggered by my mouse's position and exclude some applications, How can I do it?

Code: Select all

/*
Mouse Scroll v04 
by Mikhail V., 2021 
tested on Windows 10, AHK 1.1.28
*/

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

running := 0

; === User settings ===
swap := false 
; swap := true 				; swap scroll direction

; horiz := false 
horiz := true 				; use horizontal movement as input

k := 2					; scroll speed coefficient (higher k means higher speed)

; === Internal settings ===
scrollsLimit := 15			; max amount of scroll at once 
S := 12						; unit distance (higher S = lower speed)
T := 30					; scan frequency in MS (

; ==============

dy := 0
dyTotal := 0
scrollsTotal := 0

; #if running
loop 
{
	sleep %T%

	if (running) {
		; mousegetpos, mx						; get current mouse position 
		mousegetpos, mx, my						; get current mouse position 
		if (horiz = 0) {
			dy := k * (my - myLast)						; relative mouse movement vertical
			myLast := my									; save position
		} else {
			dy := k * (mx - mxLast)						; relative mouse movement horizontal
			mxLast := mx									; save position
		}
		dyTotal := dyTotal + dy
		scrolls := dyTotal // S
		dyTotal := dyTotal - scrolls * S					; calculate remainder after division
		direction := (scrolls >= 0) ^ swap				; get direction
		scrollsN := abs(scrolls)
		scrollsTotal := scrollsTotal + scrollsN
		n := min(scrollsN, scrollsLimit)
		; tooltip,  %scrolls% -- %dy%
		if (direction = 1) 
			send, {wheeldown %n%} 
		if (direction = 0) 
			send, {wheelup %n%} 
	}
}

rbutton::
	running := 1
	dyTotal := 0
	mousegetpos, mxLast, myLast
return

rbutton up::
	running := 0
	if (scrollsTotal = 0) 
		send {rbutton}
	scrollsTotal := 0
return

; +esc:: ExitApp
Last edited by yejin on 22 Mar 2022, 06:05, edited 1 time in total.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Change script triggered by my mouse's position and exclude applications

22 Mar 2022, 05:04

@yejin - Please use code-tags for your code above. Thx.
yejin
Posts: 21
Joined: 10 Mar 2022, 14:44

Re: Change script triggered by my mouse's position and exclude applications

22 Mar 2022, 06:23

BoBo wrote:
22 Mar 2022, 05:04
@yejin - Please use code-tags for your code above. Thx.
Edited, thanks
yejin
Posts: 21
Joined: 10 Mar 2022, 14:44

Re: Change script triggered by my mouse's position and exclude applications

22 Mar 2022, 08:16

I'm a noob and only know simple piecing together like this, obviously it won't work, hope someone can help me, thanks a lot.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
/*
Mouse Scroll v04 
by Mikhail V., 2021 
tested on Windows 10, AHK 1.1.28
*/

if !(WinActive("ahk_class Chrome_WidgetWin_1")) 
#SingleInstance force
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
running := 0

; === User settings ===
swap := false 
; swap := true 				; swap scroll direction

; horiz := false 
horiz := false 				; use horizontal movement as input

k := 2					; scroll speed coefficient (higher k means higher speed)

; === Internal settings ===
scrollsLimit := 15			; max amount of scroll at once 
S := 12						; unit distance (higher S = lower speed)
T := 30					; scan frequency in MS (

; ==============

dy := 0
dyTotal := 0
scrollsTotal := 0

; #if running
loop 
{
	sleep %T%

	if (running) {
		; mousegetpos, mx						; get current mouse position 
		mousegetpos, mx, my						; get current mouse position 
		if (horiz = 0) {
			dy := k * (my - myLast)						; relative mouse movement vertical
			myLast := my									; save position
		} else {
			dy := k * (mx - mxLast)						; relative mouse movement horizontal
			mxLast := mx									; save position
		}
		dyTotal := dyTotal + dy
		scrolls := dyTotal // S
		dyTotal := dyTotal - scrolls * S					; calculate remainder after division
		direction := (scrolls >= 0) ^ swap				; get direction
		scrollsN := abs(scrolls)
		scrollsTotal := scrollsTotal + scrollsN
		n := min(scrollsN, scrollsLimit)
		; tooltip,  %scrolls% -- %dy%
		if (direction = 1) 
			send, {wheeldown %n%} 
		if (direction = 0) 
			send, {wheelup %n%} 
	}
}
SetTimer, Check, 20
return
Check:
If Zone("scrolling_zone")
{
    running := 1
    dyTotal := 0
    mousegetpos, mxLast, myLast
}
else
If Zone("non_scrolling_zone")
{
    running := 0
    if (scrollsTotal = 0) 
    send {rbutton}
    scrollsTotal := 0
}
return
Zone(name){
      a := 1713
      b := 1160

      CoordMode, Mouse, Screen
       MouseGetPos, mX, mY

      scrolling_zone := (mX > a && mY < b)
      non_scrolling_zone := (mX < a OR mY > b)


      If (name = "scrolling_zone")
        return scrolling_zone
    else if (name = "non_scrolling_zone")
        return non_scrolling_zone
}

yejin
Posts: 21
Joined: 10 Mar 2022, 14:44

Re: Change script triggered by my mouse's position and exclude applications

22 Mar 2022, 10:25

The first is the original script, and the second is what I changed, the script does not respond, I want when my mouse moves to the scrolling_zone(x>1713,Y<1160), I can use the mouse movement to scroll, when move out, release the scroll block. And to be able to exclude specified applications like chrome which I would like the script not to work.
RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: Change script triggered by my mouse's position and exclude applications  Topic is solved

22 Mar 2022, 12:10

While I haven't analyzed your script in extreme detail, a couple of things pop out at me:

After your variable declarations, you enter a loop from which there is no escape. Your functions and labels are not called from within that loop, so they will never be executed. Neither will your timer.

You have way more code in your Zone() function than you need. When you have only two possible outcomes (in the zone or not), there is no need to explicitly test for each condition. either it is "in the zone" or it isn't.

Your function only needs to return true if it is in, or false otherwise. (put a space after "()" when you define a function or drop the open brace to the next line.)

Code: Select all

Zone() {
      a := 1713
      b := 1160

      MouseGetPos, mX, mY
      Return (mX > a && mY < b)
}
You then only need to call Zone() with

Code: Select all

If (Zone()) {
	; Do this code
}
Else {
	; Do that code
}
Also, you should put your CoordMode command in the autoexec portion at the top of the script rather than within your function

Russ
yejin
Posts: 21
Joined: 10 Mar 2022, 14:44

Re: Change script triggered by my mouse's position and exclude applications

23 Mar 2022, 07:10

@RussF
Thank you very much, it works, but there is another problem, the distance of the scroll block is not the same as the mouse, how to keep it consistent?

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
/*
Mouse Scroll v04 
by Mikhail V., 2021 
tested on Windows 10, AHK 1.1.28
*/

#SingleInstance force
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
running := 0
CoordMode, Mouse, Screen
SetTimer, Check, 20
Thread, interrupt, 0 

; === User settings ===
swap := false 
; swap := true 				; swap scroll direction

; horiz := false 
horiz := false 				; use horizontal movement as input

k := 1					; scroll speed coefficient (higher k means higher speed)

; === Internal settings ===
scrollsLimit := 30			; max amount of scroll at once 
S := 1						; unit distance (higher S = lower speed)
T := 30					; scan frequency in MS (

; ==============

dy := 0
dyTotal := 0
scrollsTotal := 0

; #if running
loop 
{
	sleep %T%

	if (running) {
		; mousegetpos, mx						; get current mouse position 
		mousegetpos, mx, my						; get current mouse position 
		if (horiz = 0) {
			dy := k * (my - myLast)						; relative mouse movement vertical
			myLast := my									; save position
		} else {
			dy := k * (mx - mxLast)						; relative mouse movement horizontal
			mxLast := mx									; save position
		}
		dyTotal := dyTotal + dy
		scrolls := dyTotal // S
		dyTotal := dyTotal - scrolls * S					; calculate remainder after division
		direction := (scrolls >= 0) ^ swap				; get direction
		scrollsN := abs(scrolls)
		scrollsTotal := scrollsTotal + scrollsN
		n := min(scrollsN, scrollsLimit)
		; tooltip,  %scrolls% -- %dy%
		if (direction = 1) 
			send, {wheeldown %n%} 
		if (direction = 0) 
			send, {wheelup %n%} 
	}
}
Check:
If Zone(scrolling)
{
    running := 1
    dyTotal := 0
    mousegetpos, mxLast, myLast
}
else
{
    running := 0
    if (scrollsTotal = 0) 
    Zone(scrolling) := 1
    scrollsTotal := 0
}
Zone(scrolling){
      a := 1713
      b := 1160

      
       MouseGetPos, mX, mY

      return (mX > a && mY < b)
}
return


RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: Change script triggered by my mouse's position and exclude applications

23 Mar 2022, 07:51

yejin wrote: the distance of the scroll block is not the same as the mouse, how to keep it consistent?
I'm not sure I understand what you mean.
yejin wrote:Thank you very much, it works,
I don't see how. Again, not analyzing Mikhail's script, just your additions, see the comments:

Code: Select all

Check:
If Zone(scrolling) ; the variable "scrolling" is not defined or set anywhere.  Why are you using it?
{
    running := 1
    dyTotal := 0
    mousegetpos, mxLast, myLast
}
else
{
    running := 0
    if (scrollsTotal = 0) 
    Zone(scrolling) := 1	; You cannot assign a value to a function other than passing an argument - which, in this case, is an invalid variable.  What exactly are you trying to do here?
    scrollsTotal := 0
}
Return		; this was moved up from the bottom of the script where it is unnecessary

Zone(scrolling){		; again - you are not using "scrolling" in this function. the function can be defined as just "Zone() {"
      a := 1713
      b := 1160
      MouseGetPos, mX, mY
      return (mX > a && mY < b)
}
; return - this does nothing here
Russ
yejin
Posts: 21
Joined: 10 Mar 2022, 14:44

Re: Change script triggered by my mouse's position and exclude applications

23 Mar 2022, 08:42

@Russ
I don't know what I did, but it does work, I mean the distance I move the mouse is not the same as the distance the scroll block moves, the mouse moves the distance a, but the scroll block moves the distance b, I want a=b, the distance the mouse moves is sent to the wheel event after some calculation, but I don't want this, how to get the mouse movement distance?
RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: Change script triggered by my mouse's position and exclude applications

23 Mar 2022, 09:11

yejin wrote:
23 Mar 2022, 08:42
@Russ
I don't know what I did, but it does work, I mean the distance I move the mouse is not the same as the distance the scroll block moves, the mouse moves the distance a, but the scroll block moves the distance b, I want a=b, the distance the mouse moves is sent to the wheel event after some calculation, but I don't want this, how to get the mouse movement distance?
It looks like you have changed Mikhail's original values for K and S from 2 and 12 respectively to 1 for both of them. Since the comments say that they have to do with scroll speed and unit distance, that would be the first place I would look. Try changing the values and see what happens.

Russ
yejin
Posts: 21
Joined: 10 Mar 2022, 14:44

Re: Change script triggered by my mouse's position and exclude applications

23 Mar 2022, 09:39

RussF wrote:
23 Mar 2022, 09:11
yejin wrote:
23 Mar 2022, 08:42
@Russ
I don't know what I did, but it does work, I mean the distance I move the mouse is not the same as the distance the scroll block moves, the mouse moves the distance a, but the scroll block moves the distance b, I want a=b, the distance the mouse moves is sent to the wheel event after some calculation, but I don't want this, how to get the mouse movement distance?
It looks like you have changed Mikhail's original values for K and S from 2 and 12 respectively to 1 for both of them. Since the comments say that they have to do with scroll speed and unit distance, that would be the first place I would look. Try changing the values and see what happens.

Russ
Yes, because I see that there are multiplication and division operations, I thought changing to 1 would keep the value unchanged, but it didn't work, another way is to replace it with a new code, that is how to get the mouse movement distance value and send it directly to the wheel event, but I have no idea and can't find relevant examples and tutorials.
RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: Change script triggered by my mouse's position and exclude applications

23 Mar 2022, 09:51

Again...
RussF wrote:Try changing the values and see what happens.
Russ
yejin
Posts: 21
Joined: 10 Mar 2022, 14:44

Re: Change script triggered by my mouse's position and exclude applications

23 Mar 2022, 10:14

There doesn't seem to be a better way, so be it, thank you.
mikhail22
Posts: 19
Joined: 14 Jan 2018, 11:50

Re: Change script triggered by my mouse's position and exclude applications

23 Mar 2022, 17:30

yejin wrote:
23 Mar 2022, 08:42
@Russ
I don't know what I did, but it does work, I mean the distance I move the mouse is not the same as the distance the scroll block moves, the mouse moves the distance a, but the scroll block moves the distance b, I want a=b, the distance the mouse moves is sent to the wheel event after some calculation, but I don't want this, how to get the mouse movement distance?
Hi @yejin I'm the author of the script ;) .
So the script just sends some amount of wheelup/wheeldown events (same as scrolling the mouse wheel) and of course it is not same pixel size as the mouse movement. And it is impossible to do what you want. Most windows scrolls by some amount of lines of text, not pixels, and it is different in most applications.
yejin
Posts: 21
Joined: 10 Mar 2022, 14:44

Re: Change script triggered by my mouse's position and exclude applications

24 Mar 2022, 09:49

mikhail22 wrote:
23 Mar 2022, 17:30
yejin wrote:
23 Mar 2022, 08:42
@Russ
I don't know what I did, but it does work, I mean the distance I move the mouse is not the same as the distance the scroll block moves, the mouse moves the distance a, but the scroll block moves the distance b, I want a=b, the distance the mouse moves is sent to the wheel event after some calculation, but I don't want this, how to get the mouse movement distance?
Hi @yejin I'm the author of the script ;) .
So the script just sends some amount of wheelup/wheeldown events (same as scrolling the mouse wheel) and of course it is not same pixel size as the mouse movement. And it is impossible to do what you want. Most windows scrolls by some amount of lines of text, not pixels, and it is different in most applications.
Hi thanks for the script, it's great, I get it, thanks.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], just me, Mateusz53, mikeyww, MrHue, mstrauss2021 and 327 guests