• Artigos
  • Projetos
  • Download
  • Lista
  • Docs
  • Comunidade
  • ?

key-press-event com PHP-GTK

23/11/2006  Fernando Henrique Correa  Ler e Comentar

Via muita dificuldade para fazer ações com teclas em minhas aplicações.

Após bater bastante a cabeça cheguei em um resultado.

--PHP-GTK com eventos de teclado--

No primeiro Bloco temos $KEYS_METHODS e $KEYS_MODIFY onde ficam os mapas de teclas e metodos chamados por elas. Repare que concateno teclas para modificadores de ações.

Class GUIPaiWindow com os metodos de CTRL+F1 e CTRL+F2.
Class Gui com metodos de F1 e F2.

Espero que ajude todos.
[]'s
FernandoHcorrea.

<?
$KEYS_METHODS 
= array(
    
'65470' => 'key_f1', 
    
'65471' => 'key_f2',
    
'65507-65470' => 'key_ctrl_f1',
    
'65507-65471' => 'key_ctrl_f2'
);
$KEYS_MODIFY = array(
    
'65507',    //Ctrl 
    
'65513',    //Alt
    
'65505'        //Shift
);

class 
GUIPaiWindow extends GtkWindow{
    
    var 
$keyval;
    var 
$keytime;
    var 
$last_keyval;
    var 
$last_keytime;

    function 
__construct(){
        
parent::__construct();

        
$this->preBuild();
        
    }

    function 
preBuild(){
        
$this->connect("key-press-event",array($this,"on_key"));
    }

    function 
on_key(GtkWidget $widget, GdkEvent $event){
        
        
$this->keyval = $event->keyval;
        
$this->keytime = $event->time;
        
        
$this->validaModificadores($event);        
        
$this->acionaMethod();
                
    }
    
    function 
validaModificadores(GdkEvent $event){
        
$keys_modify = $GLOBALS['KEYS_MODIFY'];
        
        if(
in_array($event->keyval, $keys_modify)){
            
$this->last_keyval = $event->keyval;
            
$this->last_keytime = $event->time;
        }
            
    }
    
    function 
acionaMethod(){
        
        if((
$this->keytime-$this->last_keytime)<400){
            
$idx_method = $this->last_keyval.'-'.$this->keyval;
        }else{
            
$idx_method = $this->keyval;    
        }
        
        
$method = $GLOBALS['KEYS_METHODS'][$idx_method];
        if(
method_exists($this,$method)){                                    
            
$this->{$method}();            
        }
        
    }
    
    function 
key_ctrl_f1(){
        echo 
"Ctrl+F1 ".__class__." Metodo \n";        
    }
    
    function 
key_ctrl_f2(){
        echo 
"Ctrl+F2 ".__class__." Metodo \n";        
    }

    
}


class 
gui extends GUIPaiWindow{
    
    function 
__construct(){
        
parent::__construct();

        
$this->set_title("Fernando H. Correa");        
        
$this->set_default_size(250,100);        
        
$this->show_all();
    }
    
    function 
key_f1(){
        echo 
"F1 ".__class__." Metodo \n";
    }
    
    function 
key_f2(){
        echo 
"F2 ".__class__." Metodo \n";
    }
        
}


new 
gui();
gtk::main();
?>




Comentários

 Adicionar Comentário
 login
 Senha
 Título
 Comentário

Livros


  • Artigos

    • Livro sobre PHP-GTK é notícia
    • Máscara de formatação

    Projetos

    • Instalador PHP-GTK2
    • Agenda Telefonica
    • GenTKode
    • Tulip
  • Google

    Parceiros

 
Designed by Wolfgang Bartelme Designed by Wolfgang Bartelme

© 2006 Wordpress Themes | Theme (Not so) Fresh
XHTML CSS

PHP-GTK Brasil