0
votes

I have a code snipped on .ahk

#k::
  if WinExist("a") {
    Sendinput test
    }
return

This code should send "test" only if window with name "a" is opened, as I read from docs. But it's not working -(. It's sending string "test" all the time. I need code snippet that will do something; e.g. send string or something else, only if specific window is opened and do nothing is that window is closed. Can you please tell me what I'm doing wrong.

1
The expression evaluates to true, if there is an active window (see WinTitle & Last Found Window). You will hardly ever find a system, where there is no active window at any given time. If you need to identify a specific window, follow the documentation. - IInspectable
Well what is command that match the exact name of the open window. I have been trying to find one but doc. are so none intuitive. - IGRACH

1 Answers

2
votes

Your code does work properly for me.

However, unless you've used "SetTitleMatchMode", your condition will be true if you have any window title that starts with "a".

"DetectHiddenText" may also be affecting the return value of WinExist.

Troubleshoot your problem by starting with a more unique title, like this

#k::
  if WinExist("gob.txt - Notepad") {
    Sendinput test
    }
return

Test that this code works as expected when you do have "gob.txt" open in notepad. Then test when you don't have the file open. Experiment with different settings for SetTitleMatchMode. Hopefully, you will eventually be able to isolate where the problem is. I can't do it because I don't know what windows you have open on your system.

It also helps to clear out other code that could be affecting your test by right-clicking on the AutoHotKey icon on the task bar and choosing "Edit this script" and including only the code you are testing.

Good luck