c - 如何在gtk上的按钮信号中添加计时器?

标签 c ubuntu gtk glade

所以我正在设计一个带有空地的虚拟数字键盘。它现在可以工作了,但我想要按钮以 t9 方式打印字母,但我不知道如何计时......请帮忙。到目前为止我的代码是:

 #include <gtk/gtk.h>
       #include <string.h>
       GtkWidget *g_entry1;

       int main(int argc, char *argv[])
       {
           GtkBuilder      *builder; 
           GtkWidget       *window;

           gtk_init(&argc, &argv);

           builder = gtk_builder_new();
           gtk_builder_add_from_file (builder, "labtest.glade", NULL);

           window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
           gtk_builder_connect_signals(builder, NULL);
           g_entry1 = GTK_WIDGET(gtk_builder_get_object(builder,"entry1"));

       g_object_unref(builder);

    gtk_widget_show(window);                
    gtk_main();

    return 0;
}


void on_btn1_clicked()
{
    gchar *num;
    char num_arr[20];
    num = gtk_entry_get_text(GTK_ENTRY(g_entry1));
strcpy(num_arr,num);
    strcat(num_arr,"1");
    gtk_entry_set_text(GTK_ENTRY(g_entry1), num_arr);
}

void on_btn2_clicked()
{
gchar *num;

    char num_arr[20];
    num = gtk_entry_get_text(GTK_ENTRY(g_entry1));
strcpy(num_arr,num);
    strcat(num_arr,"2");
    gtk_entry_set_text(GTK_ENTRY(g_entry1), num_arr);
}

void on_btn3_clicked()
{
gchar *num;

    char num_arr[20];
    num = gtk_entry_get_text(GTK_ENTRY(g_entry1));
strcpy(num_arr,num);
    strcat(num_arr,"3");
    gtk_entry_set_text(GTK_ENTRY(g_entry1), num_arr);
}
void on_btn4_clicked()
{
gchar *num;
    char num_arr[20];
    num = gtk_entry_get_text(GTK_ENTRY(g_entry1));
strcpy(num_arr,num);
    strcat(num_arr,"4");
    gtk_entry_set_text(GTK_ENTRY(g_entry1), num_arr);
}

其余按钮依此类推

最佳答案

(我想将此作为评论,但遗憾的是我没有 50 声誉)

请您澄清一下您的问题。

I wanted the buttons to print letters in a t9 fashion but I can't figure out how to time it.

这到底是什么意思?另外,也许您可​​以提供 labtest.glade 文件的链接

<小时/>

编辑:

好的,所以您可以使用struct timespec,您的button2可能有这样的东西:

const gchar btn2_input[4] = { '2', 'A', 'B', 'C' };
struct timespec last_time_clicked;

#define MAX_DELAY 0.5f

void on_btn2_clicked() {
  struct timespec actual_time;
  float delay;
  const gchar *o_text;
  gchar *n_text;
  GString *s_text;
  static int i = 0;
  gssize text_len;

  clock_gettime(CLOCK_MONOTONIC, &actual_time);
  timespec_subtract(&last_time_clicked, &actual_time, &last_time_clicked);
  delay = timespec_to_float(&last_time_clicked);
  last_time_clicked = actual_time;

  o_text = gtk_entry_get_text(GTK_ENTRY(g_entry1));
  s_text = g_string_new(o_text);
  text_len = s_text->len;

  if(delay > MAX_DELAY)
    i = 0;
  else {
    text_len--;
    i = (i + 1) % 4;
  }

  s_text = g_string_overwrite_len(s_text, text_len, btn2_input + i, 1);

  n_text = g_string_free (s_text, FALSE);
  gtk_entry_set_text(GTK_ENTRY(g_entry1), n_text);
  g_free(n_text);
}

您可能想更改MAX_DELAY,并且有timespec_subtracttimespec_to_float的代码:

void timespec_normalize(struct timespec *t) {
  int n;
  const int sn = 1000000000;

  n = t->tv_nsec / sn;
  t->tv_sec += n;
  t->tv_nsec -= n * sn;

  return;
}

int timespec_subtract(struct timespec *r, struct timespec *x,
              struct timespec *y) {

  timespec_normalize(x);
  timespec_normalize(y);

  if(x->tv_nsec < y->tv_nsec) {
    x->tv_sec--;
    x->tv_nsec += 1000000000;
  }

  r->tv_sec = x->tv_sec - y->tv_sec;
  r->tv_nsec = x->tv_nsec - y->tv_nsec;

  return 0;
}

float timespec_to_float(struct timespec *t) {
  return (float)(t->tv_sec) + (float)(t->tv_nsec) / 1000000000.0f;
}

因此您所需要做的就是检查实际延迟是否大于MAX_DELAY

关于c - 如何在gtk上的按钮信号中添加计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44795397/

相关文章:

python - gtk.Builder() 和多个空地文件中断

c - 这个段错误的原因是什么?

c - 字符串的简单字符输出

php - 尝试运行 PHP 返回 php : symbol lookup error: php: undefined symbol: pcre2_set_depth_limit_8

linux - 关于如何托管公共(public) TensorBoard 以展示 ML 原型(prototype)的建议?

node.js - 允许非 root 用户 process.setuid 给某些其他用户

c - 如何使用 Win32 API 确定打印机的 postscript 支持?

c - 链表中的节点交换

Python GTK 菜单项需要单击两次才能激活

python - 带有 urllib2 和 PyWebKitGtk 的 cookie