AHK Script with ImageSearch - autohotkey

I wrote script that automatically detects play button on the screen and clicks it.
Here it is:
SetTimer Go, 1000
CoordMode Pixel, Screen
CoordMode Mouse, Screen
^!r:: Reload
F4::T4 := !T4
F5::play()
Go:
If (!T4){
return
}
play()
return
play(){
FoundX := 0
FoundY := 0
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, play2.png
If (ErrorLevel = 2){
MsgBox Could not conduct the search.
}
Else{
If (ErrorLevel = 1){
return
}
Else{
x := FoundX + 40
y := FoundY + 40
MouseClick, left, x, y
}
}
}
While in regular window it works fine in fullscreen (fullscreened borderless window) it's behaving weird.
For example sometimes when it sees the button it clicks but then keeps clicking even thou its not on the screen anymore. What's more even if i reload the script it would still keep clicking in that spot. After pressing play a fast-forward button which is fairly similar. Does ImageSearch have some tolerance settings?
The other sorcery is that when i change focus to another window (which is on top but the play button is still visible) it clicks, which change the focus back, and will not click anymore even after the button is back. However if use ALT+TAB to go back to that other window it triggers.
Can anyone explain to me wth is going on here?

Related

Lbutton event seems to disable my dragging

LButton::
MouseClick, Left
if(recodring==true){
mouseGetPos, x, y
xPosPlan[pos] := x
yPosPlan[pos] := y
xPosPlan.push(0)
yPosPlan.push(0)
timePlan.push(0)
pos := pos + 1
return
}
I made a script that records mouse clicks but in this part I dont know why left button dragging got disabled ?
What's happening is that when you press the left button down, the script intercepts that event and presses the left button down and up again. Try replacing
LButton::
MouseClick, Left
by
~LButton::
. This will make the script not intercept that event inthe first place.

while loop not executing correctly when toggled on/off

Goals:
Run ahk script so that a window stays active. When the user clicks off the window it immediately becomes active again.
This is so an overlay (considered its own window) can be used in a game and that if the overlay is clicked on by accident the game window will become the active window again.
I would also like this to be able to be turned on and off during game play so that the user can alt+tab if necessary.
Problems:
I'm testing my code implementation, so far i have it set up to make a blank notepad file become the active window and stay active.
The problem is the toggle (ctrl+alt+J). I can toggle the code it off just fine but when i toggle it on the window doesn't become active again.
Code:
stop := 0
; 0 = off, 1 = on
while (stop = 0)
{
IfWinNotActive, Untitled - Notepad
{
WinActivate, Untitled - Notepad
}
}
return
^!j::
stop := !stop
if (stop = 0){
MsgBox, stop is off.
}
else{
MsgBox, stop is on.
}
return
The reason it doesn't work after you toggle it off is that While only runs until is evaluates to false. Even if what it would evaluate later becomes true again, it won't restart.
Here's what you can do to make your current code work:
stop := 0
; 0 = off, 1 = on
labelWinCheck: ; label for GoSub to restart while-loop
while (stop = 0)
{
IfWinNotActive, Untitled - Notepad
{
WinActivate, Untitled - Notepad
}
Sleep , 250 ; added sleep (250 ms) so CPU isn't running full blast
}
return
^!j::
stop := !stop
if (stop = 0){
MsgBox, stop is off.
} else {
MsgBox, stop is on.
}
GoSub , labelWinCheck ; restarts while-loop
return
There are a couple of different ways that I would look at to achieve your goal.
Easy: use SetTimer instead of While.
stop := 0
SetTimer , labelWinCheck , 250 ; Repeats every 250 ms
labelWinCheck:
If !WinActive( "Untitled - Notepad" )
WinActivate , Untitled - Notepad
Return
^!j::
SetTimer , labelWinCheck , % ( stop := !stop ) ? "off" : "on"
MsgBox , % "stop is " . ( stop ? "on" : "off" )
Return
Advanced: us OnMessage() to monitor WinActivate events. I don't have a working example of this as that would take a bit of research for me, but here is a link for a solution I made to monitor keyboard events, Log multiple Keys with AutoHotkey. The links at the bottom may especially prove useful.

OneNote intercepts pen cursor before AHK?

