OpenGL / Gtkmm game - moving the keyboard

I program the game in "OpenGL" and use "Gtkmm" as a window manager. I want to use the keyboard to move the camera (thus, “UP-key” to move forward, “DOWN-key” to move back, etc.)

Some time ago, when I was programming in Java, I used this technique to “move”:
When the application received, for example, a “UP-key- clicksignal , it subsequently set the “shouldMoveForward” flag to “true” and when it later received an “UP-key- release , it will return the flag“ false. "
And the“ game loop "constantly checked this flag, and if it was true, it moves the camera forward, otherwise it did nothing.

So, I would like to use the same technique in "Gtkmm". So I just redefined these functions with my "Gtk :: DrawingArea":

bool Gtk::Widget::on_key_press_event(GdkEventKey* event)
bool Gtk::Widget::on_key_release_event(GdkEventKey* event)

But the problem is this: when I, for example, press the UP key and hold it for 5 seconds, this sequence of signals is emitted:

press  ...<little time waiting>...  release  press  release  press  release  press  release   ..........   press  release  press  release

, " Linux" .

" Windows" , , , :

press  ...<little time waiting>...  press  press  press  press  press  ..........  press  press  release

, " " Gtkmm.

, ("" ) Gtkmm ?

+3
2

( GTK +) .

+2

java-, .

java, :

// Keyboard
private final List<Integer> keysPressed = new LinkedList<Integer>();
private final List<Integer> keysDown = new LinkedList<Integer>();
private final List<Integer> keysRemove = new LinkedList<Integer>();

public final void keyPressed(final KeyEvent e) {
    int key = e.getKeyCode();

    // Fix AutoKeyRepeat under X11
    if (keysRemove.contains(key)) {
        keysRemove.remove(Integer.valueOf(key));
    }

    if (!keysDown.contains(key)) {
        keysDown.add(key);
        keysPressed.add(key);
    }
    e.consume();
}

public final void keyReleased(final KeyEvent e) {
    int key = e.getKeyCode();
    keysRemove.add(key);
    e.consume();
}

public final void clearKeys() {
    for (Integer key : keysRemove) {
        keysDown.remove(Integer.valueOf(key));
        if (keysPressed.contains(key)) {
            keysPressed.remove(Integer.valueOf(key));
        }
    }
    keysRemove.clear();
    keysPressed.clear();
}

clearKeys ( ), , keyPressed. , , - keyReleased keyPressed. , keyRemove keyPressed, - , keyRemove.

, keyPressed, clearKeys.

(, , ) : http://github.com/BonsaiDen/Bonsai-Game-Library/blob/master/src/org/bonsai/dev/GameInput.java

0

Source: https://habr.com/ru/post/1732227/


All Articles