Using "Input" Function with "WinActive" Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Using "Input" Function with "WinActive"

Post by SirSocks » 21 Apr 2021, 10:08

Hello,
Request: I would like the ability to type inside a gui, when the gui is the active window.
Issue: When the gui is not active, it still grabs the first character. (see gif below)
Question: How can I type inside the active gui window and resume normal typing when the gui is not active?

Notice how the letter "H" gets stuck in the gui.

Image

Code: Select all

#Persistent
#SingleInstance Force
SingleKey := ""
SetTimer, DisplayLabel, on

Gui,add,text, w300 vMyTextVar,
gui, show, w300,Text_Window
return

DisplayLabel:
If WinActive("Text_Window")
{
Input, SingleKey, L1,
DisplayText .= SingleKey
GuiControl,, MyTextVar, %DisplayText%
}
Return

~Backspace::
If WinActive("Text_Window")
{
Length := StrLen(DisplayText)
Length--
DisplayText := SubStr(DisplayText, 1, Length)
GuiControl,, MyTextVar, %DisplayText%
}
Return

~Esc::
ExitApp
User avatar
mikeyww
Posts: 26951
Joined: 09 Sep 2014, 18:38

Re: Using "Input" Function with "WinActive"

Post by mikeyww » 21 Apr 2021, 10:27

It sounds like you want to activate a window without clicking on it. You can use MouseGetPos to determine which window is under the mouse, and then activate that window.
User avatar
boiler
Posts: 16965
Joined: 21 Dec 2014, 02:44

Re: Using "Input" Function with "WinActive"  Topic is solved

Post by boiler » 21 Apr 2021, 10:58

Try this, which checks to see if the GUI is still active after getting the keystroke, and if not, it simply sends it to whatever window is active instead.

Code: Select all

#Persistent
#SingleInstance Force
SingleKey := ""
SetTimer, DisplayLabel, on

Gui,add,text, w300 vMyTextVar,
gui, show, w300,Text_Window
return

DisplayLabel:
If WinActive("Text_Window")
{
Input, SingleKey, L1,
If !WinActive("Text_Window")
{
	Send, % SingleKey
	return
}
DisplayText .= SingleKey
GuiControl,, MyTextVar, %DisplayText%
}
Return

~Backspace::
If WinActive("Text_Window")
{
Length := StrLen(DisplayText)
Length--
DisplayText := SubStr(DisplayText, 1, Length)
GuiControl,, MyTextVar, %DisplayText%
}
Return

~Esc::
ExitApp
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Re: Using "Input" Function with "WinActive"

Post by SirSocks » 21 Apr 2021, 11:47

@boiler Thank you so much! I truly appreciate your help. :dance:
Post Reply

Return to “Ask for Help (v1)”