1
votes

I would like to know how I can enter keyboard events to control a sphere?I would want a sphere and move it around the screen with keyboard events

1

1 Answers

3
votes

OSG uses event handlers. Just create one like this:

// KeyboardEventHandler.h
#pragma once

#include <osgGA/GUIEventHandler>

class KeyHandler : public osgGA::GUIEventHandler
{
public:
    KeyHandler()
    {}

public:
    virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&)
    {
        switch(ea.getEventType())
        {
            case(osgGA::GUIEventAdapter::KEYDOWN):
            {
                // Place your code here...
            }

            default:
                return false;
        }
    }

    virtual void accept(osgGA::GUIEventHandlerVisitor& v)
    {
        v.visit(*this);
    }
};

Now connect it to your view:

m_osgViewer->addEventHandler(new KeyHandler());

You can pass "this"-pointer to KeyboardHandler and call viewer's methods from inside.