Click and drag one pixel in given direction - autohotkey

I am looking for a script that will click and hold the right mouse button down and drag in a given direction 1 pixel every "x" or 2 seconds. Something I can either tell to move in given direction by hitting the corresponding direction key or by adjusting the script manually.
Thank you!

Not entirely sure what you want, but try this:
CoordMode, Pixel, Screen
direction = left
secondsBetweenMoves = 0.1
F1:: ;F1 to start it
SendInput, {LButton Down}
SetTimer, Move, %secondsBetweenMoves%
Return
F2:: ;F2 to end it
SendInput, {LButton Up}
SetTimer, Move, Off
Return
Move:
MouseGetPos, mouseX, mouseY
If (direction = "left") {
MouseMove, mouseX-1, mouseY
}
Else If (direction = "right") {
MouseMove, mouseX+1, mouseY
}
Else If (direction = "up") {
MouseMove, mouseX, mouseY-1
}
Else If (direction = "down") {
MouseMove, mouseX, mouseY+1
}
Return

The send event command might make your life a whole lot easier. For example, what you're trying to do in a simplistic infinite loop:
coordmode, mouse, screen
setmousedelay, 0 # This makes the mouse move extremely fast
loop {
mousegetpos, mx, my
mx := mx+1
sendevent {click, r, down}
sendevent {click, %mx%, %my%, r, up}
sleep 2000
}
This loop will hold down the right button and move one pixel to the right every two seconds. If you want it to move to the left, change
mx := mx+1
to
mx := mx-1
Making it move up or down is the same mechanism, but adding or subtracting to the variable "my" instead.
This loop can be made into a timer, with hotkeys to enable or disable it.

Related

How do I activate a script only if the mouse is at a certain coordinate of the screen without interfering with my keyboard?

I cooked up a script that lets me map my keyboard's media shortcuts to my mouse LRM buttons when they are pressed while the mouse coordinate is leftmost or rightmost of the screen. While it does work, I'm having having strange side effects:
When I have caps lock on, ever few strokes the letter comes out lowercase.
When I use shift to type capital letters for an extended period of time, this will turn on caps lock
Using the keyboard history, I see that my script is constantly sending the "Alt Up" key, I did this so that it release the "Alt Down" state, but something is off.
My goal is to send an modifier key when a mouse is over a certain coordinate, so that when I click with that mouse button, it launches another ahk-programmed shortcut. But can't figure out where the logic error is in my code or thinking process.
Here's the script:
; ------------------------
; Global Initializers
#InstallKeybdHook
#MaxThreadsPerHotkey 1
; ---------------------
; Control Spotify; position your mouse top-most edge and use L/M/R-mouse keys.
SetTimer, WatchCursorx, 1000
return
WatchCursorx:
CoordMode, Mouse, Screen
MouseGetPos, xpos, ypos
;Based on location of the mouse simulate shortcut activation
If (xpos == 2559 || xpos == 0)
{
Send {Alt Down}
}
Else
{
Send {Alt Up}
}
return
;Define shortcuts mentioned above
!RButton::
Send {Media_Next}
return
!LButton::
Send {Media_Prev}
return
!MButton::
send {Media_Play_Pause}
return
#If(docs) is meant for this.
You could use it for example like this:
CoordMode, Mouse, Screen
#If, MouseOnTheRight()
LButton::SendInput, {Media_Prev}
RButton::SendInput, {Media_Next}
MButton::SendInput, {Media_Play_Pause}
#If
MouseOnTheRight()
{
MouseGetPos, x
return x == A_ScreenWidth - 1
}
Per my comments, try it like this:
CoordMode, Mouse, Screen
~RButton::
MouseGetPos, xpos, ypos
If (xpos == 2559 || xpos == 0)
{
Send {Media_Next}
sleep, 500
Send {esc} ' this gets rid of right context menu
}
return
~LButton::
MouseGetPos, xpos, ypos
If (xpos == 2559 || xpos == 0)
Send {Media_Prev}
return
~MButton::
MouseGetPos, xpos, ypos
If (xpos == 2559 || xpos == 0)
Send {Media_Play_Pause}
return
Note, the preceding ~ lets the original mouse click go through so ordinarily the context menu will come up on right click. I add a Sleep and Send Escape key to dismiss . . . Ymmv

