]>
Commit | Line | Data |
---|---|---|
7de7aed4 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: keyboard.cpp | |
be5a51fb | 3 | // Purpose: Keyboard wxWidgets sample |
7de7aed4 | 4 | // Author: Vadim Zeitlin |
a96bab36 | 5 | // Modified by: Marcin Wojdyr |
7de7aed4 VZ |
6 | // Created: 07.04.02 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2002 Vadim Zeitlin | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
7de7aed4 VZ |
12 | #include "wx/wxprec.h" |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
7de7aed4 VZ |
17 | #ifndef WX_PRECOMP |
18 | #include "wx/wx.h" | |
19 | #endif | |
20 | ||
41f02b9a FM |
21 | #ifndef __WXMSW__ |
22 | #include "../sample.xpm" | |
23 | #endif | |
7de7aed4 VZ |
24 | |
25 | // Define a new frame type: this is going to be our main frame | |
26 | class MyFrame : public wxFrame | |
27 | { | |
28 | public: | |
a96bab36 | 29 | MyFrame(const wxString& title); |
7de7aed4 VZ |
30 | |
31 | private: | |
a96bab36 VZ |
32 | // event handlers |
33 | void OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(true); } | |
34 | void OnAbout(wxCommandEvent& event); | |
a30305f8 VZ |
35 | |
36 | void OnTestAccelA(wxCommandEvent& WXUNUSED(event)) | |
37 | { m_logText->AppendText("Test accelerator \"A\" used.\n"); } | |
38 | void OnTestAccelCtrlA(wxCommandEvent& WXUNUSED(event)) | |
39 | { m_logText->AppendText("Test accelerator \"Ctrl-A\" used.\n"); } | |
40 | void OnTestAccelEsc(wxCommandEvent& WXUNUSED(event)) | |
41 | { m_logText->AppendText("Test accelerator \"Esc\" used.\n"); } | |
42 | ||
a96bab36 VZ |
43 | void OnClear(wxCommandEvent& WXUNUSED(event)) { m_logText->Clear(); } |
44 | void OnSkip(wxCommandEvent& event) { m_skip = event.IsChecked(); } | |
a30305f8 | 45 | |
a96bab36 VZ |
46 | void OnKeyDown(wxKeyEvent& event) { LogEvent("KeyDown", event); } |
47 | void OnKeyUp(wxKeyEvent& event) { LogEvent("KeyUp", event); } | |
48 | void OnChar(wxKeyEvent& event) { LogEvent("Char", event); } | |
49 | void OnPaintInputWin(wxPaintEvent& event); | |
7de7aed4 | 50 | |
a96bab36 | 51 | void LogEvent(const wxString& name, wxKeyEvent& event); |
7de7aed4 | 52 | |
a96bab36 VZ |
53 | wxTextCtrl *m_logText; |
54 | wxWindow *m_inputWin; | |
55 | bool m_skip; | |
7de7aed4 VZ |
56 | }; |
57 | ||
7de7aed4 | 58 | |
a96bab36 VZ |
59 | // Define a new application type, each program should derive a class from wxApp |
60 | class MyApp : public wxApp | |
7de7aed4 VZ |
61 | { |
62 | public: | |
a96bab36 VZ |
63 | // 'Main program' equivalent: the program execution "starts" here |
64 | virtual bool OnInit() | |
7de7aed4 | 65 | { |
a96bab36 VZ |
66 | // create the main application window |
67 | new MyFrame("Keyboard wxWidgets App"); | |
7de7aed4 | 68 | |
a96bab36 VZ |
69 | // If we returned false here, the application would exit immediately. |
70 | return true; | |
7de7aed4 | 71 | } |
7de7aed4 VZ |
72 | }; |
73 | ||
be5a51fb | 74 | // Create a new application object: this macro will allow wxWidgets to create |
7de7aed4 VZ |
75 | // the application object during program execution (it's better than using a |
76 | // static object for many reasons) and also declares the accessor function | |
77 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
78 | // not wxApp) | |
79 | IMPLEMENT_APP(MyApp) | |
80 | ||
a96bab36 | 81 | |
7de7aed4 VZ |
82 | // ============================================================================ |
83 | // implementation | |
84 | // ============================================================================ | |
85 | ||
7de7aed4 | 86 | // frame constructor |
a96bab36 VZ |
87 | MyFrame::MyFrame(const wxString& title) |
88 | : wxFrame(NULL, wxID_ANY, title), | |
89 | m_inputWin(NULL), | |
90 | m_skip(true) | |
7de7aed4 | 91 | { |
41f02b9a FM |
92 | SetIcon(wxICON(sample)); |
93 | ||
a96bab36 VZ |
94 | // IDs for menu items |
95 | enum | |
96 | { | |
97 | QuitID = wxID_EXIT, | |
98 | ClearID = wxID_CLEAR, | |
99 | SkipID = 100, | |
a30305f8 VZ |
100 | TestAccelA, |
101 | TestAccelCtrlA, | |
102 | TestAccelEsc | |
a96bab36 VZ |
103 | }; |
104 | ||
7de7aed4 VZ |
105 | // create a menu bar |
106 | wxMenu *menuFile = new wxMenu; | |
a96bab36 VZ |
107 | |
108 | menuFile->Append(ClearID, "&Clear log\tCtrl-L"); | |
109 | menuFile->AppendSeparator(); | |
110 | ||
a30305f8 VZ |
111 | menuFile->Append(TestAccelA, "Test accelerator &1\tA"); |
112 | menuFile->Append(TestAccelCtrlA, "Test accelerator &2\tCtrl-A"); | |
113 | menuFile->Append(TestAccelEsc, "Test accelerator &3\tEsc"); | |
114 | menuFile->AppendSeparator(); | |
115 | ||
a96bab36 VZ |
116 | menuFile->AppendCheckItem(SkipID, "Call event.&Skip()\tCtrl-S"); |
117 | menuFile->Check(SkipID, true); | |
7de7aed4 | 118 | menuFile->AppendSeparator(); |
7de7aed4 | 119 | |
a96bab36 | 120 | menuFile->Append(QuitID, "E&xit\tAlt-X", "Quit this program"); |
7de7aed4 VZ |
121 | |
122 | // the "About" item should be in the help menu | |
123 | wxMenu *menuHelp = new wxMenu; | |
a96bab36 | 124 | menuHelp->Append(wxID_ABOUT, "&About...\tF1", "Show about dialog"); |
7de7aed4 VZ |
125 | |
126 | // now append the freshly created menu to the menu bar... | |
127 | wxMenuBar *menuBar = new wxMenuBar(); | |
a96bab36 VZ |
128 | menuBar->Append(menuFile, "&File"); |
129 | menuBar->Append(menuHelp, "&Help"); | |
7de7aed4 VZ |
130 | |
131 | // ... and attach this menu bar to the frame | |
132 | SetMenuBar(menuBar); | |
133 | ||
a96bab36 VZ |
134 | m_inputWin = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxSize(-1, 50), |
135 | wxRAISED_BORDER), | |
136 | m_inputWin->SetBackgroundColour(*wxBLUE); | |
137 | ||
138 | wxTextCtrl *headerText = new wxTextCtrl(this, wxID_ANY, "", | |
139 | wxDefaultPosition, wxDefaultSize, | |
140 | wxTE_READONLY); | |
141 | headerText->SetValue( | |
142 | " event key KeyCode mod UnicodeKey " | |
143 | " RawKeyCode RawKeyFlags"); | |
144 | ||
145 | ||
41f02b9a | 146 | m_logText = new wxTextCtrl(this, wxID_ANY, "", |
a96bab36 VZ |
147 | wxDefaultPosition, wxDefaultSize, |
148 | wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL); | |
149 | ||
150 | // set monospace font to have output in nice columns | |
41f02b9a | 151 | wxFont font(10, wxFONTFAMILY_TELETYPE, |
a96bab36 VZ |
152 | wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL); |
153 | headerText->SetFont(font); | |
154 | m_logText->SetFont(font); | |
155 | ||
156 | // layout | |
157 | wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL); | |
158 | sizer->Add(m_inputWin, wxSizerFlags().Expand()); | |
159 | sizer->Add(headerText, wxSizerFlags().Expand()); | |
160 | sizer->Add(m_logText, wxSizerFlags(1).Expand()); | |
161 | SetSizerAndFit(sizer); | |
162 | ||
163 | // set size and position on screen | |
164 | SetSize(700, 340); | |
165 | CentreOnScreen(); | |
166 | ||
167 | // connect menu event handlers | |
168 | ||
169 | Connect(QuitID, wxEVT_COMMAND_MENU_SELECTED, | |
170 | wxCommandEventHandler(MyFrame::OnQuit)); | |
171 | ||
172 | Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, | |
173 | wxCommandEventHandler(MyFrame::OnAbout)); | |
174 | ||
175 | Connect(ClearID, wxEVT_COMMAND_MENU_SELECTED, | |
176 | wxCommandEventHandler(MyFrame::OnClear)); | |
177 | ||
178 | Connect(SkipID, wxEVT_COMMAND_MENU_SELECTED, | |
179 | wxCommandEventHandler(MyFrame::OnSkip)); | |
180 | ||
a30305f8 VZ |
181 | Connect(TestAccelA, wxEVT_COMMAND_MENU_SELECTED, |
182 | wxCommandEventHandler(MyFrame::OnTestAccelA)); | |
183 | ||
184 | Connect(TestAccelCtrlA, wxEVT_COMMAND_MENU_SELECTED, | |
185 | wxCommandEventHandler(MyFrame::OnTestAccelCtrlA)); | |
186 | ||
187 | Connect(TestAccelEsc, wxEVT_COMMAND_MENU_SELECTED, | |
188 | wxCommandEventHandler(MyFrame::OnTestAccelEsc)); | |
189 | ||
41f02b9a | 190 | // connect event handlers for the blue input window |
a96bab36 VZ |
191 | m_inputWin->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MyFrame::OnKeyDown), |
192 | NULL, this); | |
193 | m_inputWin->Connect(wxEVT_KEY_UP, wxKeyEventHandler(MyFrame::OnKeyUp), | |
194 | NULL, this); | |
195 | m_inputWin->Connect(wxEVT_CHAR, wxKeyEventHandler(MyFrame::OnChar), | |
196 | NULL, this); | |
41f02b9a | 197 | m_inputWin->Connect(wxEVT_PAINT, |
a96bab36 VZ |
198 | wxPaintEventHandler(MyFrame::OnPaintInputWin), |
199 | NULL, this); | |
200 | ||
201 | // and show itself (the frames, unlike simple controls, are not shown when | |
202 | // created initially) | |
203 | Show(true); | |
7de7aed4 VZ |
204 | } |
205 | ||
206 | // event handlers | |
207 | ||
7de7aed4 VZ |
208 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
209 | { | |
a96bab36 VZ |
210 | wxMessageBox("Demonstrates keyboard event processing in wxWidgets\n" |
211 | "(c) 2002 Vadim Zeitlin\n" | |
212 | "(c) 2008 Marcin Wojdyr", | |
213 | "About wxWidgets Keyboard Sample", | |
214 | wxOK | wxICON_INFORMATION, this); | |
7de7aed4 VZ |
215 | } |
216 | ||
a96bab36 | 217 | void MyFrame::OnPaintInputWin(wxPaintEvent& WXUNUSED(event)) |
7de7aed4 | 218 | { |
a96bab36 VZ |
219 | wxPaintDC dc(m_inputWin); |
220 | dc.SetTextForeground(*wxWHITE); | |
221 | wxFont font(*wxSWISS_FONT); | |
222 | font.SetWeight(wxFONTWEIGHT_BOLD); | |
223 | font.SetPointSize(font.GetPointSize() + 2); | |
224 | dc.SetFont(font); | |
225 | ||
41f02b9a | 226 | dc.DrawLabel("Press keys here", |
a96bab36 | 227 | m_inputWin->GetClientRect(), wxALIGN_CENTER); |
7de7aed4 VZ |
228 | } |
229 | ||
62fa9712 | 230 | |
a96bab36 VZ |
231 | // helper function that returns textual description of wx virtual keycode |
232 | const char* GetVirtualKeyCodeName(int keycode) | |
7de7aed4 | 233 | { |
a96bab36 | 234 | switch ( keycode ) |
7de7aed4 | 235 | { |
a96bab36 | 236 | #define WXK_(x) \ |
41f02b9a | 237 | case WXK_##x: return #x; |
a96bab36 VZ |
238 | |
239 | WXK_(BACK) | |
240 | WXK_(TAB) | |
241 | WXK_(RETURN) | |
242 | WXK_(ESCAPE) | |
243 | WXK_(SPACE) | |
244 | WXK_(DELETE) | |
245 | WXK_(START) | |
246 | WXK_(LBUTTON) | |
247 | WXK_(RBUTTON) | |
248 | WXK_(CANCEL) | |
249 | WXK_(MBUTTON) | |
250 | WXK_(CLEAR) | |
251 | WXK_(SHIFT) | |
252 | WXK_(ALT) | |
253 | WXK_(CONTROL) | |
254 | WXK_(MENU) | |
255 | WXK_(PAUSE) | |
256 | WXK_(CAPITAL) | |
257 | WXK_(END) | |
258 | WXK_(HOME) | |
259 | WXK_(LEFT) | |
260 | WXK_(UP) | |
261 | WXK_(RIGHT) | |
262 | WXK_(DOWN) | |
263 | WXK_(SELECT) | |
264 | WXK_(PRINT) | |
265 | WXK_(EXECUTE) | |
266 | WXK_(SNAPSHOT) | |
267 | WXK_(INSERT) | |
268 | WXK_(HELP) | |
269 | WXK_(NUMPAD0) | |
270 | WXK_(NUMPAD1) | |
271 | WXK_(NUMPAD2) | |
272 | WXK_(NUMPAD3) | |
273 | WXK_(NUMPAD4) | |
274 | WXK_(NUMPAD5) | |
275 | WXK_(NUMPAD6) | |
276 | WXK_(NUMPAD7) | |
277 | WXK_(NUMPAD8) | |
278 | WXK_(NUMPAD9) | |
279 | WXK_(MULTIPLY) | |
280 | WXK_(ADD) | |
281 | WXK_(SEPARATOR) | |
282 | WXK_(SUBTRACT) | |
283 | WXK_(DECIMAL) | |
284 | WXK_(DIVIDE) | |
285 | WXK_(F1) | |
286 | WXK_(F2) | |
287 | WXK_(F3) | |
288 | WXK_(F4) | |
289 | WXK_(F5) | |
290 | WXK_(F6) | |
291 | WXK_(F7) | |
292 | WXK_(F8) | |
293 | WXK_(F9) | |
294 | WXK_(F10) | |
295 | WXK_(F11) | |
296 | WXK_(F12) | |
297 | WXK_(F13) | |
298 | WXK_(F14) | |
299 | WXK_(F15) | |
300 | WXK_(F16) | |
301 | WXK_(F17) | |
302 | WXK_(F18) | |
303 | WXK_(F19) | |
304 | WXK_(F20) | |
305 | WXK_(F21) | |
306 | WXK_(F22) | |
307 | WXK_(F23) | |
308 | WXK_(F24) | |
309 | WXK_(NUMLOCK) | |
310 | WXK_(SCROLL) | |
311 | WXK_(PAGEUP) | |
312 | WXK_(PAGEDOWN) | |
313 | WXK_(NUMPAD_SPACE) | |
314 | WXK_(NUMPAD_TAB) | |
315 | WXK_(NUMPAD_ENTER) | |
316 | WXK_(NUMPAD_F1) | |
317 | WXK_(NUMPAD_F2) | |
318 | WXK_(NUMPAD_F3) | |
319 | WXK_(NUMPAD_F4) | |
320 | WXK_(NUMPAD_HOME) | |
321 | WXK_(NUMPAD_LEFT) | |
322 | WXK_(NUMPAD_UP) | |
323 | WXK_(NUMPAD_RIGHT) | |
324 | WXK_(NUMPAD_DOWN) | |
325 | WXK_(NUMPAD_PAGEUP) | |
326 | WXK_(NUMPAD_PAGEDOWN) | |
327 | WXK_(NUMPAD_END) | |
328 | WXK_(NUMPAD_BEGIN) | |
329 | WXK_(NUMPAD_INSERT) | |
330 | WXK_(NUMPAD_DELETE) | |
331 | WXK_(NUMPAD_EQUAL) | |
332 | WXK_(NUMPAD_MULTIPLY) | |
333 | WXK_(NUMPAD_ADD) | |
334 | WXK_(NUMPAD_SEPARATOR) | |
335 | WXK_(NUMPAD_SUBTRACT) | |
336 | WXK_(NUMPAD_DECIMAL) | |
337 | WXK_(NUMPAD_DIVIDE) | |
e7b12eec VZ |
338 | |
339 | WXK_(WINDOWS_LEFT) | |
340 | WXK_(WINDOWS_RIGHT) | |
341 | WXK_(COMMAND) | |
a96bab36 | 342 | #undef WXK_ |
e7b12eec | 343 | |
41f02b9a FM |
344 | default: |
345 | return NULL; | |
7de7aed4 VZ |
346 | } |
347 | } | |
348 | ||
a96bab36 VZ |
349 | // helper function that returns textual description of key in the event |
350 | wxString GetKeyName(const wxKeyEvent &event) | |
7de7aed4 | 351 | { |
a96bab36 VZ |
352 | int keycode = event.GetKeyCode(); |
353 | const char* virt = GetVirtualKeyCodeName(keycode); | |
354 | if ( virt ) | |
355 | return virt; | |
b6885972 | 356 | if ( keycode > 0 && keycode < 32 ) |
a96bab36 | 357 | return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode - 1)); |
b6885972 | 358 | if ( keycode >= 32 && keycode < 128 ) |
a96bab36 | 359 | return wxString::Format("'%c'", (unsigned char)keycode); |
e7b12eec | 360 | |
52bbab36 | 361 | #if wxUSE_UNICODE |
e7b12eec VZ |
362 | int uc = event.GetUnicodeKey(); |
363 | if ( uc != WXK_NONE ) | |
364 | return wxString::Format("'%c'", uc); | |
52bbab36 | 365 | #endif |
e7b12eec VZ |
366 | |
367 | return "unknown"; | |
a96bab36 VZ |
368 | } |
369 | ||
7de7aed4 | 370 | |
a96bab36 VZ |
371 | void MyFrame::LogEvent(const wxString& name, wxKeyEvent& event) |
372 | { | |
62fa9712 | 373 | wxString msg; |
41f02b9a | 374 | // event key_name KeyCode modifiers Unicode raw_code raw_flags |
a96bab36 VZ |
375 | msg.Printf("%7s %15s %5d %c%c%c%c" |
376 | #if wxUSE_UNICODE | |
377 | "%5d (U+%04x)" | |
378 | #else | |
379 | " none " | |
380 | #endif | |
381 | #ifdef wxHAS_RAW_KEY_CODES | |
b6885972 | 382 | " %7lu 0x%08lx" |
a96bab36 VZ |
383 | #else |
384 | " not-set not-set" | |
385 | #endif | |
386 | "\n", | |
62fa9712 | 387 | name, |
a96bab36 VZ |
388 | GetKeyName(event), |
389 | event.GetKeyCode(), | |
390 | event.ControlDown() ? 'C' : '-', | |
391 | event.AltDown() ? 'A' : '-', | |
392 | event.ShiftDown() ? 'S' : '-', | |
66ad7d6a | 393 | event.MetaDown() ? 'M' : '-' |
a96bab36 | 394 | #if wxUSE_UNICODE |
66ad7d6a VZ |
395 | , event.GetUnicodeKey() |
396 | , event.GetUnicodeKey() | |
a96bab36 VZ |
397 | #endif |
398 | #ifdef wxHAS_RAW_KEY_CODES | |
66ad7d6a VZ |
399 | , (unsigned long) event.GetRawKeyCode() |
400 | , (unsigned long) event.GetRawKeyFlags() | |
a96bab36 VZ |
401 | #endif |
402 | ); | |
62fa9712 | 403 | |
a96bab36 | 404 | m_logText->AppendText(msg); |
7de7aed4 VZ |
405 | |
406 | if ( m_skip ) | |
7de7aed4 | 407 | event.Skip(); |
7de7aed4 | 408 | } |
a96bab36 VZ |
409 | |
410 |