137. How to have Enter move to next field in form - Part 2?

Problem

This article achieves the same objective as Part 1, that is, using Enter to move to next field. Just want to show you that php-gtk2 gives a lot of flexibility and rooms for creativity that allow us to achieve the same thing in many different ways as shown below:

How to have Enter move to next field in form - Part 2?


Solution

  • First set up the base as outlined in Part 1.
  • Now instead of registering key-press-event on each GtkEntry, we register it on GtkWindow to detect the "Enter" key.
  • If Enter key is pressed, we use GtkWidget::is_focus() to check if the cursor is on any of the GtkEntry field.
  • If it's on one of the GtkEntry field we use GtkWidget::grab_focus() to move the cursor to the next field.
  • For this example, if we reach the last field we move it back to the first field. This in a way "force" the user to click the submit button.

Sample Code

Note: use if ($event->keyval!=65293) if your version of php-gtk2 does not have Gdk::KEY_Return defined.

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
37   
39   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
71   
72   
73   
74   
75   
76   
77   
78   
79   
80   
81   
82   
83   
84   
85   
87   
88   
89   
90   
91   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 240);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Use Enter to Move to Next Field - Part 2\n".
"     using key-press-evnet on GtkWindow");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);

$fields = array('Item number', 'Item Description', 'Unit price', 'Quantity');
$field_size = array(120, 200, 80, 80);
$entry = array(); // to store the text entries

$table = new GtkTable();
display_table($table, $fields, $field_size); // display the table
$vbox->pack_start($table);

function display_table($table, $fields, $field_size) {
    global $entry;
    $row = 0;
    foreach ($fields as $field) {
        $label = new GtkLabel("  $field: ");
        $alignment = new GtkAlignment(1, .5, 0, 0);
        $alignment->add($label);
        $table->attach($alignment, 0, 1, $row, $row+1,
            Gtk::FILL, Gtk::SHRINK, 0, 0);

        $entry[$row] = new GtkEntry();
        $alignment = new GtkAlignment(0, .5, 0, 0);
        $alignment->add($entry[$row]);
        $entry[$row]->set_size_request($field_size[$row], -1);
        $table->attach($alignment, 1, 2, $row, $row+1);
        ++$row;
    }
}

// create a submit button
$button = new GtkButton('Submit');
$button->set_size_request(60, 28);
$button->connect('clicked', 'on_click');
$row = count($fields);
$alignment = new GtkAlignment(0, 0.5, 0, 0);
$alignment->add($button);
$table->attach($alignment, 1, 2, $row, $row+1);

$window->connect('key-press-event', 'on_keypress'); // note 1
$window->show_all();
Gtk::main();

function on_click($button) {
    global $fields, $entry;
    $i=0;
    foreach($fields as $field) {
        echo "$field: ".$entry[$i]->get_text()."\n";
        ++$i;
    }
}

function on_keypress($widget, $event) {
    global $entry, $button;
    if ($event->keyval!=Gdk::KEY_Return) return false; // note 2
    // use if ($event->keyval!=65293) if your version of php-gtk2
    // does not have Gdk::KEY_Return defined

    $found = 0;
    for ($i=0; $i<count($entry); ++$i) {
        if ($entry[$i]->is_focus()) { // note 3
            $found = 1;
            break;
        }
    }
    if ($found) {
        $next_row = $i+1; // note 4
        if ($next_row>=count($entry)) $next_row = 0; // note 5
        $entry[$next_row]->grab_focus(); // note 4
    }
}

?>

Output

As shown above.

 

Explanation

  1. Register the key-press-event on the GtkWindow.
  2. If key press is not Enter, return a false to let the default handler handles it.
  3. check if the cursor is on any of the GtkEntry field.
  4. Move focus to next row.
  5. If it's the last field, move focus back to the first field.

Related Links

Add comment


Security code
Refresh