Implement the MouseMoved function in a native app?

I have been trying for several hours MouseMoved Function from BView in a native App but I can’t do it. I have do it before in a OpenGl context but in native app that don’t do anything…I want to take the X and Y mouse position in real time and display it. (in terminal or directly in a label)

This is this the prototype:

void BView::MouseMoved ( BPoint, uint32,Const BMessage *)
here is the doc about it. https://www.haiku-os.org/docs/api/classBView.html#ac8b20516e42bab2f1eeb130e2432bde0

Have you the most simple example on the hand?

You need to subclass BView and implement the method:

class MyView : public BView {
     public:
     MyView() : BView("test", 0) {}
     void MouseDown(BPoint where, uint32, const BMessage*) {
          puts("the mouse has moved");
     }
};

Then add this view to a window using AddChild() as usual.

Thank for reply but I don’t find the trouble to solve it…

This is the code I have now but it doesn’t print any caracter about the MouseDown function. Have put “MaVue” to call this class but nothing displayed while moving the mouse.

#include <Application.h>
#include <Window.h>
#include <stdio.h>
#include <View.h>

const int32 HELLO_HAIKU = 'HELO';

//Classe qui crée un BView (personnalisé)
class MyView : public BView
{
     public:
     MyView() : BView(Bounds(),"test",B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW)
     {	
     };
         void MouseDown(BPoint coord, uint32 intoview, const BMessage*msg)
    {
          puts("the mouse has moved");
          printf("The mouse");
     }
};

//Classe de gestion de fenêtre
class HelloWindow : public BWindow
{
    public:
    //Déclaration de fonction "HelloWindo"
    HelloWindow(BRect frame):BWindow(frame, "Hello Window", B_TITLED_WINDOW, 0)
    {
    	BView *view = new BView(Bounds(),"view", B_FOLLOW_ALL_SIDES, B_WILL_DRAW);
    	//On applique une couleur a view (contenu de la fenêtre) couleur par du blanc passe au gris.
	 	view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
    	//Donne le BView =>view en tant qu'enfant dans HelloWindow 
    	AddChild(view);
    	
    	//On crée ensuite la vue personnalisée et on dit que c'est l'enfant de la vue principale
    	MyView *MaVue = new MyView();
    	view->AddChild(MaVue);
    }

	//Gestion de la fermeture
    bool QuitRequested()
    {
    be_app_messenger.SendMessage(B_QUIT_REQUESTED);
    return BWindow::QuitRequested();
    }
    
  
};

//Cette classe DOIT être placée après la précédente car on invoque la classe HelloWindows dedans
//Ceci afin de ne pas provoquer d'erreur de compilation
class HelloHaiku : public BApplication
{
    public:
    HelloHaiku() : BApplication("application/hello-haiku")
    {
    }

	//Fonction de création de la fenêtre et lancement
    void ReadyToRun()
    {
    	//Definit la taille de la fenetre
   		BWindow *win = new HelloWindow(BRect(100, 100, 400, 200));
   		//On envoie la fenêtre vers l'écran
        win->Show();
    }
};



//Programme Principale
int main(void)
{
	//On appelle HelloHaiku (Classe qui crée une application)
	HelloHaiku app;
	//On lance l'application
    app.Run();
    //Après on quitte
    return 0;
}

Two problems I found:

  • You’re defining the MouseDown function, not MouseMoved…? I’m surprised this didn’t cause a compile warning…
void MouseDown(BPoint coord, uint32 intoview, const BMessage*msg)
  • puts() does not put things in the window; that’s DrawString().

Thanks! I don’t saw it Yesterday…But I try it again and it don’t work.
I have try in the OpenGL App using the printf function but that don’t work well (this display the string after closed the app) so I must use the cout function to display string in the Terminal in real time (this is the openGL way I use and it work for OpenGL).

I use this code in OpenGL context and it work (This display strings in Terminal)…But do not on the Nattive app. I don’t know why.:disappointed:

void MouseMoved(BPoint Coord,uint32 intoView,const BMessage * msg2)
	{
	//Teste si la souris bouge dans la fenêtre ou pas.
	if(intoView==1)
	{
		cout<<"You are in the window\n";
		cout<<"X:"<<Coord.x<<"| Y:"<<Coord.y<<"\n";
	}
	else
		cout<<"You are out the Window\n";
	}

Ok I have solve the trouble…This is the full code:

#include <Application.h>
#include <Window.h>
#include <View.h>
#include <iostream>
using namespace std;

const int32 HELLO_HAIKU = 'HELO';

//Classe qui crée un BView (personnalisé)
class MyView : public BView
{
     public:
     MyView(const BRect &frame)
     	:BView(frame,"textview", B_FOLLOW_ALL_SIDES, B_WILL_DRAW){}
     
    void MouseMoved(BPoint Coord,uint32 intoView,const BMessage * msg2)
	{
	//Teste si la souris bouge dans la fenêtre ou pas.
	if(intoView==1)
	{
		cout<<"Vous etes dans la fenetre\n";
		cout<<"X:"<<Coord.x<<"| Y:"<<Coord.y<<"\n";
	}
	else
		cout<<"Vous etes hors de la fenetre\n";
	}
	//void Draw(BRect update);

};

//Classe de gestion de fenêtre
class HelloWindow : public BWindow
{
    public:
    //Déclaration de fonction "HelloWindo"
    HelloWindow(BRect frame):BWindow(frame, "Hello Window", B_TITLED_WINDOW, 0)
    {
    	//ON crée un rectangle de la taille de view
    	BRect rect(Bounds());
    	//On crée ensuite la vue personnalisée et on dit que c'est l'enfant de la vue principale
    	MyView *MaVue = new MyView(rect);
    	AddChild(MaVue);
    }


	//Gestion de la fermeture
    bool QuitRequested()
    {
    be_app_messenger.SendMessage(B_QUIT_REQUESTED);
    return BWindow::QuitRequested();
    }
    
  
};

//Cette classe DOIT être placée après la précédente car on invoque la classe HelloWindows dedans
//Ceci afin de ne pas provoquer d'erreur de compilation
class HelloHaiku : public BApplication
{
    public:
    HelloHaiku() : BApplication("application/hello-haiku")
    {
    }
    
	//Fonction de création de la fenêtre et lancement
    void ReadyToRun()
    {
    	//Definit la taille de la fenetre
   		BWindow *win = new HelloWindow(BRect(100, 100, 400, 200));
   		//On envoie la fenêtre vers l'écran
        win->Show();
    }
};

//Programme Principale
int main(void)
{
	//On appelle HelloHaiku (Classe qui crée une application)
	HelloHaiku app;
	//On lance l'application
app.Run();
//Après on quitte
return 0;
}

Maybe it dont worked because I had not set the size of the View…

I think you just forgot the \n in the printf. By default the terminal is line buffered so it doesn’t print anything until the next newline character.

Thanks! Yes this trick work. I have to also put few %f because MouseMoved return somes float in the BPoint.

It’s good practice to append the keyword override to inherited methods to validate that the argument count/order matches the base class.