Scroll Without Moving Cursor Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Sweetins
Posts: 110
Joined: 02 Jul 2017, 13:22

Scroll Without Moving Cursor

Post by Sweetins » 24 Jan 2022, 07:48

Hello all, basically am trying to scroll in MS Word like I would in a pdf.
Am trying to prevent the down or up arrow keys from moving the cursor when pressed but only scroll.
Apparently there appears to be some difference between the two hotkeys below and I don't seem to understand why.

Code: Select all

Up::Click WU

Up::
if pagelight 
Click WU
else
Send {Up}
return
The first one works fine as I expect but in this single line hotkey I can't seem to include any condition. The second one does the scrolling but also moves the cursor. First off can anyone help explain how the single line hotkey differs from the multi? And secondly how do I get my scrolling happening without cursor movement (there has to be a means of basing the hotkey on a condition.

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Scroll Without Moving Cursor  Topic is solved

Post by mikeyww » 24 Jan 2022, 08:09

It works but depends on the value of pagelight. Use the hook.

Code: Select all

$Up::
If pagelight
 Click WU
Else Send {Up}
SoundBeep, 1500
Return
Explained: Dollar

Maybe easier:

Code: Select all

#If pagelight
Up::Click WU
#If

Sweetins
Posts: 110
Joined: 02 Jul 2017, 13:22

Re: Scroll Without Moving Cursor

Post by Sweetins » 24 Jan 2022, 09:18

mikeyww wrote:
24 Jan 2022, 08:09
It works but depends on the value of pagelight. Use the hook.

Code: Select all

$Up::
If pagelight
 Click WU
Else Send {Up}
SoundBeep, 1500
Return
Explained: Dollar
Than for the response. But same result, scrolls and moves the cursor up. And if am right the $ is to prevent the hotkey from triggering itself in case the Send command is to activate a trigger. The condition pagelight is always true so the Send doesn't even get to happen.
mikeyww wrote:
24 Jan 2022, 08:09
Maybe easier

Code: Select all

#If pagelight
Up::Click WU
#If
This might work but there already a contest sensitive condition for MS Word within which all the hotkeys are. I'd not liked to create another #If contest.

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Scroll Without Moving Cursor

Post by mikeyww » 24 Jan 2022, 09:20

Try the posted script by itself before integrating it into your own script. You can report back on whether this script works.

By itself:

Code: Select all

pagelight := True
#If pagelight
Up::Click WU
#If

Sweetins
Posts: 110
Joined: 02 Jul 2017, 13:22

Re: Scroll Without Moving Cursor

Post by Sweetins » 24 Jan 2022, 10:49

mikeyww wrote:
24 Jan 2022, 08:09
Maybe easier

Code: Select all

#If pagelight
Up::Click WU
#If
This might work but there already a contest sensitive condition for MS Word within which all the hotkeys are. I'd not liked to create another #If contest.
Yh, using the #If contest will work indeed like I did imply

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Scroll Without Moving Cursor

Post by mikeyww » 24 Jan 2022, 11:05

If this script works alone but not inside your script, then you can post your script for feedback about it.

Sweetins
Posts: 110
Joined: 02 Jul 2017, 13:22

Re: Scroll Without Moving Cursor

Post by Sweetins » 24 Jan 2022, 12:18

It works as well. Thanks a lot.

But just thinking out loud why won't this just scroll without moving the cursor Up

Code: Select all

Pagelight:=True
 Up::
 If Pagelight
 Click WU
 return
But this does it perfectly?

Code: Select all

 Up::Click WU
What is it with executing the If condition first before the Click WU makes it move the cursor as well as scroll?

User avatar
mikeyww
Posts: 26851
Joined: 09 Sep 2014, 18:38

Re: Scroll Without Moving Cursor

Post by mikeyww » 24 Jan 2022, 12:23

That's exactly what this script does. Your script is a different script, which you have not posted.

Your bug is not "If" but is instead line 1.

Explained: Auto-execute

Unlabeled code between hotkey routines is unreachable and will never execute.

Sweetins
Posts: 110
Joined: 02 Jul 2017, 13:22

Re: Scroll Without Moving Cursor

Post by Sweetins » 25 Jan 2022, 02:10

mikeyww wrote:
24 Jan 2022, 12:23
That's exactly what this script does. Your script is a different script, which you have not posted.
That's very true... having had a second look at the script and doing a clean troubleshoot (analyzing with ListLines), it turns out the problem wasn't the if statement at all but a practice I've long adopted was failing me.

Code: Select all

pagelight:= True
Up::
if pagelight 
Click WU
else
Up::Up
return

Code: Select all

pagelight:= True
Up::
if pagelight 
Click WU
else
{
	Up::Up
	}
return
The two script above were all run alone (the first, being by initial) and the second works alright. It just scrolls and doesn't move the cursor.

First of, normally if I want to make the function of a hotkey an option in an if statement as in the above two scripts, I use Up::Up instead of Send {Up}. Secondly, I know single line commands for If statements doesn't need curly braces.

If I just changed the Up::Up to Send {Up} in the first script it works fine. If I wanted to maintain Up::Up and just used curly braces around it (as in the case of the second script above), it then also works fine.

The lesson to be length is not quiet clear. Should it be a practice to always use curly braces in an if statement (whether there are single or multiline commands)? Or having been using Up::Up in most instances in my script (like in the scripts above) which tends to work perfectly than Send {Up}, it is actually not a good practice?

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Scroll Without Moving Cursor

Post by boiler » 25 Jan 2022, 05:38

Hotkeys, hotstrings, and key remaps like Up::Up are not subject to regular if/else conditions. Do not put hotkey-type definitions inside your code flow like that. The way you put conditions on hotkeys and the like are with directives: #If and #IfWin… See the examples on those pages and study Structure of a Script.

Sweetins
Posts: 110
Joined: 02 Jul 2017, 13:22

Re: Scroll Without Moving Cursor

Post by Sweetins » 25 Jan 2022, 16:45

Well noted. Thanks a lot @boiler .

Post Reply

Return to “Ask for Help (v1)”