AutoHotkey Run script while holding down key

I need help with a script, i want it to only run while im holding down a key. Heres the script:
;If you use this, you have to use absolute screen coordinates.
CoordMode, Mouse, Screen
;Suppose a 100x100 px bounding box for your game inventory.
;Eg., from (500, 500) to (600, 600)
#if GetKeyState("joy5")
joy5:: MouseMove, 1771, 531
joy5 Up::MouseMove %oldx%,%oldy%
Numpad8::
{
;Get current Mouse coords
MouseGetPos, xCurrent ,yCurrent
;Calculate future Mouse coords
xMoved := xCurrent
yMoved := yCurrent - 35
;Check if the future mouse postion will be
;below the top border of your bounding box,
;aka still inside it, after it has moved.
;If so, proceed and move the mouse,
;otherwise do nothing.
MouseGetPos, CoordXRec, CoordYRec
MouseMove, xMoved, yMoved
if(yMoved < 503 && yMoved > 350 && yMoved > 360){
MouseMove 1846, 166
}
if(yMoved < 145){
MouseMove, %CoordXRec%, %CoordYRec%, 0
}
if(yMoved < 718 && yMoved < 720 && yMoved > 680){
MouseMove 1771, 671
}
return
}
Numpad5::
{
;Get current Mouse coords
MouseGetPos, xCurrent ,yCurrent
;Calculate future Mouse coords
xMoved := xCurrent
yMoved := yCurrent +35
;Check if the future mouse postion will be
;below the top border of your bounding box,
;aka still inside it, after it has moved.
;If so, proceed and move the mouse,
;otherwise do nothing.
MouseMove, xMoved, yMoved
if(yMoved > 285 && yMoved < 360){
MouseMove 1773, 526
}
if(yMoved > 697 && yMoved < 715){
MouseMove 1772, 736
}
return
}
Numpad4::
{
;Get current Mouse coords
MouseGetPos, xCurrent ,yCurrent
;Calculate future Mouse coords
xMoved := xCurrent -40
yMoved := yCurrent
;Check if the future mouse postion will be
;below the top border of your bounding box,
;aka still inside it, after it has moved.
;If so, proceed and move the mouse,
;otherwise do nothing.
if (xMoved > 1740) {
MouseMove, xMoved, yMoved
}
return
}
Numpad6::
{
;Get current Mouse coords
MouseGetPos, xCurrent ,yCurrent
;Calculate future Mouse coords
xMoved := xCurrent +40
yMoved := yCurrent
;Check if the future mouse postion will be
;below the top border of your bounding box,
;aka still inside it, after it has moved.
;If so, proceed and move the mouse,
;otherwise do nothing.
if (xMoved < 1917) {
MouseMove, xMoved, yMoved
}
return
}
Basicly you control the mouse with WASD and theres some other functionality to it aswell but i want to make it so that you have to hold down a key in order to move. Thanks!
only move when holding down a key.
Move mouse to 500,500 when Number-Pad-8 is pressed and NumLock is on. Return mouse to original location when key is released.
Numpad8::move()
Numpad8 UP::unmove()
move()
{
global oldx,oldy
MouseGetPos oldx,oldy
MouseMove 500,500
}
unmove()
{
global oldx,oldy
MouseMove %oldx%,%oldy%
}
To answer your original question (if the question changed so drastically, please open another one and finish this one):
You can dis/enable hotkeys dynamically, using the Hotkey-command. Assuming your masterkey is Space:
space:: ; this is a static hotkey definition
hotkey, numpad8, moveMouse1 ; this is a dynamic hotkey definition
hotkey, numpad6, moveMouse2
; etc
return
space up::
hotkey, numpad8, OFF ; this is a dynamic hotkey removal
hotkey, numpad6, OFF
; etc
return
moveMouse1: ; this is a label
moveMouse 50, 100
; your actions
return
moveMouse2:
; ....
return

pixelsearch and click right pixel

