2

我正在尝试将类似 vim 的功能合并到我在 gtk+ 2.0 和 gtksourceview 2.0 中创建的文本编辑器中。我让它工作,除了当我按“i”进入插入模式时,它会正确进入这个模式,但是它会在文本缓冲区中输入一个“i”。这是我的按键功能,它设置为在按下 esc 键时进入命令模式(离开插入模式),正如我之前所说,当按下 i 键时进入插入模式:

gboolean on_key_press_win_main (GtkWidget *widget, GdkEventKey *event, gpointer user_data)
{

  switch (event->keyval)
  {

    case GDK_i:
      if (event->state & GDK_CONTROL_MASK)
      {
        printf("key pressed: %s\n", "ctrl + i");
      }
      else
      {
        GtkTextBuffer *tbuffer;
                GtkTextView *text_view;
                int page = 0;
        gchar *msg;
        gint row, col;
        GtkTextIter iter;

              page = gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
                text_view = GTK_TEXT_VIEW(txtinput[notebookPages[page]]);
                tbuffer = gtk_text_view_get_buffer (text_view);

        if (insert_mode == 0)
        {
            insert_mode = 1;
            command_mode = 0;

            /* update statusbar */
            gtk_statusbar_pop(GTK_STATUSBAR(statusbar), 0); 

            gtk_text_buffer_get_iter_at_mark(tbuffer, &iter, gtk_text_buffer_get_insert(tbuffer));

            row = gtk_text_iter_get_line(&iter);
            col = gtk_text_iter_get_line_offset(&iter);

            msg = g_strdup_printf("INSERT\t\t Col %d Ln %d", col+1, row+1);
            gtk_statusbar_push(GTK_STATUSBAR(statusbar), 0, msg);

            gtk_text_view_set_editable (text_view, TRUE);

        }

      }

      break;

        /* esc key */
        case 65307:
    {
        GtkTextBuffer *tbuffer;
          GtkTextView *text_view;
          int page = 0;
      gchar *msg;
      gint row, col;
      GtkTextIter iter;

        page = gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
          text_view = GTK_TEXT_VIEW(txtinput[notebookPages[page]]);
          tbuffer = gtk_text_view_get_buffer (text_view);

      insert_mode = 0;
      command_mode = 1;
      gtk_text_view_set_editable (text_view, FALSE);

      /* update statusbar */
      gtk_statusbar_pop(GTK_STATUSBAR(statusbar), 0); 

      gtk_text_buffer_get_iter_at_mark(tbuffer, &iter, gtk_text_buffer_get_insert(tbuffer));

      row = gtk_text_iter_get_line(&iter);
      col = gtk_text_iter_get_line_offset(&iter);

      msg = g_strdup_printf("Col %d Ln %d", col+1, row+1);
      gtk_statusbar_push(GTK_STATUSBAR(statusbar), 0, msg);

    }

    break;

    default:
      return FALSE; 
  }

  return FALSE; 
}

如您所见gtk_text_view_set_editable (text_view, FALSE),进入命令模式时 gtk_text_view_set_editable (text_view, TRUE)设置,进入插入模式时设置。但是,当进入插入模式时, text_view 在按键注册到缓冲区之前被设置为可编辑,即使它是按键情况下的最后一个命令。进入插入模式时,如何防止 i 被放置在文本缓冲区中?

4

2 回答 2

3

Return TRUE from the event handler instead of FALSE to block any further processing of the event. Event handlers work like a filter, you filter out the keystrokes that you don't want to pass on to the textview.

PS. Don't use constants like 65307, use GDK_KEY_Escape or whatever it is.

于 2012-06-06T16:03:04.060 回答
0

你不知道吗,当我写完这个问题时,一个可行的选择就会出现在我的脑海中。这可能就是key_release_event创建的原因。像魅力一样工作。

于 2012-06-06T05:57:34.810 回答