1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/event_synthesis_gtk.h"
#include "base/keyboard_code_conversion_gtk.h"
namespace base {
GdkEvent* SynthesizeKeyEvent(GdkWindow* window,
bool press, guint gdk_key, guint state) {
GdkEvent* event = gdk_event_new(press ? GDK_KEY_PRESS : GDK_KEY_RELEASE);
event->key.type = press ? GDK_KEY_PRESS : GDK_KEY_RELEASE;
event->key.window = window;
if (window)
g_object_ref(window);
event->key.send_event = false;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
event->key.time = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
event->key.state = state;
event->key.keyval = gdk_key;
GdkKeymapKey* keys;
gint n_keys;
if (event->key.keyval != 0 &&
gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(),
event->key.keyval, &keys, &n_keys)) {
event->key.hardware_keycode = keys[0].keycode;
event->key.group = keys[0].group;
g_free(keys);
}
return event;
}
void SynthesizeKeyPressEvents(GdkWindow* window,
base::KeyboardCode key,
bool control, bool shift, bool alt,
std::vector<GdkEvent*>* events) {
if (control)
events->push_back(
SynthesizeKeyEvent(window, true, GDK_Control_L, 0));
if (shift) {
events->push_back(SynthesizeKeyEvent(window, true, GDK_Shift_L,
control ? GDK_CONTROL_MASK : 0));
}
if (alt) {
guint state = (control ? GDK_CONTROL_MASK : 0) |
(shift ? GDK_SHIFT_MASK : 0);
events->push_back(
SynthesizeKeyEvent(window, true, GDK_Alt_L, state));
}
// TODO(estade): handle other state flags besides control, shift, alt?
// For example caps lock.
guint state = (control ? GDK_CONTROL_MASK : 0) |
(shift ? GDK_SHIFT_MASK : 0) |
(alt ? GDK_MOD1_MASK : 0);
guint gdk_key = base::GdkKeyCodeForWindowsKeyCode(key, shift);
events->push_back(SynthesizeKeyEvent(window, true, gdk_key, state));
events->push_back(SynthesizeKeyEvent(window, false, gdk_key, state));
if (alt) {
guint state = (control ? GDK_CONTROL_MASK : 0) |
(shift ? GDK_SHIFT_MASK : 0) | GDK_MOD1_MASK;
events->push_back(
SynthesizeKeyEvent(window, false, GDK_Alt_L, state));
}
if (shift) {
events->push_back(
SynthesizeKeyEvent(window, false, GDK_Shift_L,
(control ? GDK_CONTROL_MASK : 0) | GDK_SHIFT_MASK));
}
if (control) {
events->push_back(
SynthesizeKeyEvent(window, false, GDK_Control_L, GDK_CONTROL_MASK));
}
}
} // namespace base
|