key-press-event

This signal is emitted when a key has been pressed down.

This signal is emitted before any changes to the widget are made. For example, if you are using a GtkEntry, the signal will be emitted before the text of the entry is changed.

Useful event properties are:

  • keyval - integer that can be compared to the key symbols to check which key has been pressed down.
  • state - a bitmask of GdkModifierTypes that can be used to check if Ctrl or Alt have been hold down additional to the pressed key.

Example 155. Using the key-press-event signal

<?php
$wnd = new GtkWindow();
$wnd->connect_simple('destroy', array('Gtk', 'main_quit'));

//we want to receive key press events
$wnd->connect('key-press-event', 'onKeyPress');

//here we handle the key press events
function onKeyPress($widget, $event) {
    if ($event->state & Gdk::CONTROL_MASK && $event->keyval == Gdk::KEY_q) {
        //Press Ctrl+q, and the window will be closed
        $widget->destroy();
    } else if ($event->keyval == Gdk::KEY_F1) {
        //Press F1 to shwo a help text in the title
        $widget->set_title('Press Ctrl+Q to quit');
    }
}

$wnd->show();
Gtk::main();
?>

Callback function

bool callback(GtkWidget widget, GdkEvent event);