This project is read-only.

6. Mouse & Keyboard Input - Tutorial

Vortex2D input system allows you read mouse and keyboard state asynchroniously or subscribe on input events: MouseDown, MouseUp, KeyDown, KeyUp etc. It is required to create interactive applications. Keyboard and mouse input represented with Game.Keyboard and Game.Mouse objects which are available from any point of application since it is started.

Example from this tutorial implements very basic balloon shooting game mechanics based on mouse event handling.

mouseAndKeyboardInput.jpg

Mouse Asynchronous Input

All of mouse states are fixed and available through Game.Mouse object. Here you can read X, Y/Position coordinates or of mouse cursor inside window's client area. LeftDown, RightDown, MiddleDown properties return true if currently corresponding mouse button is down. LeftPressed, RightPressed, MiddlePressed return true if mouse button was pressed this frame. LeftReleased, RightReleased, MiddleReleased reports that button was released this frame. Property InWindow returns true if mouse cursor is currently under client part of game window otherwise - false.

CursorType property of Game.Mouse object allows to set/get mouse cursor shape. All of supported cursor shapes can be found in Vortex.Input.CursorType.

In tutorial code you can find one place with asynchronous reading of mouse state, This code is equal to synchronous event handling which will be described later

//will be true during whole frame after mouse left button was pressed down
if (Mouse.LeftPressed) {
    BlowBalloonsAround(Mouse.Position);
}

Mouse Synchronous Input - Events

To provide high responsiveness (possible few events per frame) it will be better to create mouse events. To handle that event you should prepare handler method with specified signature (int, int, Vortex.Input.MidificationKeys)

private void OnMouseDown(int x, int y, ModificationKeys keys) {
    BlowBalloonsAround( new Vector(x, y) );
}

After you can register event to be processed in Load() method:

Game.Mouse.OnLeftButtonDown += new WindowsMouseEventHandler(this.OnMouseDown);

List of all mouse events you can subscribe on: OnMove, OnLeftButtonDown, OnRightButtonDown, OnRightButtonUp. Note, you can use the same handler for few kind of events and differentiate them with content of ModificationKeys flags.

Keyboard Asynchronous Input

To get state of some key (all keys for async input are in Vortex.Input.Key enum) you need to call Keyboard.IsDown(Key) method. Argument - key which state you want to know. If you want to get which key was pressed this frame use Keyboard.IsPressed(Key). Released - Keyboard.IsReleased(Key). Just changed its state - Keyboard.IsToggled(Key). Another useful method - Keyboard.IsPressedCombination(Key modKey, Key primaryKey). It returns true if primaryKey was pressed with modKey being down.

In tutorial you can find a bit of sample code which toggles Game debug information output on pressing F2 key.

if (Keyboard.IsPressed(Key.F2)) {
    //Tthis property enables showing FPS or debug info accessible with F12
    DebugInfoEnabled = !DebugInfoEnabled;
}

Keyboard Synchronous Input - Events

Similar to mouse sync input Keyboard has few events which can used for reaction on keyboard changes. There are only three events: OnKeyDown, OnKeyUp, OnChar. First both send value of VirtualKey type (analog of windows virtual key codes). OnChar sends char symbol which is decoded using keyboard layout / language settings etc.

Sample of using sync keyboard events:

// adding event
Game.Keyboard.OnKeyDown += new WindowsKeyboardEventHandler(this.OnKeyDown);

// handler for keyboard event
private void OnKeyDown (VirtualKey vKey) {
    if (vKey == VirtualKey.V) {
        //Toggling vertical synchronization, device will be reset between frames
        Window.VerticalSync = !Window.VerticalSync;
    }
}

Conclusion

Vortex2D has both synchronous and asynchronous input model which can be used for most of cases. Note, scene graph subsystem dispatches synchronous events and sends them to nodes.

Last edited Aug 18, 2010 at 10:07 PM by AlexKhomich, version 8

Comments

No comments yet.