How to activate button in a loop - autohotkey

I want to activate the button called EditBUtton2, but I'm finding it difficult. I've only been able to successfully identify it in a loop, just not activate it.
^+f7::
WinGet, OutputVar, ID
MsgBox, "%OutputVar%"
WinGet, ActiveControlList, ControlList, A
Loop, Parse, ActiveControlList, `n
{
if(A_LoopField = "EditButton2")
MsgBox, "found button successfully."
; I want to activate this button, but this doesn't work:
Send, {Space}
;controlsend,, {Space}, %A_Loopfield%
}
return

Related

Why is my call to WinGetTitle returning an empty string?

I'm building a script that will pause my music if it's playing when I lock my workstation. I use spotify, which should be simple to get it's play state by inspecting the window title. When not playing anything, it's title is simply "Spotify", but when it's playing media, the window title changes to the title of the track currently playing. I can see this using the Window Spy.
I've tried finding the spotify window and reading it's title, using WinGetTitle, title, ahk_exe Spotify.exe which should write the title into the var title. This doesn't work, title is an empty string. Intriguingly however it works if the spotify window is minimized.
#L::
{
WinGetTitle, title, ahk_exe Spotify.exe
if(title != "Spotify")
{
Send {Media_Play_Pause}
}
DllCall("LockWorkStation")
return
}
This is on Windows 10. WinGetClass, c, ahk_exe Spotify.exe correctly finds the window, but the class name is Chrome_WidgetWin0 because I'm guessing the app is written in Electron. Other electron apps seem to have the same class name, just incrementing the number at the end.
What I'd love is a way of hooking into whatever windows API spotify is using to report it's current play state, as Windows 10 recognises it as a media application and adds play/pause buttons to it's tab in the taskbar, and in the windows volume control overlay.
Thanks
There are probably more than one windows of class "Chrome_WidgetWin0" belonging to the process "Spotify.exe".
Try this:
#IfWinExist ahk_exe Spotify.exe
#l::
WinGet, id, list, ahk_exe Spotify.exe
Loop, %id%
{
this_ID := id%A_Index%
WinGetTitle, title, ahk_id %this_ID%
If (title = "")
continue
If (title != "Spotify")
{
Send {Media_Play_Pause}
break
}
}
DllCall("LockWorkStation")
return
#IfWinExist
EDIT: To find out whether it really works, run this test:
#IfWinExist ahk_exe Spotify.exe
#q:: ; Win+Q
WinGet, id, list, ahk_exe Spotify.exe
Loop, %id%
{
this_ID := id%A_Index%
WinGetTitle, title, ahk_id %this_ID%
; MsgBox, "%title%"
If (title = "")
continue
MsgBox, "%title%"
If (title != "Spotify")
{
Send {Media_Play_Pause}
break
}
}
return
#IfWinExist

Autohotkey: Ctrl + 1 mapping

I want to remap mouse click to a button depend on a state of Numlock. If Numlock is 'ON' then I want the normal behavior else map to mouse click. The script I created below works fine but I have trouble remapping CTRL+1.
SHIFT+1 will result in '!' so it is simple to do but I don't know what's the behavior for CTRL+1 is. The current script has the dummy "Send, {^1}" but that is not the real behavior.
I appreciate any suggestion.
*The Keywait is for simulating holding the mouse button and the script is enable/disabled base on Numlock state.
$1::
if GetKeyState("NumLock","T")
Send, {1}
else
{
Click down,
KeyWait 1
Click up
}
Return
$+1::
if GetKeyState("NumLock","T")
Send, {!}
else
{
Click down,
KeyWait 1
Click up
}
Return
$^1::
if GetKeyState("NumLock","T")
Send, {^1}
else
{
Click down,
KeyWait 1
Click up
}
Return
The following should do what you've described, with some cleanup to the code itself. I also recommend reading up on the various Send modes, as SendInput is superior in just about every way possible.
$1::
if GetKeyState("NumLock","T")
SendInput, 1
else
{
Click down
KeyWait 1
Click up
}
Return
$+1::
if GetKeyState("NumLock","T")
SendInput, {!}
else
{
Click down
KeyWait 1
Click up
}
Return
$^1::
if GetKeyState("NumLock","T")
SendInput, ^1
else
{
Click down
KeyWait 1
Click up
}
Return

How do I send a singular key press automatically?

Update 05.08.16 - 12:08 AM:
Here is a video of me trying to get this to work: https://youtu.be/h9gAirlUGyI
So, I'm trying to have a key pressed once on program launch.
This is what I have:
{
Send, {F11}
}
Now when I launch the program it doesn't send the key.
Help?
flag = 0
processName := "xenia.exe"
Loop
{
If ProcessExist(processName)
{
WinGetTitle, title, ahk_exe %processName%
IfWinActive, %title%
{
if flag = 0
{
Send, {F11}
flag = 1
}
}
else
{
flag = 0
}
}
Sleep, 1000
}
ProcessExist(Name)
{
Process,Exist,%Name%
return Errorlevel
}
Per your other question and my comment there:
; This code both launches and fullscreens XENIA . . .
#Persistent
#SingleInstance, Force
Run, xenia.exe
Sleep, 500
GoSub, DoIt
return
numpad0::
GoSub, DoIt
return
DoIt:
IfWinExist, ahk_class XeniaWindowClass ahk_exe xenia.exe
{
WinActivate, ahk_class XeniaWindowClass
#IfWinActive, ahk_class XeniaWindowClass
{
Send, {F11}
}
}
return
Now, if the question is REALLY about launching xenia with a file you've clicked on, e.g., using an explorer extension, there are ways of making it work (by adding the script to the registry and/or passing the file name and path into the command line. Again, please give us details as to what you want to happen!

AutoHotKey - Send singular key press on program launch

I'd like to have the F11 key pressed as soon as the program opens without having to press any hotkeys or such, basically automatic key press on launch. Here is my code:
; This section fullscreens XENIA on Play
#SingleInstance, Force
numpad0::
IfWinExist, ahk_class XeniaWindowClass ahk_exe xenia.exe
{
WinActivate, ahk_class XeniaWindowClass
#IfWinActive, ahk_class XeniaWindowClass
{
Send, {F11}
}
}
return
Any help appreicated, thank you!
Oh, I see. Then this is how you do it. You run the function and you can also call it by pressing numpad0 (but, you have to launch this script after running the program, or see my answer on previous post):
; This section fullscreens XENIA on Play
#Persistent
#SingleInstance, Force
GoSub, DoIt
return
numpad0::
GoSub, DoIt
return
DoIt:
IfWinExist, ahk_class XeniaWindowClass ahk_exe xenia.exe
{
WinActivate, ahk_class XeniaWindowClass
#IfWinActive, ahk_class XeniaWindowClass
{
Send, {F11}
}
}
return
Hth, lmk . . .
Maybe like this?
#Persistent
SetTimer, XeniaWatcher
XeniaWatcher() {
WinWaitActive, ahk_class XeniaWindowClass
Send, {F11}
WinWaitNotActive, ahk_class XeniaWindowClass
}
Or this?
#Persistent
SetTimer, XeniaWatcher
XeniaWatcher() {
WinWaitActive, ahk_class XeniaWindowClass
Send, {F11}
WinWaitClose, ahk_class XeniaWindowClass
}
; This section fullscreens XENIA on Play
#SingleInstance, Force
SetInterval, check, 200 ; check every 200ms
sentB4:=0
check:
If ( (WinExist("ahk_class XeniaWindowClass")) && (WinExist("ahk_exe xenia.exe")) ) {
If (sentB4 == 0) {
WinActivate, ahk_class XeniaWindowClass
If ( WinActive("ahk_class XeniaWindowClass") ) {
send, {F11} ; send desired key (fullscreen in this case)
sentB4:=1 ; update status
}
}
} else {
sentB4:=0 ; reset status
}
return
I updated your outdated IfWinExist functions because they were deprecated, read comments for more info
numpad0::
gosub, check
return
Add that for manual triggering as well...

AutoHotKey - Defining variable and printing it out

Defining variable and printing it out
Hey guys!
I just found AHK, and I love it!
I'm currently writing a script, which allows me to select some different options from a dropdown, and then it will automatically enter the value from that dropdown in a specific field:
Now my problem is, that when I example choose "SPIRIT" - for some reason it enters these numbers:
"643232442221" and NOT the numbers it is supposed to: "6432324422"
It's like it's adding extra numbers to the field. Why is that???
#T::
Gui, Add, Text, x26 y177 w420 h30 , Vælg Handling Agent
Gui, Add, DropDownList, x26 y217 w420 h20 vCfs, WFS|Spirit
Gui, Show, x131 y91 h381 w481, New GUI Window
Submit:
Gui, Submit
varCfs = %cfs%
varConsol = %consol%
varMawb = %mawb%
if(varCfs = "WFS"){
varCfs = 6402111562
} else if (varCfs = "SPIRIT"){
varCfs = 6432324422
}
Gui, Hide
;After submit. If the console # is already opened in EDIe, make that window active
;if not, let's generate an error, saying that the user must lookup the console.
IfWinExist, Edit Consol %consol%
{
WinActivate ; use the window found above
;Arrival Info
Click 697,76
Click 651,109
Send {Ctrl Down}a{Ctrl Up}
Sleep 200
Send {Delete}
Sleep 350
Send %varCfs%
+F11::Send, %mawb% ;Shift+F11
} else {
WinActivate, ediEnterprise
MsgBox, 4, Find Consol #, Error!
Gui, Destroy
Gui, show
Return ;Then stop!
}
Return
GuiClose:
ExitApp
I can't reproduce environment to test your code. But I made some corrections that maybe can help you. Try this code:
#T::
Gui, Add, Text, x26 y177 w420 h30 , Vælg Handling Agent
Gui, Add, DropDownList, x26 y217 w420 h20 vCfs, WFS|Spirit
Gui, Show, x131 y91 h381 w481, New GUI Window
Submit:
Gui, Submit
varCfs := cfs
varConsol := consol
varMawb := mawb
if(varCfs = "WFS"){
varCfs := 6402111562
} else if (varCfs = "SPIRIT"){
varCfs := 6432324422
}
Gui, Hide
;After submit. If the console # is already opened in EDIe, make that window active
;if not, let's generate an error, saying that the user must lookup the console.
IfWinExist, Edit Consol %consol%
{
WinActivate ; use the window found above
;Arrival Info
Click 697,76
Click 651,109
Send {Ctrl Down}a{Ctrl Up}
Sleep 200
Send {Delete}
Sleep 350
Send %varCfs%
} else {
WinActivate, ediEnterprise
MsgBox, 4, Find Consol #, Error!
Gui, Destroy
Gui, show
Return ;Then stop!
}
Return
+F11::Send, %mawb% ;Shift+F11
GuiClose:
ExitApp