 |
GTK+ Forums Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
|
|
|
| Author |
Message |
|
|
shri_desai09 Familiar Face
Joined: 24 Jul 2006 Posts: 7
|
Posted: Wed Aug 30, 2006 12:14 pm Post subject: Backspace key |
|
|
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. |
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 386 Location: Fairfax, Virginia
|
Posted: Wed Aug 30, 2006 3:14 pm Post subject: |
|
|
If you are using the key-press-event signal callback, you can use the GdkEventKey structure. For example:
| Code: (Plaintext) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 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! |
|
| Back to top |
|
 |
shri_desai09 Familiar Face
Joined: 24 Jul 2006 Posts: 7
|
Posted: Fri Sep 01, 2006 7:06 am Post subject: hi |
|
|
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. |
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 386 Location: Fairfax, Virginia
|
Posted: Fri Sep 01, 2006 1:11 pm Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group CodeBB 1.0 Beta 2
|