Debian Codegraph @

examples/book/events/keyboard_simple/examplewindow.cc

raw
1 /* gtkmm example Copyright (C) 2011 gtkmm development team
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include "examplewindow.h"
18
19 ExampleWindow::ExampleWindow()
20 {
21 set_title("Keyboard Events");
22 m_container.set_margin(10);
23 set_child(m_container);
24
25 // Radio buttons:
26 m_first.set_label("First");
27 m_second.set_label("Second");
28
29 m_second.set_group(m_first);
30 m_first.set_active();
31
32 // Main Container:
33 m_container.set_orientation(Gtk::Orientation::HORIZONTAL);
34 m_container.append(m_first);
35 m_container.append(m_second);
36
37 // Events.
38 auto controller = Gtk::EventControllerKey::create();
39 controller->signal_key_pressed().connect(
40 sigc::mem_fun(*this, &ExampleWindow::on_window_key_pressed), false);
41 add_controller(controller);
42 }
43
44 bool ExampleWindow::on_window_key_pressed(guint keyval, guint, Gdk::ModifierType state)
45 {
46 //Gdk::ModifierType::ALT_MASK -> the 'Alt' key(mask)
47 //GDK_KEY_1 -> the '1' key
48 //GDK_KEY_2 -> the '2' key
49
50 //select the first radio button, when we press alt + 1
51 if((keyval == GDK_KEY_1) &&
52 (state & (Gdk::ModifierType::SHIFT_MASK | Gdk::ModifierType::CONTROL_MASK | Gdk::ModifierType::ALT_MASK)) == Gdk::ModifierType::ALT_MASK)
53 {
54 m_first.set_active();
55 //returning true, cancels the propagation of the event
56 return true;
57 }
58 else if((keyval == GDK_KEY_2) &&
59 (state & (Gdk::ModifierType::SHIFT_MASK | Gdk::ModifierType::CONTROL_MASK | Gdk::ModifierType::ALT_MASK)) == Gdk::ModifierType::ALT_MASK)
60 {
61 //and the second radio button, when we press alt + 2
62 m_second.set_active();
63 return true;
64 }
65 else if(keyval == GDK_KEY_Escape)
66 {
67 //close the window, when the 'esc' key is pressed
68 close();
69 return true;
70 }
71
72 //the event has not been handled
73 return false;
74 }
75
76 ExampleWindow::~ExampleWindow()
77 {
78 }
79