gpt4 book ai didi

events - 将 XKeyEvent 发送到 gtk3 应用程序

转载 作者:行者123 更新时间:2023-12-04 01:35:34 25 4
gpt4 key购买 nike

我使用了以下 documentation在 Vala 中编写一个方法来将按键事件(例如 V)发送到具有焦点的应用程序。

代码如下:

public void press(string accelerator)
{
X.KeyEvent key_event;
if(create_key_event(accelerator, X.EventType.KeyPress, out key_event)) {
X.Event event = (X.Event)key_event;
key_event.display.send_event(key_event.window, false,
X.EventMask.KeyPressMask, ref event);
}
}

private bool create_key_event(string accelerator,
int event_type, out X.KeyEvent key_event)
{
// convert accelerator
uint keysym;
Gdk.ModifierType modifiers;
Gtk.accelerator_parse(accelerator, out keysym, out modifiers);
unowned X.Display display = Gdk.x11_get_default_xdisplay();
key_event = X.KeyEvent();

int keycode = display.keysym_to_keycode(keysym);

if(keycode != 0) {
X.Window root_window = Gdk.x11_get_default_root_xwindow();

// get window with focus
X.Window focus;
int revert_to_return;
display.get_input_focus(out focus, out revert_to_return);

key_event.display = display;
key_event.root = root_window;
key_event.window = focus;
key_event.subwindow = X.None;
key_event.time = X.CURRENT_TIME;
key_event.keycode = keycode;
key_event.state = modifiers;
key_event.type = event_type;
key_event.x = 1;
key_event.y = 1;
key_event.x_root = 1;
key_event.y_root = 1;
return true;
}

return false;
}

这在 gtk2 应用程序上运行良好。然而,gtk3 应用程序似乎完全忽略了此类事件。有没有办法将此类事件也发送到 gtk3 应用程序?

最佳答案

我对此做了更多的调查。似乎当与 XTest 扩展一起发送时,此类事件将被 gtk3 应用程序接收,在声明中也提到 documentation .

这是 vala 中的示例,您需要将其绑定(bind)到 xtst.vapi 才能构建它:

public void press(string accelerator)
{
if(perform_key_event(accelerator, true)) {
debug("Successfully pressed key " + accelerator);
}
}

private bool perform_key_event(string accelerator, bool press)
{
// convert accelerator
uint keysym;
Gdk.ModifierType modifiers;
Gtk.accelerator_parse(accelerator, out keysym, out modifiers);
unowned X.Display display = Gdk.x11_get_default_xdisplay();
int keycode = display.keysym_to_keycode(keysym);

if(keycode != 0) {

if(Gdk.ModifierType.CONTROL_MASK in modifiers) {
int modcode = display.keysym_to_keycode(Gdk.Key.Control_L);
XTest.fake_key_event(display, modcode, press, delay);
}
if(Gdk.ModifierType.SHIFT_MASK in modifiers) {
int modcode = display.keysym_to_keycode(Gdk.Key.Shift_L);
XTest.fake_key_event(display, modcode, press, delay);
}

X.Test.fake_key_event(display, keycode, press, 0);
return true;
}

return false;
}

关于events - 将 XKeyEvent 发送到 gtk3 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7819871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com