B3DSDK mouse input using C#.

Archives Forums/Blitz3D SDK Tutorials/B3DSDK mouse input using C#.

monotonic(Posted 2008) [#1]
I have had some trouble trying to get mouse input using C# and the SDK when rendering to a panel or picture-box etc. I have seen some posts which suggest placing another panel over the render target and wiring up events to this, I thought this was a little bit hacky, good idea but hacky. So I decided to try and find another way.

The method I chose to go with is the SetBlitz3DEventCallback callback, it is pretty simple to setup and it doesn't have the nice layout of events in C# but it works.


(using System.Runtime.InteropServices)

Callback function declaration:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int dBBEventHandler(int hWnd, int Msg, int wParam, int lParam);
private dBBEventHandler BBEventHandler; 

This declares a function pointer which we will hook up to recieve events from the SDK.


Hook-up the callback procedure (This must be done before calling bb.BeginBlitz3D.):
BBEventHandler = new dBBEventHandler(Engine_BBEventHandler);
bb.SetBlitz3DEventCallback(Marshal.GetFunctionPointerForDelegate(BBEventHandler).ToInt32());



The event handler body:
public int Engine_BBEventHandler(int hWnd, int Msg, int wParam, int lParam)
{
    // process the event
    switch (Msg)
    {
         case WM_LBUTTONDOWN:
             // left mouse button is down
             MessageBox.Show("Left mouse down");
             break;

         case WM_RBUTTONDOWN:
             // right mouse button is down
             MessageBox.Show("Right mouse down");
             break;
    }

    return -1;

}



Here is some constants that are used to determine what message is being fired, to get a complete list just look into the Win32 API.

// Windows messages
const int WM_LBUTTONDOWN = 0x201;
const int WM_LBUTTONUP = 0x202;
const int WM_LBUTTONDBLCLK = 0x203;
const int WM_RBUTTONDOWN = 0x204;
const int WM_RBUTTONUP = 0x205;
const int WM_RBUTTONDBLCLK = 0x206;
const int WM_MBUTTONDOWN = 0x207;
const int WM_MBUTTONUP = 0x208;
const int WM_MBUTTONDBLCLK = 0x209;



This is pretty basic stuff but I thought it might come in handy to anybody who is not familiar with callbacks or C#.


Rob Pearmain(Posted 2008) [#2]
Awesome, this was a problem I was getting.

I guess it is the same for Keyhit?


monotonic(Posted 2008) [#3]
There is a bug in the SDK whereby, if you call SetBlitz3DHWND and assign a control to render in, the callback doesn't fire events for keyboard input. But, if you don't call the above function you get keyboard input.

A real pain in the ....