OSG processes keyboard input

Source: Internet
Author: User
Objectives of this chapter:Bytes ¶

Enable the program to associate Keyboard Events with specific functions. In the previous tutorial, we can use the update callback to Control Turret rotation. In this chapter, we will add a keyboard interface class to update the turret corner through the user's keyboard input.

Gui (graphical user interface) event processor: Gui event adapter and GUI action AdapterBytes ¶

The guieventhandler class provides developers with GUI event interfaces for the form system. The event processor uses the guieventadapter instance to receive updates. The event processor can also use the guiactionadapter instance to send requests to the GUI system for some specific operations.

Guieventadapter instances include various event types (push, release, DoubleClick, drag, move, keydown, keyup, frame, resize, scrollup, scrolldown, and scrollleft ). Depending on the guieventadapter event type, its instance may have more related attributes. For example, the X and Y coordinates are related to mouse events. The keyup and keydown events are associated with a key value (for example, "a" and "f1.

Guieventhandler uses the guiactionadapter to request the GUI to execute the action. These actions include requestredraw (), requestcontinuousupdate (), and requestwarppointer (x, y ).

The guieventhandler class interacts with the GUI through the handle method. The handle method has two parameters: A guieventadapter instance is used to receive updates to the GUI, and a guiactionadapter is used to send requests to the GUI. The handle method is used to check the action type and value of guieventadapter, perform the specified action, and send a request to the GUI system using guiactionadapter. If the event has been correctly processed, the Boolean value returned by the handle method is true; otherwise, the value is false.

A gui system may be associated with multiple guieventadapters (the order of guieventadapter is stored in the eventhandlerlist Of The viewport class). Therefore, the return value of this method can be used to control multiple executions of a single keyboard event. If a guieventhandler returns false, the next guieventhandler will continue to respond to the same keyboard event.

The following example demonstrates how guieventhandler interacts with the GUI system: trackballmanipulator class (inherited from guieventhandler) receives mouse event updates as a guieventadapter instance. The parsing of mouse events is completed by the trackballmanipulator class, and the "throw" operation can be implemented (the so-called Throw means that the user presses the key to drag the model and suddenly releases it, to achieve continuous rotation or movement of the model ). When parsing an event, trackballmanipulator sends a request to the GUI system (using guiactionadapter), starts the timer, and repeats the call to calculate the new model direction or location data.

Simple keyboard interfaceBytes ¶

The following describes how to create a keyboard interface class that associates keyboard input with a specific function. After you register the interface class with the buttons and set the corresponding C ++ response function, you can create corresponding table entries. This table is used to save key values ("A", "f1", etc.), key status (press, release), and C ++ response functions. Essentially, you can implement interaction operations that are similar to pressing the f key, that is, performing the functionone operation. Since the new class will inherit from the guieventhandler class, the handle method of these classes will be triggered whenever the GUI system captures a GUI event. After the handle method is triggered, the key value and key status of the GUI event (for example, releasing the key) will be compared with the entries in the table. If a consistent entry is found, then execute the function associated with this key value and status

You can use the addfunction method to register key entries. This function has two forms. The first type uses the key value and response function as the input value. This function is mainly used when users only process key_down events. For example, you can associate the press event of the "A" key with an operation function with the anti-sawtooth effect. However, you cannot use this function to handle the release of keys.

In another case, you may need to differentiate the different actions generated by the "Press" and "release" events of a single button. For example, the attacker action that controls the first-person view. Press W to accelerate the model forward. After the W key is released, the motion model stops gradually. A feasible design method is to design different response functions for pressing and releasing keys. One of the two is used to press the button.

# Ifndef handler # define handler # include <osgga/guieventhandler> class handler: Public osgga: guieventhandler {public: typedef void (* functiontype) (); Enum keystatustype {key_up, key_down}; // The struct used to save the current button status and execute the function. // Write down the status of the current button to avoid repeated calls. // (If you have pressed the button, you do not have to call the corresponding method again) struct functionstatustype {functionstatustype () {keystate = key_up; keyfunction = NULL;} functiontype keyfunction; keystatustype keystate ;}; typedef STD: Map <int, functionstatustype> keyfunctionmap; // This function is used to associate key values and response functions. If the key value has not been registered before, it and its response function will be added to the ing of the "press the button" event and return true .? // Otherwise, no operation is performed and false is returned. Bool addfunction (INT whatkey, functiontype newfunction); // you can specify whether a function is associated with a key_up or key_down event. Bool addfunction (INT whatkey, keystatustype keypressstatus, functiontype newfunction );//? This method compares the status of the currently pressed keys and the list of registered keys/statuses. If the entries match and the event is newer (that is, the key is not pressed), the response function is executed. Virtual bool handle (const osgga: guieventadapter & EA, osgga: guiactionadapter &); // overload function, used to implement GUI event processing accessors. Virtual void accept (osgga: guieventhandlervisitor & V) {v. Visit (* This) ;}; protected: // Save the registered "Press key" method and its key value. Keyfunctionmap keyfuncmap; // Save the registered "release button" method and its key value. Keyfunctionmap keyupfuncmap;}; # endif

Use the keyboard interface class:

The following code demonstrates how to use the class defined above: // creates a scenario and a viewport. //... // Declare the response function: //... // declare and initialize the instance of the keyboard event processor. Keyboardeventhandler * Keh = new keyboardeventhandler (); // Add the event processor to the event processor list in the viewport. // If push_front is used and the handle method of the first item in the list returns true, Other Processors // no longer respond to the same GUI event in the GUI. You can also use push_back to grant the first processing permission of an event to another event processor, or set the return value of the handle method to false. OSG 2.x can also be replaced by the addeventhandler method. Viewer. geteventhandlerlist (). push_front (Keh); // register the key value and respond to the function. // Trigger the toggelsomething function when the key is pressed. // (Release key A does not work) Keh-> addfunction ('A', togglesomething); // trigger startaction function when key J is pressed. (For example, accelerate the speed of the model) // note that you can not add the second parameter. Keh-> addfunction ('J', keyboardeventhandler: key_down, startaction); // triggers the stopaction function when the J key is released. Keh-> addfunction ('J', keyboardeventhandler: key_up, stopaction); // enter the simulation loop //...

Go to the next tutorial basic keyboard input

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.