[Solved] Need two events to always react

Just starting out? Need help? Post your questions and find answers here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

[Solved] Need two events to always react

Post by Dude »

SOLVED - See next post. :D

I may have asked this before but I couldn't find it. :(

My app has a hotkey event and button event that BOTH need to be triggered at any time, no matter what the user is doing. My event loop currently looks like the below, but as you can see, as soon as something interrupts the loop (the ColorRequester) then the hotkey can't respond anymore.

I tried BindEvent(#WM_HOTKEY,@HotKeyEvent()) but #WM_HOTKEY isn't valid. :(

So what's the logical way to accomplish this goal? Thanks!

Code: Select all

Procedure HotKeyEvent()
  MessageRequester("Dammit","Now you can't click the ButtonGadget! :)")
EndProcedure

hWnd=OpenWindow(0,200,200,220,40,"test",#PB_Window_SystemMenu)
RegisterHotKey_(hWnd,0,#MOD_CONTROL,#VK_BACK) ; Ctrl+Backspace.

ButtonGadget(0,10,10,200,20,"Click me and try to use the hotkey :(")

Repeat
  Event=WaitWindowEvent()
  If Event=#WM_HOTKEY
    HotKeyEvent()
  ElseIf Event=#PB_Event_Gadget
    ColorRequester()
  EndIf
Until Event=#PB_Event_CloseWindow
I also tried this, but if the MessageRequester is open then I can't click the ButtonGadget:

Code: Select all

Procedure HotKeyEvent()
  MessageRequester("Dammit","Now you can't click the ButtonGadget! :)")
EndProcedure

Procedure MyCallback(hWnd,uMsg,WParam,LParam)
  If uMsg=#WM_HOTKEY
    HotKeyEvent()
  EndIf
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

hWnd=OpenWindow(0,200,200,220,40,"test",#PB_Window_SystemMenu)
RegisterHotKey_(hWnd,0,#MOD_CONTROL,#VK_BACK) ; Ctrl+Backspace.

ButtonGadget(0,10,10,200,20,"Click me and try to use the hotkey :(")

SetWindowCallback(@MyCallback())

Repeat
  Event=WaitWindowEvent()
  If Event=#PB_Event_Gadget
    ColorRequester()
  EndIf
Until Event=#PB_Event_CloseWindow
Even doing the HotKeyEvent() as a thread doesn't work:

Code: Select all

Procedure HotKeyEvent(null)
  MessageRequester("Dammit","Now you can't click the ButtonGadget! :)")
EndProcedure

Procedure MyCallback(hWnd,uMsg,WParam,LParam)
  If uMsg=#WM_HOTKEY
    CreateThread(@HotKeyEvent(),0)
  EndIf
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

hWnd=OpenWindow(0,200,200,220,40,"test",#PB_Window_SystemMenu)
RegisterHotKey_(hWnd,0,#MOD_CONTROL,#VK_BACK) ; Ctrl+Backspace.

ButtonGadget(0,10,10,200,20,"Click me and try to use the hotkey :(")

SetWindowCallback(@MyCallback())

Repeat
  Event=WaitWindowEvent()
  If Event=#PB_Event_Gadget
    ColorRequester()
  EndIf
Until Event=#PB_Event_CloseWindow
So I'm fresh out of ideas. :(
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: [Solved] Need two events to always react

Post by Dude »

Turns out PureBasic's MessageRequester() is MODAL, thus preventing my main event loop from reacting. :(

If I replace it with the non-modal MessageBox_() API version, it all works as I want. Woohoo! :D

Code: Select all

Procedure HotKeyEvent(null)
  ;MessageRequester("Dammit","Now you can't click the ButtonGadget! :(")
  MessageBox_(0,"ButtonGadget always works!","Woohoo!",#MB_ICONASTERISK)
EndProcedure

Procedure MyCallback(hWnd,uMsg,WParam,LParam)
  If uMsg=#WM_HOTKEY
    CreateThread(@HotKeyEvent(),0)
  EndIf
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

hWnd=OpenWindow(0,200,200,220,40,"test",#PB_Window_SystemMenu)
RegisterHotKey_(hWnd,0,#MOD_CONTROL,#VK_BACK) ; Ctrl+Backspace.

ButtonGadget(0,10,10,200,20,"Click me and try to use the hotkey :(")

SetWindowCallback(@MyCallback())

Repeat
  Event=WaitWindowEvent()
  If Event=#PB_Event_Gadget
    ColorRequester()
  EndIf
Until Event=#PB_Event_CloseWindow
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [Solved] Need two events to always react

Post by skywalk »

Your code would match PB's MessageRequester() if you passed your window's hWnd to MessageBox_().

Code: Select all

int WINAPI MessageBox(
  _In_opt_ HWND    hWnd,
  _In_opt_ LPCTSTR lpText,
  _In_opt_ LPCTSTR lpCaption,
  _In_     UINT    uType
);
Parameters:
hWnd [in, optional]
Type: HWND
A handle to the owner window of the message box to be created. If this parameter is NULL, the message box has no owner window.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply