Accessing the message loop of a Xojo thread


I’m using the RegisterHotKey API call to be notified when a specific shortcut is pressed. Up to now, I was using a dummy window, always hidden, to process the callback, like this:

#if TargetWindows then
  dim p As Ptr=AddressOf WindowProc //WindowProc: a method with these parameters: hWnd As Integer,uMsg As Integer,wParam As Integer,lParam As Integer
  Declare Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongW" _
  (ByVal hWnd as int32,ByVal nIndex as Int32, ByVal dwNewLong as Ptr) as Int32


In the WindowProc method, I check if the message is about the shortcut key (and handle it). If not, it’s passed back to the OS using:

Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Integer, ByVal hWnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lparam As Integer) As Integer
Return CallWindowProc(lpPrevWndProc,hWnd,uMsg,wParam,lparam)

This worked fine, but I’d now like to use the same technique in a console app, so without a valid window’s handle.
The documentation about the RegisterHotKey API says this about the 1st parameter (the hwnd parameter):

If this parameter is NULL , WM_HOTKEY messages are posted to the message queue of the calling thread and must be processed in the message loop.

I presume the “calling thread” is Xojo’s main thread, the one handling the UI. Xojo doesn’t provide the handle of a thread (I’m assuming ThreadID is an internal Xojo property, not the system handle), so I bet I’d lose my time in searching for an API to obtain the thread’s messages.
Is there a way I’m not seeing to monitor the message loop of a Xojo thread?

Did you tried using the thread handler?

1 Like

That is what MBS Plugins do in HotkeyMBS class, so you could try that right away.

1 Like

Nice to know, thanks. Will be a useful reference for other cases (I’ll probably choose the easier way of the MBS plugin for now).

Wondering how I overlooked that…
When I started my original project, it was around 2015, the same year the HotKeyMBS class looks to have been added. I guess I searched close before you added it.

Thank you, I’ll give this a try.

Edit: I’m seeing the issue. It’s desktop-only. Could this be set to work on consoles, too?

I can change the flag for you.

Not sure if that works, bur you can figure out.

Thank you.
If this isn’t going to work, I’ll reconsider making my app as a console; we’ll see.

I’ve sent you a download link.