Page 1 of 1

SendInput problem with win gadget [SOLVED]

Posted: Wed Sep 24, 2025 6:07 am
by Allen
Hi,

In below code, if I comment out line ";MessageRequester("Wait","Wait Capture screen to complete")" , the progam does not work, I cannot replace it with a delay() command such that no user inout is required. Any suggestion? Without the CreateWin(), the capture screen also works.

Code: Select all

UseJPEGImageDecoder()
UseJPEGImageEncoder()

Procedure CaptureScreen()
  Protected.INPUT InputKey
  ; Print screen Key down
  InputKey\Type = #INPUT_KEYBOARD
  InputKey\ki\wVk =#VK_SNAPSHOT
  InputKey\ki\dwFlags = 0
  SendInput_(1, @InputKey, SizeOf(INPUT))
  Delay(20)
  ;Key Release
  InputKey\Type = #INPUT_KEYBOARD
  InputKey\ki\wVk = #VK_SNAPSHOT
  InputKey\ki\dwFlags =  #KEYEVENTF_KEYUP
  SendInput_(1, @InputKey, SizeOf(INPUT))
  Delay(20)
EndProcedure ;CaptureScreen()

Procedure CreateWin()
  OpenWindow(0, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu)
  ButtonGadget(0, 10, 10, 200, 20, "Standard Button")
  ButtonGadget(1, 10, 40, 200, 20, "Left Button", #PB_Button_Left)
  ButtonGadget(2, 10, 70, 200, 20, "Right Button", #PB_Button_Right)
  ButtonGadget(3, 10,100, 200, 60, "Multiline Button  (longer text gets automatically wrapped)", #PB_Button_MultiLine)
  ButtonGadget(4, 10,170, 200, 20, "Toggle Button", #PB_Button_Toggle)
  ;MessageRequester("Wait","Wait window to complete")
EndProcedure

Global.i ImageID
CreateWin()
ClearClipboard()
CaptureScreen()
;MessageRequester("Wait","Wait Capture screen to complete")
ImageID=GetClipboardImage(#PB_Any)
If ImageID
  MessageRequester("Capture Success",Str(ImageID))
Else
  MessageRequester("Capture Fail",Str(ImageID))
EndIf
Win 11 pro 24H2, PB 6.21.

Thanks

Allen

Re: SendInput problem with win gadget

Posted: Wed Sep 24, 2025 6:54 am
by BarryG
It works fine without MessageRequester(). Do a paste into a paint program after running it, and you'll see. :)

Re: SendInput problem with win gadget

Posted: Wed Sep 24, 2025 8:29 am
by Allen
BarryG,

Yes, you are right, I can paste the image in PAINT after the program ended eventhough the ImageID return is zero. However, in the program, ImageID return zero (Image not initialized) means I cannot use other image command such as SaveImage(ImgeID,..) command to save the image inside the program. I try to use GetClipImage(#FixID) and it also fail to allow me to save the image inside the program.

Do you have any suggestion?

Thanks.

Allen

Re: SendInput problem with win gadget

Posted: Wed Sep 24, 2025 9:32 am
by Allen
Hi,

I would like to explain what I want to do, a popup window with STOP button and a count down timer, if no user inout, at the end of the count down, the screen will be capturedd and image saved .

Code: Select all

UseJPEGImageDecoder()
UseJPEGImageEncoder()
EnableExplicit

Procedure CaptureScreen()
  Protected.INPUT InputKey
  ; Print screen Key down
  InputKey\Type = #INPUT_KEYBOARD
  InputKey\ki\wVk =#VK_SNAPSHOT
  InputKey\ki\dwFlags = 0
  SendInput_(1, @InputKey, SizeOf(INPUT))
  Delay(20)
  ;Key Release
  InputKey\Type = #INPUT_KEYBOARD
  InputKey\ki\wVk = #VK_SNAPSHOT
  InputKey\ki\dwFlags =  #KEYEVENTF_KEYUP
  SendInput_(1, @InputKey, SizeOf(INPUT))
  Delay(20)
EndProcedure ;CaptureScreen()

Procedure CreateWin(CountDown)
  OpenWindow(0, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu)
  StringGadget(0, 10, 10, 200, 20, Str(CountDown))
  ButtonGadget(1, 10, 40, 200, 20, "Stop")
EndProcedure

Procedure Main()
  Protected.i Event,CountDown=3,ImageID
  CreateWin(CountDown)
  AddWindowTimer(0,1,1000) ; Main Win
  Repeat
    Event = WaitWindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        End
      Case #PB_Event_MinimizeWindow
        End
      Case #PB_Event_Timer 
        CountDown-1
        SetGadgetText(0,Str(CountDown))
        If CountDown<=0
          RemoveWindowTimer(0,1)
          ClearClipboard()
          CaptureScreen()
          ;MessageRequester("Wait","Wait Capture screen to complete")
          ImageID=GetClipboardImage(#PB_Any)
          If ImageID
            MessageRequester("Capture Success",Str(ImageID))
            ;SaveImage(ImageID,....)
          Else
            MessageRequester("Capture Fail",Str(ImageID))
            End
          EndIf
        EndIf
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1 ; Button STOP
            MessageRequester("Info","STOP pressed, program ended")
            End
        EndSelect
    EndSelect
  ForEver
EndProcedure

Main()
The program does not work without the "MessageRequester("Wait","Wait Capture screen to complete")".

Any suggestion?

Thanks

Allen

Re: SendInput problem with win gadget (SOLVED)

Posted: Wed Sep 24, 2025 11:36 am
by Allen
Hi,

I found my mistake. Sendinput only send to the window in focus. To change the focus to desktop, I need to hide/minimize the count down window first. Add "HideWindow(0,#True): before CaptureScreen() the program will work.

Allen