[SOLVED] Crash with UnbindGadgetEvent()?

Just starting out? Need help? Post your questions and find answers here.
Joubarbe
Enthusiast
Enthusiast
Posts: 714
Joined: Wed Sep 18, 2013 11:54 am
Location: France

[SOLVED] Crash with UnbindGadgetEvent()?

Post by Joubarbe »

Code: Select all

EnableExplicit

#WINDOW_MAIN = 0
#CANVAS_MAIN = 0

Global window_width = 800
Global window_height = 600

Declare BindInputEvents()
Declare UnbindInputEvents()

Procedure OnInput()
  Debug "OnInput()"
EndProcedure

Procedure OnKeyUp()
  Debug "OnKeyUp()"
  Define char = GetGadgetAttribute(#CANVAS_MAIN, #PB_Canvas_Key)
  Select char
    Case #PB_Shortcut_Escape
      End
    Case #PB_Shortcut_Return
      UnbindInputEvents()
  EndSelect
EndProcedure

Procedure OnKeyDown()
  Debug "OnKeyDown()"
EndProcedure

Procedure BindInputEvents()
  BindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

Procedure UnbindInputEvents()
  UnbindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

OpenWindow(#WINDOW_MAIN, 0, 0, window_width, window_height, "")
CanvasGadget(#CANVAS_MAIN, 0, 0, window_width, window_height, #PB_Canvas_Keyboard)
SetActiveGadget(#CANVAS_MAIN)

BindInputEvents()

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Hit return, the CPU goes wild for a few seconds, then the debugger "quits unexpectedly".

My goal here is to stop listening to the keyboard when I'm in a loading process, so that if the user hits return twice, it doesn't confirm the first value that comes after loading is finished.
Last edited by Joubarbe on Wed Oct 30, 2024 7:06 am, edited 1 time in total.
User avatar
JHPJHP
Addict
Addict
Posts: 2267
Joined: Sat Oct 09, 2010 3:47 am

Re: Crash with UnbindGadgetEvent()?

Post by JHPJHP »

Hi Joubarbe,

The problem stems from a timing issue when unbinding key events in a key event.
• Some events cannot be processed because they were previously unblinded.
• Cannot process a KeyUp event when it was already removed on a KeyDown event.

Try the following:

Code: Select all

EnableExplicit

#WINDOW_MAIN = 0
#CANVAS_MAIN = 0

Global window_width = 800
Global window_height = 600

Declare BindInputEvents()
Declare UnbindInputEvents()

Define Event, EventMenu

Procedure OnInput()
  Debug "OnInput()"
EndProcedure

Procedure OnKeyUp()
  Debug "OnKeyUp()"
  Define char = GetGadgetAttribute(#CANVAS_MAIN, #PB_Canvas_Key)
  Select char
    Case #PB_Shortcut_Escape
      End
    Case #PB_Shortcut_Return
      PostEvent(#PB_Event_Menu, #WINDOW_MAIN, 10)
  EndSelect
EndProcedure

Procedure OnKeyDown()
  Debug "OnKeyDown()"
EndProcedure

Procedure BindInputEvents()
  BindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

Procedure UnbindInputEvents()
  UnbindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

OpenWindow(#WINDOW_MAIN, 0, 0, window_width, window_height, "")
CanvasGadget(#CANVAS_MAIN, 0, 0, window_width, window_height, #PB_Canvas_Keyboard)
SetActiveGadget(#CANVAS_MAIN)

BindInputEvents()

Repeat
  Event = WaitWindowEvent()

  Select Event
    Case #PB_Event_Menu
      EventMenu = EventMenu()

      Select EventMenu
        Case 10
          UnbindInputEvents()
      EndSelect
    Case #PB_Event_CloseWindow : Break
  EndSelect
ForEver

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Joubarbe
Enthusiast
Enthusiast
Posts: 714
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Crash with UnbindGadgetEvent()?

Post by Joubarbe »

Well, thank you again! :D
Joubarbe
Enthusiast
Enthusiast
Posts: 714
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Post by Joubarbe »

Actually no I don't understand.

I need to show a "loading" popup during loading times. During that time, there is a callback to call during which I want all inputs to be deactivated. What I do is this:

Code: Select all

  Procedure RequesterLoading(message$, *callback.__Generic, custom_value = #PB_Ignore) : With terminal\requesters
    \message$ = message$
    \callback_loading = *callback
    \custom_value1 = custom_value
    PostEvent(#EVENT_UNBIND_INPUT_EVENTS)
    PostEvent(#EVENT_OPEN_LOADING_REQUESTER)
  EndWith : EndProcedure
Then #EVENT_OPEN_LOADING_REQUESTER opens the popup and posts another event to 1/ call the callback 2/ close the popup 3/ post an event that reactivate all input events.

I don't know if I'm clear, but in my understanding, that should work. It doesn't crash, but I can still spam the Return key and activate things after the loading is finished.

EDIT: Something about like that I guess: (if you press enter during the delay, it will be taken into account, even though it's supposed to be unbound)

Code: Select all

EnableExplicit

#WINDOW_MAIN = 0
#CANVAS_MAIN = 0

Global window_width = 800
Global window_height = 600

Declare BindInputEvents()
Declare UnbindInputEvents()

Define Event, EventMenu

Procedure OnInput()
  Debug "OnInput()"
EndProcedure

Procedure OnKeyUp()
  Debug "OnKeyUp()"
  Define char = GetGadgetAttribute(#CANVAS_MAIN, #PB_Canvas_Key)
  Select char
    Case #PB_Shortcut_Escape
      End
    Case #PB_Shortcut_Return
      PostEvent(#PB_Event_Menu, #WINDOW_MAIN, 10)
  EndSelect
EndProcedure

Procedure OnKeyDown()
  Debug "OnKeyDown()"
EndProcedure

Procedure BindInputEvents()
  BindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

Procedure UnbindInputEvents()
  UnbindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

OpenWindow(#WINDOW_MAIN, 0, 0, window_width, window_height, "")
CanvasGadget(#CANVAS_MAIN, 0, 0, window_width, window_height, #PB_Canvas_Keyboard)
SetActiveGadget(#CANVAS_MAIN)

BindInputEvents()

BindEvent(70000, @BindInputEvents())

Repeat
  Event = WaitWindowEvent()

  Select Event
    Case #PB_Event_Menu
      EventMenu = EventMenu()

      Select EventMenu
        Case 10
          UnbindInputEvents()
          Debug "Loading..."
          Delay(2000)
          PostEvent(70000)
      EndSelect
    Case #PB_Event_CloseWindow : Break
  EndSelect
ForEver
User avatar
ChrisR
Addict
Addict
Posts: 1484
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Post by ChrisR »

I'm not sure I fully understand
You can probably use a Thread to avoid locking the event queue
And for Unbind in @Callback() or in its Callstack, I'd rather use AddKeyboardShortcut(#WINDOW_MAIN, #PB_Shortcut_Return, 10)

Code: Select all

EnableExplicit

CompilerIf Not #PB_Compiler_Thread
  Debug "Enable Thread Safe Mode compiler Option"
  End
CompilerEndIf

#WINDOW_MAIN = 0
#CANVAS_MAIN = 0

Global window_width = 800
Global window_height = 600

Declare BindInputEvents()
Declare UnbindInputEvents()

Define Event, EventMenu

Procedure OnInput()
  Debug "OnInput()"
EndProcedure

Procedure OnKeyUp()
  Debug "OnKeyUp()"
  Define char = GetGadgetAttribute(#CANVAS_MAIN, #PB_Canvas_Key)
  Select char
    Case #PB_Shortcut_Escape
      End
    Default
      Debug Chr(char)
  EndSelect
EndProcedure

Procedure OnKeyDown()
  Debug "OnKeyDown()"
EndProcedure

Procedure BindInputEvents()
  BindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

Procedure UnbindInputEvents()
  UnbindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

Procedure Loading(Delay)
  UnbindInputEvents()
  Debug "Loading in progress... "
  Delay(Delay)
  BindInputEvents()
EndProcedure

OpenWindow(#WINDOW_MAIN, 0, 0, window_width, window_height, "")
CanvasGadget(#CANVAS_MAIN, 0, 0, window_width, window_height, #PB_Canvas_Keyboard)
SetActiveGadget(#CANVAS_MAIN)
AddKeyboardShortcut(#WINDOW_MAIN, #PB_Shortcut_Return, 10)

BindInputEvents()

Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_Menu
      Select EventMenu()
        Case 10
          CreateThread(@Loading(), 2000)
      EndSelect
    Case #PB_Event_CloseWindow : Break
  EndSelect
ForEver
Joubarbe
Enthusiast
Enthusiast
Posts: 714
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Post by Joubarbe »

Thanks ChrisR. However, I would prefer to avoid threads.

I'm actually not sure to understand how events are really handled. I find it very weird that no matter what I try, I just cannot deactivate keyboard input for a period of time (and reactivate it). Somehow, events always go through. What I thought is that if I use PostEvent() after unbinding input events, there's a loop of events that goes without any input; so doing another PostEvent() to reactivate input events would then ignore every key that has been entered while the input events are supposed to be unbound. I want to be able to show a big "Loading" on screen and ignore every input while it is displayed. I've tried pretty much every scenario, but nothing works :( There's clearly something I don't understand. I even tried removing the keyboard focus of the canvas, but events still go through.

Menu 1 ---> Loading ---> Menu 2 ---> Menu 3

Spamming Return in Menu 1 and during Loading shouldn't lead you to Menu 3, but that's what happens right now.
User avatar
skywalk
Addict
Addict
Posts: 4242
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Post by skywalk »

Joubarbe wrote: Wed Oct 30, 2024 3:48 pm I would prefer to avoid threads.
Please don't. Only the simplest of app's can function without threads.
Unless you want users to stare at hourglasses and the like whenever a long process has started?

Just study the many fine examples of threads and gui's on the forum that make use of PostEvent and Semaphores.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
JHPJHP
Addict
Addict
Posts: 2267
Joined: Sat Oct 09, 2010 3:47 am

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Post by JHPJHP »

Hi Joubarbe,

I'm currently working... while writing the following post and fielding calls you received a couple excellent answers, but I decided to post anyways.

My previous post explained why your program was crashing, the following explains why key events are still being processed.
1. Key events are bind to the CanvasGadget.
2. The Enter key initiates a PostEvent to unbind the key events.
3. In the event to unbind the key event you have a two second delay.
4. After the two second delay key events are again bind to the CanvasGadget.

Your two second delay is preventing your key events from being processed (still in the queue). After the two second delay the key events are again bind, and the event loop is released to process the queued events.

There are various ways to accomplish what you're trying to do, here are just a couple.
• Disable the CanvasGadget while the popup is open; disabled gadgets do not accept events.
• Make the popup modal (focus) so that key events do not affect your main window.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Joubarbe
Enthusiast
Enthusiast
Posts: 714
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Post by Joubarbe »

JHPJHP wrote: Wed Oct 30, 2024 4:01 pm • Disable the CanvasGadget while the popup is open; disabled gadgets do not accept events.
I would love this to work, as it seems the most simple solution.

Code: Select all

EnableExplicit

#WINDOW_MAIN = 0
#CANVAS_MAIN = 0

Global window_width = 800
Global window_height = 600

Procedure OnKeyUp()
  Define char = GetGadgetAttribute(#CANVAS_MAIN, #PB_Canvas_Key)
  Debug "OnKeyUp(): " + char
  
  If char = #PB_Shortcut_Return
    SetActiveGadget(-1)
    DisableGadget(#CANVAS_MAIN, #True)
    Debug "Loading started - all input disabled"
    Delay(2000)
    Debug "Loading finished"
    DisableGadget(#CANVAS_MAIN, #False)
    SetActiveGadget(#CANVAS_MAIN)
  EndIf
EndProcedure

OpenWindow(#WINDOW_MAIN, 0, 0, window_width, window_height, "")
CanvasGadget(#CANVAS_MAIN, 0, 0, window_width, window_height, #PB_Canvas_Keyboard)
SetActiveGadget(#CANVAS_MAIN)

BindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
If you hit return during the delay, the event still goes through, even though the gadget is disabled. This is what I don't understand. Sorry for not being clear before, I should have posted a proper example, but I was confused with all my tests.

I must say that my GUI is entirely homemade; I draw everything on one single canvas (ie. different "views" with different controls), therefore I cannot change focus.
User avatar
JHPJHP
Addict
Addict
Posts: 2267
Joined: Sat Oct 09, 2010 3:47 am

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Post by JHPJHP »

Hi Joubarbe,

You're still facing the same problem as before; you're not allowing the events to process (delay) before enabling the gadget.

Back to your original example, try the following:
• The timer allows events to process similar to threads.
• You can also replace the BindEvent Procedures with DisableGadget, just make sure to set the CanvasGadget active in the timer event.

Code: Select all

EnableExplicit

#WINDOW_MAIN = 0
#CANVAS_MAIN = 0

Global window_width = 800
Global window_height = 600

Declare BindInputEvents()
Declare UnbindInputEvents()

Define Event, EventMenu

Procedure OnInput()
  Debug "OnInput()"
EndProcedure

Procedure OnKeyUp()
  Debug "OnKeyUp()"
  Define char = GetGadgetAttribute(#CANVAS_MAIN, #PB_Canvas_Key)
  Select char
    Case #PB_Shortcut_Escape
      End
    Case #PB_Shortcut_Return
      PostEvent(#PB_Event_Menu, #WINDOW_MAIN, 10)
  EndSelect
EndProcedure

Procedure OnKeyDown()
  Debug "OnKeyDown()"
EndProcedure

Procedure BindInputEvents()
  BindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

Procedure UnbindInputEvents()
  UnbindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

OpenWindow(#WINDOW_MAIN, 0, 0, window_width, window_height, "")
CanvasGadget(#CANVAS_MAIN, 0, 0, window_width, window_height, #PB_Canvas_Keyboard)
SetActiveGadget(#CANVAS_MAIN)

BindInputEvents()

Repeat
  Event = WaitWindowEvent()

  Select Event
    Case #PB_Event_Menu
      EventMenu = EventMenu()

      Select EventMenu
        Case 10
          UnbindInputEvents()
          AddWindowTimer(#WINDOW_MAIN, 123, 2500)
          Delay(2000)
      EndSelect
    Case #PB_Event_Timer
      Select EventTimer()
        Case 123
          BindInputEvents()
      EndSelect
    Case #PB_Event_CloseWindow : Break
  EndSelect
ForEver
Last edited by JHPJHP on Wed Oct 30, 2024 5:17 pm, edited 1 time in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Joubarbe
Enthusiast
Enthusiast
Posts: 714
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Post by Joubarbe »

Ok... I think I understand my mistake here. For a reason I cannot explain, I thought the canvas was the emitter of the events. Now I get it... Sorry, it's frustrating to be stupid sometimes :D :D
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Post by breeze4me »

Here's a workaround. (PB 6.12 x64 only, sometimes it also works on x86)
But the program freezing looks like a bug to me. Seeing different execution results for x86 and x64, maybe memory corruption?

Code: Select all

EnableExplicit

#WINDOW_MAIN = 0
#CANVAS_MAIN = 0

Global window_width = 800
Global window_height = 600

Declare BindInputEvents()
Declare UnbindInputEvents()


Procedure OnInput_Empty()
EndProcedure

Procedure OnInput()
  Debug "OnInput()"
EndProcedure

Procedure OnKeyUp()
  Debug "OnKeyUp()"
  Define char = GetGadgetAttribute(#CANVAS_MAIN, #PB_Canvas_Key)
  Select char
    Case #PB_Shortcut_Escape
      End
    Case #PB_Shortcut_Return
      UnbindInputEvents()
  EndSelect
EndProcedure

Procedure OnKeyDown()
  Debug "OnKeyDown()"
EndProcedure

Procedure BindInputEvents()
  BindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  BindGadgetEvent(#CANVAS_MAIN, @OnInput_Empty(), #PB_EventType_Input)
  
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

Procedure UnbindInputEvents()
  UnbindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnInput_Empty(), #PB_EventType_Input)
  
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

OpenWindow(#WINDOW_MAIN, 0, 0, window_width, window_height, "")
CanvasGadget(#CANVAS_MAIN, 0, 0, window_width, window_height, #PB_Canvas_Keyboard)
SetActiveGadget(#CANVAS_MAIN)

BindInputEvents()

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Edit:
One more dummy procedure is required for x86.

Code: Select all

EnableExplicit

#WINDOW_MAIN = 0
#CANVAS_MAIN = 0

Global window_width = 800
Global window_height = 600

Declare BindInputEvents()
Declare UnbindInputEvents()


Procedure OnInput_Empty2()
EndProcedure

Procedure OnInput_Empty()
EndProcedure

Procedure OnInput()
  Debug "OnInput()"
EndProcedure

Procedure OnKeyUp()
  Debug "OnKeyUp()"
  Define char = GetGadgetAttribute(#CANVAS_MAIN, #PB_Canvas_Key)
  Select char
    Case #PB_Shortcut_Escape
      End
    Case #PB_Shortcut_Return
      UnbindInputEvents()
  EndSelect
EndProcedure

Procedure OnKeyDown()
  Debug "OnKeyDown()"
EndProcedure

Procedure BindInputEvents()
  BindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  BindGadgetEvent(#CANVAS_MAIN, @OnInput_Empty(), #PB_EventType_Input)
  BindGadgetEvent(#CANVAS_MAIN, @OnInput_Empty2(), #PB_EventType_Input)
  
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

Procedure UnbindInputEvents()
  UnbindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnInput_Empty(), #PB_EventType_Input)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnInput_Empty2(), #PB_EventType_Input)
  
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

OpenWindow(#WINDOW_MAIN, 0, 0, window_width, window_height, "")
CanvasGadget(#CANVAS_MAIN, 0, 0, window_width, window_height, #PB_Canvas_Keyboard)
SetActiveGadget(#CANVAS_MAIN)

BindInputEvents()

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Edit 2:
The following two will crash.
However, if the bind and unbind order is the same, it will not crash.

Code: Select all

Procedure BindInputEvents()
  BindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  BindGadgetEvent(#CANVAS_MAIN, @OnInput_Empty(), #PB_EventType_Input)
  ; ...
EndProcedure

Procedure UnbindInputEvents()
  UnbindGadgetEvent(#CANVAS_MAIN, @OnInput_Empty(), #PB_EventType_Input)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  ; ...
EndProcedure

Code: Select all

Procedure BindInputEvents()
  BindGadgetEvent(#CANVAS_MAIN, @OnInput_Empty(), #PB_EventType_Input)
  BindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  ; ...
EndProcedure

Procedure UnbindInputEvents()
  UnbindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnInput_Empty(), #PB_EventType_Input)
  ; ...
EndProcedure
Joubarbe
Enthusiast
Enthusiast
Posts: 714
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Post by Joubarbe »

Thank you breeze4me. That is weird indeed... The best would definitely to go with a thread and a signal when the loading process is over (because the whole problem is to get the input back while ignoring what the user typed during the loading process).
User avatar
JHPJHP
Addict
Addict
Posts: 2267
Joined: Sat Oct 09, 2010 3:47 am

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Post by JHPJHP »

Hi Joubarbe,

More Weird Behavior:
• The order of the Unbind command doesn't make a difference.
• Changing the order of the Bind command works for both x86 & x64.

Code: Select all

EnableExplicit

#WINDOW_MAIN = 0
#CANVAS_MAIN = 0

Global window_width = 800
Global window_height = 600

Declare BindInputEvents()
Declare UnbindInputEvents()

Procedure OnInput()
  Debug "OnInput()"
EndProcedure

Procedure OnKeyUp()
  Debug "OnKeyUp()"
  Define char = GetGadgetAttribute(#CANVAS_MAIN, #PB_Canvas_Key)
  Select char
    Case #PB_Shortcut_Escape
      End
    Case #PB_Shortcut_Return
      UnbindInputEvents()
  EndSelect
EndProcedure

Procedure OnKeyDown()
  Debug "OnKeyDown()"
EndProcedure

Procedure BindInputEvents()
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  BindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  BindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

Procedure UnbindInputEvents()
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyUp(), #PB_EventType_KeyUp)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnInput(), #PB_EventType_Input)
  UnbindGadgetEvent(#CANVAS_MAIN, @OnKeyDown(), #PB_EventType_KeyDown)
EndProcedure

OpenWindow(#WINDOW_MAIN, 0, 0, window_width, window_height, "")
CanvasGadget(#CANVAS_MAIN, 0, 0, window_width, window_height, #PB_Canvas_Keyboard)
SetActiveGadget(#CANVAS_MAIN)

BindInputEvents()

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Post Reply