113. How to allow only numbers in GtkEntry - Part 2?

Problem

This example achieves the same objective as How to allow only numbers in GtkEntry - Part 1?, but uses key-press-event instead of emit_stop_by_name() as shown below:

How to allow only numbers in GtkEntry - Part 2?


Solution

In this example, we use key-press-event instead of emit_stop_by_name().

  • Use key-press-event to let us know everytime there is a key-press.
  • If it's a number, we return a false and let the default handler displays the number.
  • If it's not a number, we return a true to stop the keypress from appearing.
Note

This example does not use the new function emit_stop_by_name(). So it will work with your existing version of php-gtk2.


Sample Code

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   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
41   
42   
43   
44   
45   
46   
47   
<?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("Allow only numbers in GtkEntry - Part 2");
$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);

$vbox->pack_start(new GtkLabel("uses key-press-event"), 0, 0);

$item_number = new GtkEntry();
$item_number->connect('key-press-event', 'on_key_press'); // note 1

$hbox = new GtkHBox();
$hbox->pack_start(new GtkLabel('Item Number: '), 0, 0);
$hbox->pack_start($item_number, 0, 0);
$vbox->pack_start($hbox);
$vbox->pack_start(new GtkLabel("Note: item numbers should contain only numbers 0 to 9"), 0, 0);

$window->show_all();
Gtk::main();

function on_key_press($widget, $event) {
    // let the following keys pass through
    switch($event->keyval) {
        case Gdk::KEY_Left: // note 3
        case Gdk::KEY_Right: // note 3
        case Gdk::KEY_BackSpace: // note 3
            return false; // note 3
    }

    if (!preg_match("/[0-9]/", chr($event->keyval))) { // is this a number?
        echo "not number: $event->keyval!\n";
        return true; // note 2
    }

    return false; // yes, let the default handler display the number
}

?>

Output

As shown above.

 

Explanation

  1. Notify us when there is a keypress.
  2. Return a true to stop the keypress from appearing.
  3. If the user presses Left cursor key, Right cursor key or Backspace, let the default handler handles it. Note that this is the key difference between using this method and using the method as described in Part 1 with the use of emit_stop_by_name(). (see notes below)

Note

Difference between this method and that of Part 1
  • In this method, we use the event 'key-press-event', and we have to manually let the arrow keys, BackSpace, etc. pass through (by returning a false), otherwise all these keys will not have any response when the user presses them. Here we have only included three keys (Left, Right and BackSpace). If we want to be complete, we have to also include keys like Home, End, Ctrl-A (Select All), Ctrl-C (Copy), Ctrl-V (Paste), etc.
  • In Part 1, we use the event 'insert-text'. So we do not need to worry about handling of the cursor keys (Left, Right, Home, End) and copy/paste keys (Ctrl-A, Ctrl-C, Ctrl-V).

Related Links

Add comment


Security code
Refresh