dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] Inputbox not ready for input after form got focus

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2020
    Posts
    16

    Resolved [RESOLVED] Inputbox not ready for input after form got focus

    i'm using the following call:
    Code:
    Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    running on a timer, to check for an 'F10' keypress. While my app does not have the focus (is in background somewhere,not minimized)

    When the key is pressed, I Show the form and setfocus to it, then ask for userinput.

    But the inputbox is not ready to receive input, and it still needs a manual click to be able to receive input.

    i tried:
    Code:
    Form1.Show
    Form1.SetFocus
    
    'DoEvents 
    
    'Ask for filename
    Filename$ = InputBox("Input Filename: when no name is given+(OK), a date and time will be used", "Save image to file") 'Ask for filename
    and API call:

    Code:
    'Set Form1 Topmost
    Call SetWindowPos(Form1.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
    
    'Ask for filename
    Filename$ = InputBox("Input Filename: when no name is given+(OK), a date and time will be used", "Save image to file") 'Ask for filename
    
    'Check for input, ok or cancel
    If StrPtr(Filename$) = 0 Then 'Checks if cancel was pressed
        'User pressed cancel
        Form1.Label1.Caption = "User canceled filename input."
        Exit Function
    ElseIf Filename$ = "" Then 'User provided no input but clicked 'OK'
        Filename$ = Replace(Now(), ":", "-")
    End If
    
    'Return Form1 NotTopMost
    Call SetWindowPos(Form1.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)

    i was expecting the inputbox to be available immediately, but it somehow isn't. The problem is not there when form1 already has the focus while F10 is pressed.

    What could be the cause, or how can i achieve it to be ready for input without interaction

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,739

    Re: Inputbox not ready for input after form got focus

    Form1 can't show itself immediately, to take the focus, i.e. you have to stop running user code by exiting the sub so that the form can accept the focus.
    After the form is the active window it can open the input dialog which will take the focus from the form.
    It looks like you tried 'DoEvents', which may work on occasion, but perhaps there are several events involved in getting the form to become active, and a single DoEvents doesn't allow for all of them to occur, there may be delays involved between the events necessary to activate a window.

    Perhaps if you have the code in a timer event, and enable the timer, which shows and processes the input dialog after a short delay, which allows the form to become active before you get the tick.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,175

    Re: Inputbox not ready for input after form got focus

    Did you try AppActivate? See vb help.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2020
    Posts
    16

    Re: Inputbox not ready for input after form got focus

    This worked instantly.

    Code:
    AppActivate Form1.Caption
    Thanks for that.

  5. #5
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,175

    Re: Inputbox not ready for input after form got focus

    Good one!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width