Re: [gtk-list] gtk_entry and passwords...




Otto Hammersmith writes:
>I'm trying to get a gtk_entry suitable for entering a password (i.e.,
>no echoed text) and I can't seem to make it do just what I want.  Has
>anyone out there managed to get it to work?

I would attach to the gtk_entry's "key_press_event" signal and munge
the event.  Take a look at gtk_file_selection_key_press for an example
of doing this. You could either change the event so that any key typed
would insert a character such as "*", or you can consume the event so
that nothing is displayed in the entry.

I've attached a real simple example of doing the former. Of course, it
doesn't save the password around, so its not perfect, just an
example. 

Peter 

#include <gtk/gtk.h>


static gint
key_press (GtkWidget   *widget,
           GdkEventKey *event,
           gpointer     user_data)
{
  if ((event->keyval >= 0x20) && (event->keyval <= 0xFF))
    event->keyval = '*';
  
  return FALSE;
}

int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *entry;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  
  entry = gtk_entry_new ();
  gtk_signal_connect (GTK_OBJECT (entry), "key_press_event",
		      (GtkSignalFunc) key_press, NULL);
  gtk_container_add (GTK_CONTAINER (window), entry);
  gtk_widget_show (entry);

  gtk_widget_show (window);

  gtk_main ();

  return 0;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]