Re: detect ctrl+c in key_press_event handler



Ken Resander wrote:
> Odd, you seem to be getting 0 for both the Ctrl key and letter-C key.
> 
> If the keyboard works and you can use it for other applications, then keypresscallback
> really should return non-zero character code values.
> 
> I don't know pygtk, but if there are other keyboard input calls like sscanf in C, then experiment with these and see what you get. 
> 
> If you know C/C++ and GCC you could try the following to inspect the keyvals:   
> 
> //  main.cpp
> #include <gtk/gtk.h>
> #include <gdk/gdkkeysyms.h>
> 
> static gboolean keypresscallback ( GtkWidget * w,
>                                    GdkEventKey *event,
>                                    char * data )
>    {
>    printf ( " key PRESS val=%x state=%x\n" , event->keyval , event->state ) ;
>    return false;
>    }
> 
> int main ( int argc , char * argv[] )
>    {
>    gtk_init(&argc, &argv);
> 
>    GtkWidget * window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
>    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
>    gtk_window_set_default_size(GTK_WINDOW(window), 300 , 200);
>    gtk_window_set_title(GTK_WINDOW(window), "Test Window");
> 
>    gtk_widget_add_events (window, GDK_KEY_PRESS_MASK );
>    g_signal_connect ( window, "key-press-event",
>                       G_CALLBACK (keypresscallback), NULL ) ;
>    gtk_widget_show_all(window);
> 
>    gtk_main();
>    }
> 
> Also, could you use another standard western-european keyboard to see if it behaves differently?

I cannot test myself, I don't have a russian keyboard, but I asked
someone to test, and here is the result:

with latin layout, ctrl+C returns:
 key PRESS val=ffe3 state=0   <-- Ctrl
 key PRESS val=63 state=4     <-- Ctrl+C
and in russian layout:
 key PRESS val=ffe3 state=2000
 key PRESS val=6d3 state=2004

0x6d3 is GDK_Cyrillic_es


I did some tests with a russian user, and here are some results:

print gtk.accelerator_parse("<Control>C") shows:
(99, <flags GDK_CONTROL_MASK of type GdkModifierType>) with both layouts.
Bur when we pass that to gtk.AccelGroup().connect_group(), it works so
GTK is able to understand that 0x6d3 is the same as 0x63. If only I knew
how GTK does!!? Any idea? Some GTK developper to answer this question?


If we put that in the key_pressed handler:
  print
gtk.gdk.keymap_get_default().translate_keyboard_state(event.hardware_keycode,
event.state, event.group)

it prints:
latin layout: (99, 0, 0, <flags GDK_SHIFT_MASK | GDK_LOCK_MASK |
GDK_MOD5_MASK of type GdkModifierType>)
russian layout: (1747, 1, 0, <flags GDK_SHIFT_MASK | GDK_LOCK_MASK |
GDK_MOD5_MASK of type GdkModifierType>)

we found that this works:

  def key_press_event_handler(widget, event):
        keymap = gtk.gdk.keymap_get_default()
	# Get keycode for the pressed key
        keycode, group, level =
keymap.get_entries_for_keyval(event.keyval)[0]
	# Get keycode for the C key
        keycodec, group, level =
keymap.get_entries_for_keyval(gtk.keysyms.c)[0]
        if keycode == keycodec:
                print 'CTRL+C !!'

This works, but I don't think it's a very nice solution. If I understand
correctly in this case we compare which plastic key is pressed, and
which plasic key raises a C, and we compare both.
It will work with letters because I don't think there is a keyboard with
2 keys that will raise a C, but that won't work with numbers I think.

Do you have a better idea?

-- 
Yann


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