Jump to content

AutoFocus on input?


xbtsw
 Share

Recommended Posts

Hi Everyone,

I am trying to add 'AutoFocus' ability to my application. The application only have one window (Form1) and one Input box (Input1). I want it to be like this: If Form1 is in focus, but Input1 is not in focus and user type anything then the the GUI should automatically activate Input1 and let user type there.

The only way that I can think of now is to bind 26 GUIAccelerators from a to z, and write 26 function that activate Input1 and then send key.

I wonder if there's a easier way to do it.

Thanks!

Link to comment
Share on other sites

You can set the edit to have focus with ControlFocus and test if it has focus with ControlGetFocus.

If the edit loses focus you can detect that with a WM_COMMAND handler and test for the EN_KILLFOCUS notification.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

An example of using WM_COMMAND and EN_KILLFOCUS >>

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Hi martin and guinness,

Thanks for the helpful reply.

I know how to test/detect if a control have focus or not. My problem is I don't know how to trigger a precedure (which is set focus to Input1, if Input1 is not on focus) when user enters anything(i.e. pressed a-z or 0-9 or any printable symbol). If user didn't enter anything the focus should stay at where it was.

Or say, I have a function "SetFocus", I want to trigger it whenever user pressed any of the a-z and 0-9 and any other printable symbol, while still, the keyboard should be functional as usual (i.e. those keys should still be able to used to enter charactors).

Edited by xbtsw
Link to comment
Share on other sites

If any key pressed results in that key being directed to the edit then can you explain why it would not be ok to keep the edit focused? For example, if the edit is kept focused, what might the operator do that would cause a problem?

It is probable you need to tell us about what sort of other controls are on the form. If there was another edit thyat i snot read-only then your requirement would be daft of course, but if there are buttons say, then it would be easier to detect a shortcut to a button than keep refocusing the edit for every new key press. If you want the focus to remain on some other control, then how do you deal with Enter or Space which could be part of what someone wants to type into the edit, or could be because they want to interact with the control.

Here is a way that you can do what I think you asked for, though I have only dealt with 'a' to 'z'.

If you click the Yes button to give focus to that button then it will stay in focus as you type letters from a to z but the text will be added to the edit.

#include <GUIConstantsEx.au3>

GUICreate("unfocused writeable edit")

$Ed1 = GUICtrlCreateEdit("",10,10,380,200)
Dim $dummies[26]
Dim $AccelKeys[26][2]
for $n = 0 to 25
    $Dummies[$n] = GUICtrlCreateDummy()
    $AccelKeys[$n][1] = $Dummies[$n]
    $AccelKeys[$n][0] = chr(Asc('a') + $n)
Next

GUISetAccelerators($AccelKeys)

Local $YesID = GUICtrlCreateButton("Yes", 10, 250, 50, 20)
Local $NoID = GUICtrlCreateButton("No", 80, 250, 50, 20)
Local $ExitID = GUICtrlCreateButton("Exit", 150, 250, 50, 20)



GUISetState() ; display the GUI

Do
    Local $msg = GUIGetMsg()

    switch $msg
        Case $Dummies[0] to $Dummies[25]
            GUICtrlSetData($Ed1,chr($msg - $Dummies[0] + Asc('a')),1)
        Case $YesID
            MsgBox(0, "You clicked on", "Yes")
        Case $NoID
            MsgBox(0, "You clicked on", "No")
        Case $ExitID
            MsgBox(0, "You clicked on", "Exit")
        Case $GUI_EVENT_CLOSE
            MsgBox(0, "You clicked on", "Close")
    EndSwitch
Until $msg = $GUI_EVENT_CLOSE Or $msg = $ExitID
Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Hi Martin,

Thanks for your detailed and useful reply.

Yes, the window does have other controls that may need to have focus and receive input. The focus is redirected only in certain condition (depends on the states of other controls). I was trying to simplify the problem in my opening post by ignoring other control. Sorry for the confusion caused by the attempt of that.

Your sample using a for loop and ASCII code to bind accelerators is very helpful :oops: I never think about this way. This saves me from bind and create function for each keys. Thank you so much.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...