Create callback with plugin

Hello Everyone,
I am trying to create a callback in a plugin that I wrote for GLFW, and its purpose is to retrieve keys that have been pressed on the keyboard. How do I make a callback in Xojo for a Plugin callback?

The raw C++ code is:
GLFWSetKeyCallback(GLFWWindow* window, GLFWkeyfun callback)

My example program initializes this call in a button pressed event:

Sub Pressed() Handles Pressed
  Call GLFWSS.glfwSetKeyCallback(Window1.Handle, AddressOf key_callback)
End Sub
Public Sub key_callback(win as Ptr, aKey as Int32, aScancode as Int32, aAction as Int32, aMods as Int32)
  TextArea1.text = TextArea1.text + "Key Pressed"
End Sub

Here is a simple example program. PluginCallback1.xojo_binary_project PluginCallback1

key_callback doesn’t fire, and no text is shown in the TextArea1 control. Does anyone have helpful thoughts on how to get the callback to fire?

OS: Windows 11
Xojo 2022 r3.2

Window1.Handle, is HWND…

Is GLFWWindow* window ok with getting HWND sent into it ?

Other than that then you usually want to do the callbacks as Delegate or as event. (Event is good if you have control), else Delegate.

Though technically your code above can work but only if routine you pass into the callback is in Module and not class instance method. (and of course only if the GLFWWindow is ok with getting HWND into it)

1 Like

Hi Bjorn,

Thank you, you are correct. GLFWWindow was not Ok with getting a HWND sent to it. From your comment, I also had to add a delegate. It is working.

Thank you for your help!

1 Like

Your most welcome !

A shared class method will also work.

1 Like

Thank you @Andrew_Lambert, I will try this option tonight too.