IF NOT A_IsAdmin ; Runs script as Admin.
{
Run *RunAs "%A_ScriptFullPath%"
ExitApp
}
#MaxThreadsPerHotkey, 2
CoordMode, Pixel, Screen
#singleInstance, Force
toggle = 0
upperLeftX := 750
upperLeftY := 400
lowerRightX := 850
lowerRightY := 500
F8:: ; press F8 to toggle the loop on/off.
SoundBeep
Toggle := !Toggle
While Toggle
{ ;-- Begin of loop.
PixelSearch, X, Y,%upperLeftX%, %upperLeftY%, %lowerRightX%, %lowerRightY%, 0x000000, 0, Fast RGB
IF ErrorLevel = 1 ; IF NOTFound.
{
sleep, 100
}
IF ErrorLevel = 0 ; IF Found.
{
MouseClick, left
sleep, 300
}
} ;-- End of Loop.
return
F8 starts loop and this code checks specific pixel in rectangle and sends left click.
It works with [MouseClick, left, %X%, %Y%].But I want to know how can I use dllcall mouse event to click on specific pixel.
for example
DllCall("mouse_event",uint,1,int,%X%,int,%Y%,uint,0,int,0)
But its not working
I doubt that you actually want to do this via DLL calls. mouse_event doesn't even take coordinates, but values between 0-65535.
If you want to be able to click any pixel on the screen make sure you set it to be relative to the screen: CoordMode, Mouse, Screen
Then use ControlClick/PostMessage/SendMessage if you don't want to affect your mouse pointer by that click. Or use MouseClick/Click. Or MouseMove+Send, {LButton}.

Temporarily Pause Autofire loop on holding button

I wrote a script that sends autofire left clicks and can be triggered on and off. The script works. However, the problem is that holding the right mouse button does not work properly anymore because the left click keeps getting sent. So I want to change the script that it gets temporarily paused while I hold down the right mouse button.
How would I go about doing this? Here is my current code:
#MaxThreadsPerHotkey 3
#z::
#MaxThreadsPerHotkey 1
if keep_winz_running = y
{
keep_winz_running = n
return
}
; Otherwise:
keep_winz_running = y
Loop
{
GetKeyState, rbut, Rbutton
If rbut, = U
{
Loop,
{
MouseClick, left
Sleep, 50 ;This means the script will wait 1.5 secs
if keep_winz_running = n ; The user signaled the loop to stop.
break ; break out of the loop
}
Timers are the best!
sendToggle := false
#z::
if(!sendToggle) {
sendToggle := true
SetTimer, SendClick, 100
} else {
sendToggle := false
SetTimer, SendClick, Off
}
return
#If sendToggle
RButton::
SetTimer, SendClick, Off
KeyWait, RButton
SetTimer, SendClick, 100
return
SendClick:
Click
return
I find the send interval of 50 ms awfully fast, especially since you won't be able to actually reach 50 ms without reducing SetBatchLines and SetKeyDelay. If it really needs to be that fast, consider changing them.

Changing AHK Response, Everytime I move my mouse :(

Here's my sample script
$f2::
loop, 1
while GetKeyState("f2", "P")
{
setkeydelay, 1
send, {f2}
click
}
return
I have 3 delays Normal, Fast, Faster everytime I move my mouse it changes its delay.What should I add in the script to make the response consistent?
ListLines, Off
CoordMode, Mouse
cnt:=1, arr:=[50 ; normal
, 25 ; fast
, 0] ; faster
$F2::
;~ TrayTip,, % "delay is: "arr[cnt]
MouseGetPos, xPos, yPos
xPosPrev:=xPos, yPosPrev:=yPos
While, GetKeyState("F2", "P")
{
MouseGetPos, xPos, yPos
If (xPosPrev!=xPos Or yPosPrev!=yPos)
{
`(cnt=3) ? cnt:=1:cnt++, xPosPrev:=xPos, yPosPrev:=yPos, bool:=False
;~ TrayTip,, % "delay changed to: "arr[cnt]
While, !bool
{
MouseGetPos, xPos, yPos
bool:=(xPosPrev=xPos And yPosPrev=yPos) And A_Index>50 ? True:False
Sleep, GetKeyState("F2", "P") ? 25:-1
xPosPrev:=xPos, yPosPrev:=yPos
}
}
SetKeyDelay, arr[cnt]
Send, {F2}{Click}
Sleep, GetKeyState("F2", "P") ? 25:-1
}
Return