Palm rejection is garbage on my laptop, so I disabled the HID touchscreen to make it so that only pen input registers. So, I used AHK to make a script that will scroll around the page after the user presses ALT, and disables scrolling upon the second ALT press.
It works great, except that in OneNote (the Win 10 app), pen cursor input is hijacked by OneNote. For example, if I make a ToolTip, %xPos% %yPos%, then it won't update while my pen cursor is hovering anywhere above the OneNote window. Anywhere outside this works fine.
How can I make AHK steal pen cursor input before OneNote can get it?
isTabletMode := 1
penScrollActive := 0
#MaxThreadsPerHotkey 2 ; Allows a second instance to modify penScrollActive while PenScroll is looping.
$Alt::
; Check if PC is in tablet mode.
; 1 --> Tablet, 0 --> Desktop
RegRead, isTabletMode, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\ImmersiveShell,TabletMode
if(isTabletMode) {
if(penScrollActive) {
penScrollActive := 0 ; We are already scrolling, so turn it off.
}
else {
penScrollActive := 1 ; We are not scrolling yet, so turn on pen scrolling.
}
GoSub PenScroll
}
else ; If we aren't in tablet mode, then Alt should just send Alt
Send, {Alt}
return
PenScroll:
loop { ; For some reason, while() logic wasn't working, so this is a workaround. Breaks upon the conditional at bottom.
MouseGetPos, mouseX, mouseY
ToolTip, %mouseX% %mouseY% ; For debugging: Output what cursor pos registers as. (This isn't working in OneNote when using the pen input as a cursor (eg. hover) ).
Sleep, 20
MouseGetPos, mouseX_new, mouseY_new
if (mouseX_new - mouseX > 0) ; Horizontal scrolling
Send, {WheelLeft}
else if (mouseX_new - mouseX < 0)
Send, {WheelRight}
if (mouseY_new - mouseY > 0) ; Vertical scrolling
Send, {WheelUp}
else if (mouseY_new - mouseY < 0)
Send, {WheelDown}
if (penScrollActive = 0) ; Conditional to break out
break
}
return
; To Reload the script: Win+`
#`::Reload
Return
#If penScrollActive
LButton::Return
One thing you could maybe try is my AutoHotInterception library for AHK. You install a custom device driver, then you can hook into input events at the driver level, below the OS.
It supports "Absolute" (Graphics-tablet-like) mouse input
You want the "Subscription Mode" endpoints

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}.

Steam Utility Menu Autohotkey

I have looked thoroughly around the interwebs for this, but I was wondering if anyone had a way that I could remap Shift + Tab so that it brings up a context menu like you see in steam. This would have a transparent background, and no window icon. And just like the steam menu, I want it to have stuff that would be useful. I have tried to do this on my own, but I was not successful. Anyone have any ideas?
I feel like this is much more complicated than you make it out to be.
Here is some code to get the fade effect without a window icon. Use Shift+Tab.
#SingleInstance force
#NoTrayIcon
SetBatchLines, -1
SysGet, VirtualWidth, 78
SysGet, VirtualHeight, 79
Transparency := 0
Fade := 0
Settimer, GUI2AlwaysOnTop, 10 ; Keep gui 2 on top
Gui, 1: Default
Gui, Color, 0x000000 ; Color to black
Gui, +LastFound +AlwaysOnTop -Caption +E0x20 ; Click through GUI always on top.
Gui, 1: +owner
WinSet, Transparent, %Transparency%
Gui, Show, x0 y0 w%VirtualWidth% h%VirtualHeight% ; Cover entire screen, may have to adjust X if you have multiple monitors
Return
Shift & Tab::
If (Fade:=!Fade)
FadeIn(500, 40)
Else
FadeOut(500)
Return
FadeIn(TotalTime = 500, TransFinal = 255)
{
StartTime := A_TickCount
Loop
{
Transparency := Round(((A_TickCount-StartTime)/TotalTime)*TransFinal)
WinSet, Transparent, %Transparency%, ahk_class AutoHotkeyGUI
if (Transparency >= TransFinal)
break
Sleep, 10
}
}
FadeOut(TotalTime = 500)
{
StartTime := A_TickCount
Loop
{
Transparency := ((TimeElapsed := A_TickCount-StartTime) < TotalTime) ? 100*(1-(TimeElapsed/TotalTime)) : 0
WinSet, Transparent, %Transparency%, ahk_class AutoHotkeyGUI
if (Transparency = 0)
break
Sleep, 10
}
}
GUI2AlwaysOnTop:
Gui, 2: +AlwaysonTop
return
A good amount of the GUI code is from SmartBright. I've had these fade functions around, I know I modified someone else's script to my liking, but I cannot find the source.