Issue with #if WinActive Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1278
Joined: 06 Aug 2017, 08:21
Location: Germany

Issue with #if WinActive

04 Dec 2017, 19:15

Hi, I used #if WinActive in a code and everything worked perfectly.
This one works perfectly:

Code: Select all

#if WinActive("ahk_class Valve001")
<::Alt
return
The < key gets converted to Alt, only if the specific application is running + in foreground.

I created another code (almost the same logic), but it doesn't work, and I don't know why.

Code: Select all

^+!F11::
#if WinActive("ahk_class IrfanView")
Send {Home}
return
I want that if I press Ctrl+Shift+Alt+F11, the Home key gets pressed, but only when being in IrfanView.
The strange thing is that the Home key gets always sent, no matter where I am...

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
grimboto
Posts: 53
Joined: 09 Jul 2014, 19:20

Re: Issue with #if WinActive

04 Dec 2017, 19:33

Code: Select all

#if WinActive("ahk_class IrfanView") ;This line should be above the hotkey
^+!F11::
Send {Home}
return
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: Issue with #if WinActive

04 Dec 2017, 19:35

Hi Scr1pter, you could try this. The msgbox is just there for testing.

Code: Select all

^+!F11::
if WinActive("ahk_class IrfanView") {
Send {Home}
msgbox Test to see if IrfanView is active
}
return
EDIT: grimboto, I guess we were posting at the same time, almost. Your code is closer to Scr1pter's, of course.

Regards,
burque505
User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Issue with #if WinActive

04 Dec 2017, 20:33

The advantage of using the #if directive instead of the other way is that the key will retain its original function when the window of interest is not active. In burgue505's code, that key will get swallowed up by the hotkey and will do nothing if that window is not active. That's not a big deal in this case, but it's really important if you want to have something like the tab key or space bar or a regular letter key be your hotkey. You want them functioning as normal in other applications.
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: Issue with #if WinActive

04 Dec 2017, 22:04

Good point, boiler.
User avatar
Scr1pter
Posts: 1278
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Issue with #if WinActive

05 Dec 2017, 17:39

Thank you for your support, guys.
It worked!

May I ask another question?
I tried to send this hotkey to IrfanView when the program is not in foreground.
I made a test with Notepad and it worked:

Code: Select all

^+!F11::
ControlSend, Edit1, {Home}, Unbenannt - Editor
(In English it would be Untitled - Notepad)

Even if Notepad was in background, it worked.

I tried the same with IrfanView, but it didn't work:

Code: Select all

^+!F11::
ControlSend, Edit1, {Home}, G9-Arg-Tasten.jpg - IrfanView
Ideally it should be something like *.jpg - IrfanView.

Thanks again for your great support!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Issue with #if WinActive

05 Dec 2017, 19:14

This works for me when IrfanView is the active window, however, it seems that every time you switch the focus from some other window to the IrfanView window, the Edit control loses focus.

Code: Select all

q::
ControlSend, Edit1, {Home}, ahk_class IrfanView
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Scr1pter
Posts: 1278
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Issue with #if WinActive

06 Dec 2017, 17:37

Thanks, but any idea how it would/could work if it's not in foreground?
I use two monitors and IrfanView is maximazed on monitor 2.

Thanks.
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
Scr1pter
Posts: 1278
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Issue with #if WinActive

06 Dec 2017, 18:25

By the way, I tried your code and not even this one works - strange.
Even if IrfanView is on foreground.

I use IrfanView 4.50 64 Bit.
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Issue with #if WinActive

06 Dec 2017, 19:18

- You could use AutoHotkey's window spy to see what the class is.
- What do you want to do with the Edit control, what is the Edit control for?
- I would probably do something like ControlSetText and ControlSend with {Enter} if I wanted to do something with the Edit control.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Scr1pter
Posts: 1278
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Issue with #if WinActive

06 Dec 2017, 19:31

Hi,
I use this code to get the class of a window:

Code: Select all

^+F1::
WinGetClass, class, A
MsgBox, Class of current window is "%class%".
It actually says the class is "IrfanView".
Or do you think this WinSpy will give me "better" information?

To be honest I have no idea about the Edit control!
Basically I just want to switch from the current picture to the first picture of a list (by pressing the home key).
That's all I want.

I made a very small progress, in the Compile tool I chose "Unicode 64 Bit", because I realized my scripts don't work on 64 Bit applications.
But the general IrfanView problem still exists.
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Issue with #if WinActive  Topic is solved

06 Dec 2017, 19:56

Try this:

Code: Select all

q:: ;IrfanView - navigate to first image
ControlSend, ahk_parent, {Home}, ahk_class IrfanView
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Scr1pter
Posts: 1278
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Issue with #if WinActive

06 Dec 2017, 20:03

Hey that works!
Thanks man :)
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: macromint, peter_ahk, Rauvagol, Spawnova, wineguy and 292 guests