PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 43: How to set a button as default action when user press Enter?
Written by kksou   
Monday, 02 October 2006
Problem

You would like to set a button as default action when user press Enter as shown below:

How to set a button as default action when user press Enter?


Solution

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   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 150);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Default Button Action");
$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);

$hbox = new GtkHBox();
$vbox->pack_start($hbox, 0, 0);
$hbox->pack_start(new GtkLabel("Keyword: "), 0, 0);
$hbox->pack_start($entry = new GtkEntry(), 0, 0);
$hbox->pack_start($button = new GtkButton("Search"), 0, 0);

$entry->connect('activate', 'on_enter', $button); // note 1
$button->connect('clicked', 'on_click', $entry); // note 2

function on_enter($entry, $button) { // handle Enter key
    $keyword = $entry->get_text(); // get user input
    echo "Enter pressed. keyword = $keyword\n";
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
Explanation
  1. Set up event handler to listen to Enter key. Note that we pass the ID of the button along. We need the button ID later to simulate button click.
  2. Set up event handler to listen to button click. Note that we pass the ID of the GtkEntry along. We need the GtkEntry ID later to get user input.
  3. We simulate a button click with GtkButton::clicked().

User reviews   Average user ratings:    4.0   (from 7 users)
  1. Luis Martinez Ulloa
    June 28, 2008 2:47pm

  2. Luis Martinez Ulloa
    June 28, 2008 3:23pm
    how can be set a default button globally and not entry by entry setted

  3. XeKtRuM
    July 24, 2008 10:38am
    Great sample

    it also could be both signals handled by the same callback
    ...
    $entry->connect_simple('activate','search',$entry,$button,);
    $button->connect_simple('clicked','search',$entry,$button,0);

    function search($entry, $button, $bool) {
    $keyword = $entry->get_text();
    if($bool){
    echo "Enter pressed. keyword = $keyword\n";
    echo "now goes simulate button click\n";
    $button->clicked();
    }else{
    echo "Button clicked keyword = $keyword\n";
    }
    }
    ...
    or not??

  4. Klaus Frank
    December 03, 2008 1:11pm

    It's easy if you know how to do it. Thank you.

  5. jolemo
    December 09, 2008 10:42am

  6. sorcererayushe
    May 10, 2009 11:21pm
    But Howz Echo work

    Echo is Not Worked :(

  7. Max Hilo
    July 21, 2009 8:11am

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2009. kksou.com. All Rights Reserved