GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 30, 2014 11:57 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Backspace key
PostPosted: Wed Aug 30, 2006 12:14 pm 
Offline
Familiar Face

Joined: Mon Jul 24, 2006 12:01 pm
Posts: 7
Hi all,
i have a problem with the backspace key. I have a entry for accepting only numbers and special characters, since i want to input IP address. i am able to restrict the entry only to digits and special characters but i am not able to take the backspace character. I am totally clueless upon this.
waiting for some reply
regards,
Shri.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 3:14 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
If you are using the key-press-event signal callback, you can use the GdkEventKey structure. For example:

Code:
g_signal_connect (G_OBJECT (entry), "key-press-event",
                  G_CALLBACK (key_pressed), NULL);

/* ... */

static gboolean
key_pressed (GtkWidget *entry, GdkEventKey *event, gpointer data)
{
  if (event->keyval == GDK_BackSpace)
     return FALSE;
  else if ((event->keyval >= GDK_0 && event->keyval <= GDK_9) ||
           (event->keyval >= GDK_KP_0 && event->keyval <= GDK_KP_9))
    /* Insert the number (event->string) & then return TRUE */
  else
    return TRUE;
}


In the code above, event->keyval is used to get the key that was entered. These values are available in gdk/gdkkeysyms.h. If it was a backspace (GDK_BackSpace), then handle that & return FALSE so GTK+ will handle the backspace.

If the key was a number of a number on the keypad, you should handle it & then return TRUE so GTK+ will take no further action. In any other case, return TRUE so that all other keys are ignored.

Hope this helps!

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject: hi
PostPosted: Fri Sep 01, 2006 7:06 am 
Offline
Familiar Face

Joined: Mon Jul 24, 2006 12:01 pm
Posts: 7
Hi,
i am using a entry widget and i am using "insert_text" event to connect to the callback function. The body of the callback function is as below: here z is a global int varibale used as a count and IPaddr[] is a global char array which will hold the final string i.e the IP address.

void KeyPressEventHandler (GtkEntry *entry,
const gchar *text,
gint length,
gint *position,
gpointer data)
{
GtkEditable *editable = GTK_EDITABLE(entry);
int i,j, count=0;
int temp;

gchar *result = g_new (gchar, length);

for (i=0; i < length; i++)
{
if (isalpha(text[i]))
continue;
temp = text[i];
result[count++] = islower(text[i]) ? toupper(text[i]) : text[i];
IPaddr[z] = text[i];

if (temp == 8)
{

z=z-2;
}

}
z++;

if (count > 0) {
gtk_signal_handler_block_by_func (GTK_OBJECT (editable),
GTK_SIGNAL_FUNC (KeyPressEventHandler),
data);
gtk_editable_insert_text (editable, result, count, position);
gtk_signal_handler_unblock_by_func (GTK_OBJECT (editable),
GTK_SIGNAL_FUNC (KeyPressEventHandler),
data);
}
gtk_signal_emit_stop_by_name (GTK_OBJECT (editable), "insert_text");


g_free (result);
}


In this callback function im validating the key to be only numbers. here there is a varibale 'temp' in which i have the recent key entered, it works fine for all numbers and even some special characters too, but if i enter backspace that variable holds nothing. The comparision in the code with 8 is to chaeck for the key as backsapce, since 8 is the ASCII value of backspace.

Regards,
shri.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 01, 2006 1:11 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Well, that is because the backspace character does not insert any text. If you want to catch a backspace event, you need to use the backspace signal for that key or delete_from_cursor signal for all types of deletions.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group