Page 1 of 1

[SOLVED] Crash with UnbindGadgetEvent()?

Posted: Tue Oct 29, 2024 4:13 pm
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.

Re: Crash with UnbindGadgetEvent()?

Posted: Wed Oct 30, 2024 3:52 am
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

Re: Crash with UnbindGadgetEvent()?

Posted: Wed Oct 30, 2024 7:06 am
by Joubarbe
Well, thank you again! :D

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Posted: Wed Oct 30, 2024 8:12 am
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

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Posted: Wed Oct 30, 2024 3:35 pm
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

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Posted: Wed Oct 30, 2024 3:48 pm
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.

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Posted: Wed Oct 30, 2024 3:53 pm
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.

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Posted: Wed Oct 30, 2024 4:01 pm
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.

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Posted: Wed Oct 30, 2024 4:57 pm
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.

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Posted: Wed Oct 30, 2024 5:11 pm
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

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Posted: Wed Oct 30, 2024 5:17 pm
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

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Posted: Fri Nov 01, 2024 11:37 am
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

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Posted: Fri Nov 01, 2024 1:20 pm
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).

Re: [SOLVED] Crash with UnbindGadgetEvent()?

Posted: Fri Nov 01, 2024 3:22 pm
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