Page 1 of 1

[done] Get Event on ESC key in StringGadget

Posted: Wed Jul 11, 2018 2:21 pm
by Kukulkan
Hello,

I know about AddKeyboardShortcut() but I have issues using it to get ESC key in a given StringGadget.

At first, I'm inside a module and do not know a free menu event number for creation because users of my module might already use the same number. No #PB_Any available here :(

Also, I do not run the event loop (just a module to be used by others) and I use Bind functions to get events. But the StringGadget does not fire such keyboard events and I don't want to hook into the covering window because this will limit the user of my module (if he does the bind events for himself).

Also, it must be cross platform and Windows only tips would not really help.

Any idea?

Re: Get Event on ESC key in StringGadget

Posted: Wed Jul 11, 2018 3:21 pm
by #NULL
I used a hidden dummy window in a module to attach window timers so they are specific to my window and don't interfere with user menu values from other windows. Maybe that works for shortcuts too?

Re: Get Event on ESC key in StringGadget

Posted: Wed Jul 11, 2018 4:18 pm
by Kukulkan
Hi,

no, looks like I can't route window events from the original users window to some dummy window. The events happen in the users window and there the menu events come in.

Re: Get Event on ESC key in StringGadget

Posted: Thu Jul 12, 2018 12:29 am
by kenmo
Yes it's tricky if your module doesn't own the Window or the Gadget.

My two quick suggestions are:

1. Add a helper function for the user's code to define the event ID, then Bind that to #PB_Shortcut_Escape

2. Just choose a "random" event ID 0-64000 and hope the user code never conflicts :)

Re: Get Event on ESC key in StringGadget

Posted: Thu Jul 12, 2018 8:11 am
by Kukulkan
Thanks kenmo

I own my StringGadget but I don't own the Window or the event loop. Thus I work with Bind() events. It is okay so far but I do not get events for keystrokes. I just tried BindMenuEvent() to catch the keystroke menu events but I do not even have a MenuID to give (eg the user may not even have a menu). Thus, I found no solution yet :-(

Guessing a random menu number is very dangerous and may cause random crashes or issues later. I want to prevent that if possible :-)

Re: Get Event on ESC key in StringGadget

Posted: Thu Jul 12, 2018 11:24 am
by #NULL
As for the event loop. I used a simple callback which the user code has to call in its event loop.
In the module:

Code: Select all

  ; called by main program's event loop.
  ; returns #True if the event has been processed and
  ; returns #False if the event has been ignored.
  Procedure.i processEvent()
    Protected eventProcessed
    eventProcessed = #False
    ;If logToWindow And (win And IsWindow(win))
    If win And IsWindow(win)
      If EventWindow() = win
        If Event() = #PB_Event_CloseWindow
          closeLogWindow()
        EndIf
        eventProcessed = #True
      EndIf
    EndIf
    ProcedureReturn eventProcessed
  EndProcedure
In user main:

Code: Select all

        While WaitWindowEvent(10)
          eventProcessed = log::processEvent()
          If Not eventProcessed
            Select Event()
              Case #PB_Event_CloseWindow
                quit = #True
              Case #eventQuit
                quit = #True
            EndSelect
          EndIf
        Wend
So the module and main can each handle their own events. Not sure if that helps with the ESC though.

Re: Get Event on ESC key in StringGadget

Posted: Thu Jul 12, 2018 12:00 pm
by Kukulkan
Hey #NULL,

thanks. i finally did it with a user called event handler which must be put into the main GUI loop. Not as flexible as I wanted but if he does not, the ESC key simply is not used. So it's his decision finally.