GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Backspace key

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
shri_desai09
Familiar Face


Joined: 24 Jul 2006
Posts: 7

PostPosted: Wed Aug 30, 2006 12:14 pm    Post subject: Backspace key Reply with quote

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: 376
Location: State College, Pennsylvania

PostPosted: Wed Aug 30, 2006 3:14 pm    Post subject: Reply with quote

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

PostPosted: Fri Sep 01, 2006 7:06 am    Post subject: hi Reply with quote

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: 376
Location: State College, Pennsylvania

PostPosted: Fri Sep 01, 2006 1:11 pm    Post subject: Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP