Getting keyboard/mouse input (Noob)


20-02-2006 07:42:39

Hello all, I am new to using OgreDotNet.

In Ogre, I'm used to checking the InputReader to see what keys are pressed. How do I do this in OgreDotNet? If anybody could point me to sample code or an example using the mouse and keyboard input would be helpful.

Thanks in advance.


20-02-2006 08:33:15

This is in VB...


Protected WithEvents OEvents As OgreDotNet.EventHandler

Later, during scene generation perhaps:

OEvents = New OgreDotNet.EventHandler(Root, Window)

Root = Root object
Window = Window object

Subscribing events is important, so you can set up the event handling functions:

Protected Function FrameEnded(ByVal e As FrameEvent) As Boolean Handles OEvents.FrameEnded
End Function

Protected Function FrameStarted(ByVal e As FrameEvent) As Boolean Handles OEvents.FrameStarted
End Function

Protected Sub MouseMoved(ByVal e As MouseMotionEvent) Handles OEvents.MouseMoved
End Sub

Protected Sub MouseDragged(ByVal e As MouseMotionEvent) Handles OEvents.MouseDragged
End Sub

Public Sub MousePressed(ByVal e As MouseEvent) Handles OEvents.MousePressed
End Sub

Public Sub MouseReleased(ByVal e As MouseEvent) Handles OEvents.MouseReleased
End Sub

Protected Sub MouseClicked(ByVal e As MouseEvent) Handles OEvents.MouseClicked
End Sub


20-02-2006 18:43:36

Thanks for the help. I'm using C#, and I tried to mimick the code as best as I could, but the mouse cursor goes invisible, and I'm not sure how to see if the ESCAPE key is being pressed. I think I will try to use the C# inputReader, to avoid using CEGUI if at all possible. I'll post back if I have any probs.


20-02-2006 20:03:17

Setting up deligates in C# is very different from setting them up in VB...

VB uses the "handles" keyword at the end of a function or sub declaration. For example:

Public Function MyFunction(ByVal SomeThing as EventArgs) as Boolean

That's a typical function call... add to the end of it what event that function HANDLES...

Public Function MyFunction(ByVal SomeThing as EventArgs) as Boolean Handles SomeObject.SomeRaisedEvent

So in the Object's definition, there's SomeRaisedEvent that it raises...

When declaring that object, you have to let the compiler know to expect events, so you declare it "WithEvents", so your functions that HANDLE those events will work.


Public WithEvents SomeObject

Public Sub MySub() Handles SomeObject.SomeRaisedEvent

Class SomeObject
Public Event SomeObject()
End Class

That's the most basic way of putting it.

For C#, however, you have to set up deligates. I'm referencing some ODN examples here:

You have the function that handles the event, for example from DemoCEGUI:

protected bool QuitClicked( WindowEventArgs e )
Console.WriteLine("Quit Clicked");
return true;

This, you'll note, doesn't have anything special about it that says it handles that specific event... I happen to know it handles the Quit Button's click event, so let's find that...

On line 60, we find the deligate declaration:

mQuitButton.Clicked += new WindowEventDelegate(QuitClicked);

So for mQuitButton, it's adding the WindowEventDeligate, telling it the address of the function "QuitClicked". So now when mQuitButton.Clicked happens, the program goes to that function address and runs that function.

Notice just above that, there's the mQuitButton.SubscribeEvents call... this tells the engine to handle events on that object.

You want to avoid CEGUI... fine...

In ExampleApplication.cs (found in the OgreNet/Custom folder), you'll see:

protected OgreDotNet.EventHandler mEventHandler = null;

There's the primary declaration.

Then we have the initialization of the event handler, including adding all of the deligate function addresses to their appropriate events:

protected virtual void CreateEventHandler()
mEventHandler = new OgreDotNet.EventHandler( mRoot, mRenderWindow );
mEventHandler.FrameStarted += new FrameEventDelegate( FrameStarted );
mEventHandler.FrameEnded += new FrameEventDelegate( FrameEnded );
mEventHandler.KeyClicked += new KeyEventDelegate( KeyClicked );
mEventHandler.KeyPressed += new KeyEventDelegate( KeyPressed );
mEventHandler.KeyReleased += new KeyEventDelegate( KeyReleased );
mEventHandler.MouseMoved += new MouseMotionEventDelegate( MouseMotion );
mEventHandler.MouseClicked += new MouseEventDelegate( MouseClick );
mEventHandler.MousePressed += new MouseEventDelegate( MousePressed );
mEventHandler.MouseReleased += new MouseEventDelegate( MouseReleased );
mEventHandler.MouseDragged += new MouseMotionEventDelegate( MouseDragged );

//InputReader inputreader = mEventHandler.GetInputReader();

Those names in the parens are names of the functions you want called when those specific events take place.

For example: FrameStarted... this event is raised every time a frame starts being rendered.

protected virtual bool FrameStarted( FrameEvent e )

So there ya go!


27-02-2006 09:42:40

Sorry for the late post, I've been really busy the past week. Thanks a lot EagleEye, I appreciate your help. I should be able to figure things out now.

Once again, thanks!