Move Cursor To Send Commands

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Move Cursor To Send Commands

Post by scriptor2016 » 19 May 2022, 00:07

Just when I thought I'd asked every question I ever needed, I come across another.

Say for example, while TAB is held down, and the mouse is being moved to the right, it sends a command.

So for example:

-First, record the starting position of the cursor with MouseGetPos
-First, record the second position of the cursor with MouseGetPos again
-Then, move the cursor up to 25 pixels to the right of the starting point, and it sends keystroke A one time (not repetitively)
-Continue to move the cursor to the right, and when it hits 50 pixels from the starting point, it sends keystroke B one time (not repetitively)
-Continue to move the cursor to the right, and when it hits 75 pixels from the starting point, it sends C one time (not repetitively)
...and so forth.

I've been playing with this all night but can't get it. I'll post what I have so far, any chance someone could take a look at this? Thanks in advance..

Code: Select all

SendMode Input
CoordMode Mouse, Screen ;doesn’t matter where mouse cursor is
SetMouseDelay,-1

p=1 ;how many pixels in any direction the mouse must move before it sends a keystroke (you can experiment with this)

tab::
Loop
 {
GetKeyState, state, tab, p
if state = u
break

if state = d

MouseGetPos x1,y1 ;original cursor position
MouseGetPos x2,y2 ;second cursor position
dx:= (x2-x1)//p, dy:= (y2-y1)//p 


 

If (dx between 0 and 24)
{
sendinput, A
}

If (dx between 25 and 49)
{
sendinput, B
}

If (dx between 50 and 74)
{
sendinput, C
}

If (dx between 75 and 99)
{
sendinput, D
}

Return

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

Re: Move Cursor To Send Commands

Post by Rohwedder » 19 May 2022, 00:38

Hallo,
try:

Code: Select all

SendMode Input
CoordMode Mouse, Screen ;doesn’t matter where mouse cursor is
SetMouseDelay,-1
p = 25 ;how many pixels in any direction the mouse must move before it sends a keystroke (you can experiment with this)
$Tab::
Sending =
MouseGetPos x1,y1 ;original cursor position
While, GetKeyState("Tab","P")
{
	MouseGetPos x2,y2 ;second cursor position
	Letter := Chr(65+Max((x2-x1)//p,0)) ;A=CHR(65), B=CHR(66)...
	dy:= (y2-y1)//p
	IF InStr(Sending, Letter)
		Continue ;was already sent
	sendinput,% Letter
	Sending .= Letter ;Letters which have already been sent
}
Return

scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: Move Cursor To Send Commands

Post by scriptor2016 » 19 May 2022, 01:00

Thanks Rohwedder

Another question - I'm hoping to be able to send a custom command for a each letter. For example, when it reaches H, then maybe it sends a messagebox, H. Or maybe when it hit K, it will do something like Soundbeep. And maybe when it hits S, it will do something else like, Gui, Destroy (just as examples)

Better yet though, if using letters of the alphabet, we're limited to just 26 options. Can it be changed to use numbers instead, so that there are infinite options?


I've added a little bit to your code. Is this the right way to go about it?

Code: Select all

SendMode Input
CoordMode Mouse, Screen ;doesn’t matter where mouse cursor is
SetMouseDelay,-1
p = 25 ;how many pixels in any direction the mouse must move before it sends a keystroke (you can experiment with this)
$Tab::
Sending =
MouseGetPos x1,y1 ;original cursor position
While, GetKeyState("Tab","P")
{
	MouseGetPos x2,y2 ;second cursor position
	Letter := Chr(65+Max((x2-x1)//p,0)) ;A=CHR(65), B=CHR(66)...
	dy:= (y2-y1)//p
	IF InStr(Sending, Letter)
		Continue ;was already sent
	sendinput,% Letter
	Sending .= Letter ;Letters which have already been sent


if (Letter = A) 
tooltip, A

if (Letter = B)
tooltip, B

if (Letter = C)
tooltip, C

if (Letter = D)
tooltip, D
}  

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

Re: Move Cursor To Send Commands

Post by Rohwedder » 19 May 2022, 01:22

Then,
try:

Code: Select all

SendMode Input
CoordMode Mouse, Screen ;doesn’t matter where mouse cursor is
SetMouseDelay,-1
p = 25 ;how many pixels in any direction the mouse must move before it sends a keystroke (you can experiment with this)
$Tab::
Activities = ,
MouseGetPos x1,y1 ;original cursor position
While, GetKeyState("Tab","P")
{
	MouseGetPos x2,y2 ;second cursor position
	Activity := Max((x2-x1)//p,0)
	dy:= (y2-y1)//p
	IF InStr(Activities, "," Activity ",")
		Continue ;was already made
	Activities .= Activity "," ;Activities which have already been made
	ToolTip,% Activities ;only for test
	Switch, Activity
	{
	Case 0:SoundBeep, 4000, 20
	Case 2:ToolTip 2
	Case 4:MsgBox 8
	}
}
Return

scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: Move Cursor To Send Commands

Post by scriptor2016 » 19 May 2022, 01:30

this is excellent :) :) :)

Can I ask one more question? (I promise, it will be the last one for tonight ;) )

Is there a way to also go backwards? For example if you get up to 6, and then move the mouse opposite direction to the left, it will go in reverse like 5, 4, 3, 2, 1...



Code: Select all

SendMode Input
CoordMode Mouse, Screen ;doesn’t matter where mouse cursor is
SetMouseDelay,-1
p = 25 ;how many pixels in any direction the mouse must move before it sends a keystroke (you can experiment with this)
$Tab::
Sending =
MouseGetPos x1,y1 ;original cursor position
While, GetKeyState("Tab","P")
{
	MouseGetPos x2,y2 ;second cursor position
	Activity := Max((x2-x1)//p,0)
	dy:= (y2-y1)//p
	IF InStr(Activities, Activity ",")
		Continue ;was already made
	Activities .= Activity "," ;Activities which have already been made
	Switch, Activity
	{
	Case 0:ToolTip 0
        Case 1:ToolTip 1
	Case 2:ToolTip 2
	Case 3:
        ToolTip 3
        Soundbeep
        Case 4:ToolTip 4
        Case 5:ToolTip 5
        Case 6:ToolTip 6
	}
}
Return 

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

Re: Move Cursor To Send Commands

Post by Rohwedder » 19 May 2022, 01:36

This contradicts your premise that each activity should be executed only once per tab press!

scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: Move Cursor To Send Commands

Post by scriptor2016 » 19 May 2022, 01:39

I think what I meant was that on each tab, it would send a keystroke once, (not repetitively like as if the keystroke was being held down)

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

Re: Move Cursor To Send Commands

Post by Rohwedder » 19 May 2022, 01:46

Then perhaps?:

Code: Select all

SendMode Input
CoordMode Mouse, Screen ;doesn’t matter where mouse cursor is
SetMouseDelay,-1
p = 25 ;how many pixels in any direction the mouse must move before it sends a keystroke (you can experiment with this)
$Tab::
OldActivity =
MouseGetPos x1,y1 ;original cursor position
While, GetKeyState("Tab","P")
{
	MouseGetPos x2,y2 ;second cursor position
	Activity := (x2-x1)//p
	dy:= (y2-y1)//p
	IF (Activity = OldActivity)
		Continue
	OldActivity := Activity
	Switch, Activity
	{
	Case 0:SoundBeep, 4000, 20
	Case 2:ToolTip 2
	Case 4:MsgBox 8
	}
}
Return

scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: Move Cursor To Send Commands

Post by scriptor2016 » 19 May 2022, 01:56

ok this is awesome, I can't thank you enough :) :) :)

It's exactly what I was hoping for. I'll build on this now and maybe add the y-axis to do the same thing as the x-axis, this way I can add a ton more commands.

One thing, when the code (below) reaches 6, and then when the mouse is moved in reverse to the left, it should hit number 5 then 4 then 3 etc, but instead of going back to 5 it repeats the 6 one extra time before going on to 5, 4, 3, 2, 1....

it's okay on the 0 end though. It goes 6,5,4,3,2,1,0,1,2,3,4,5,6 and so on and it's perfect.

But the other way it goes:

0,1,2,3,4,5,6,6,5,4,3,2,1,0 (note the two 6's)

Code: Select all

SendMode Input
CoordMode Mouse, Screen ;doesn’t matter where mouse cursor is
SetMouseDelay,-1
p = 25 ;how many pixels in any direction the mouse must move before it sends a keystroke (you can experiment with this)
$Tab::
OldActivity =
MouseGetPos x1,y1 ;original cursor position
While, GetKeyState("Tab","P")
{
	MouseGetPos x2,y2 ;second cursor position
	Activity := Max((x2-x1)//p,0)
	dy:= (y2-y1)//p
	IF (Activity = OldActivity)
		Continue
	OldActivity := Activity
	Switch, Activity
	{
	Case 0:
        ToolTip 0
        SoundBeep, 1000, 20

	Case 1:
        ToolTip 1
        SoundBeep, 2000, 20

	Case 2:
        ToolTip 2
        SoundBeep, 3000, 20

	Case 3:
        ToolTip 3
        SoundBeep, 4000, 20

	Case 4:
        ToolTip 4
        SoundBeep, 5000, 20

	Case 5:
        ToolTip 5
        SoundBeep, 6000, 20

	Case 6:
        ToolTip 6
        SoundBeep, 7000, 20
         
	}
}
Return

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

Re: Move Cursor To Send Commands

Post by Rohwedder » 19 May 2022, 02:03

Then:

Code: Select all

SendMode Input
CoordMode Mouse, Screen ;doesn’t matter where mouse cursor is
SetMouseDelay,-1
p = 25 ;how many pixels in any direction the mouse must move before it sends a keystroke (you can experiment with this)
$Tab::
OldActivity =
MouseGetPos x1,y1 ;original cursor position
While, GetKeyState("Tab","P")
{
	MouseGetPos x2,y2 ;second cursor position
	Activity := Min((x2-x1)//p, 6) ;Maximum up to activity 6
	dy:= (y2-y1)//p
	IF (Activity = OldActivity)
		Continue
	OldActivity := Activity
	Switch, Activity
	{
	Case 0:
        ToolTip 0
        SoundBeep, 1000, 20

	Case 1:
        ToolTip 1
        SoundBeep, 2000, 20

	Case 2:
        ToolTip 2
        SoundBeep, 3000, 20

	Case 3:
        ToolTip 3
        SoundBeep, 4000, 20

	Case 4:
        ToolTip 4
        SoundBeep, 5000, 20

	Case 5:
        ToolTip 5
        SoundBeep, 6000, 20

	Case 6:
        ToolTip 6
        SoundBeep, 7000, 20
         
	}
}
Return

scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: Move Cursor To Send Commands

Post by scriptor2016 » 19 May 2022, 02:10

this is insane haha. It's perfect. I'm sure many people will find good uses for this. I'll continue to work on it and post back what I come up with so it all makes sense.

Thanks again, really appreciate your time on this :)

scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: Move Cursor To Send Commands

Post by scriptor2016 » 19 May 2022, 02:25

Just a quick update on what this was all about. It works when Notepad is the active window and will send the text to it when TAB is released. This isn't what's intended, just an example to show what the idea is.

Code: Select all

SendMode Input
CoordMode Mouse, Screen ;doesn’t matter where mouse cursor is
SetMouseDelay,-1
p = 100 ;how many pixels in any direction the mouse must move before it sends a keystroke (you can experiment with this)
$Tab::
OldActivity =
MouseGetPos x1,y1 ;original cursor position
While, GetKeyState("Tab","P")
{
	MouseGetPos x2,y2 ;second cursor position
	Activity := Min((x2-x1)//p, 6) ;Maximum up to activity 6
	dy:= (y2-y1)//p
	IF (Activity = OldActivity)
		Continue
	OldActivity := Activity
	Switch, Activity
	{
	Case 0:
        ToolTip 0  SEND "FIRST TAB"
        SoundBeep, 1000, 20
        TAB=FIRST TAB

	Case 1:
        ToolTip 1  SEND "TAB ONE"
        SoundBeep, 2000, 20
        TAB=TAB ONE

	Case 2:
        ToolTip 2  SEND "TAB TWO"
        SoundBeep, 3000, 20
        TAB=TAB TWO

	Case 3:
        ToolTip 3  SEND "TAB THREE"
        SoundBeep, 4000, 20
        TAB=TAB THREE

	Case 4:
        ToolTip 4  SEND "TAB FOUR"
        SoundBeep, 5000, 20
        TAB=TAB FOUR

	Case 5:
        ToolTip 5  SEND "TAB FIVE"
        SoundBeep, 6000, 20
        TAB=TAB FIVE

	Case 6:
        ToolTip 6  SEND "TAB SIX"
        SoundBeep, 7000, 20
        TAB=TAB SIX 
	}
}
Return


Tab Up::
Sendinput, {Enter}
Sendinput, %TAB%
Return

Post Reply

Return to “Ask for Help (v1)”