欢迎光临散文网 会员登陆 & 注册

(五)GTK4 键盘鼠标事件

2023-04-09 12:27 作者:FineClassFuture  | 我要投稿

    GTK的教程就是gtk-demo,说明文档就是API文档https://docs.gtk.org/gtk4

    gtk4-demo中原本应该看例子drawing area,但是这绘制效果......看了下,只是drawing area的事件,没有参考意义。

    gtk4-demo中print例子还是drawing area只能选颜色,但没有任何绘制效果,失败的例子?

    gtk4-demo中有gestures,看了下翻译是手势,还是drawing area。

    

    进入文档主页 https://docs.gtk.org/gtk4/,看到有个“Overview of GTK input and event handling”

    Inside GTK, every such input device is represented by a GdkDevice object.

    使用GdkDevice来表示输入设备

    有这么一张图

Event controllers and gestures

Event controllers are standalone objects that can perform specific actions upon received GdkEvents. These are tied to a widget, and can be told of the event propagation phase at which they will manage the events.

Gestures are a set of specific controllers that are prepared to handle pointer and/or touch events, each gesture implementation attempts to recognize specific actions out the received events, notifying of the state/progress accordingly to let the widget react to those. On multi-touch gestures, every interacting touch sequence will be tracked independently.

Since gestures are “simple” units, it is not uncommon to tie several together to perform higher level actions, grouped gestures handle the same event sequences simultaneously, and those sequences share a same state across all grouped gestures. Some examples of grouping may be:

  • A “drag” and a “swipe” gestures may want grouping. The former will report events as the dragging happens, the latter will tell the swipe X/Y velocities only after recognition has finished.

  • Grouping a “drag” gesture with a “pan” gesture will only effectively allow dragging in the panning orientation, as both gestures share state.

  • If “press” and “long press” are wanted simultaneously, those would need grouping.

Shortcuts are handled by GtkShortcutController, which is a complex event handler that can either activate shortcuts itself, or propagate them to another controller, depending on its scope.


    在github上找到个例子,是个小游戏

    https://github.com/lavalfils/poc_gtk4_2D_action_game

瞬间回到小时候了

    看看如何捕捉键盘事件

// --- add keyboard callback ---

GtkEventController *keyboard = gtk_event_controller_key_new();

gtk_widget_add_controller(window, keyboard);

g_signal_connect(keyboard, "key-pressed", G_CALLBACK(key_pressed), window);

g_signal_connect(keyboard, "key-released", G_CALLBACK(key_released), window);


这样就可以在文档里搜索了

GTK另一个麻烦(烦人)的地方,小版本更改中也会把一些API/参数更改掉



现在还有 鼠标按键事件 怎么获取?

(五)GTK4 键盘鼠标事件的评论 (共 条)

分享到微博请遵守国家法律