]> git.saurik.com Git - wxWidgets.git/blame - samples/keyboard/keyboard.cpp
Change version to 3.0.0.
[wxWidgets.git] / samples / keyboard / keyboard.cpp
CommitLineData
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 6// Created: 07.04.02
7de7aed4
VZ
7// Copyright: (c) 2002 Vadim Zeitlin
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
7de7aed4
VZ
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
14 #pragma hdrstop
15#endif
7de7aed4
VZ
16#ifndef WX_PRECOMP
17 #include "wx/wx.h"
18#endif
19
e7092398 20#ifndef wxHAS_IMAGES_IN_RESOURCES
41f02b9a
FM
21 #include "../sample.xpm"
22#endif
7de7aed4 23
c77ef57e
VZ
24// IDs for menu items
25enum
26{
27 QuitID = wxID_EXIT,
28 ClearID = wxID_CLEAR,
29 SkipHook = 100,
30 SkipDown,
31
32 // These IDs must be in the same order as MyFrame::InputKind enum elements.
33 IDInputCustom,
34 IDInputEntry,
35 IDInputText,
36
37 TestAccelA,
38 TestAccelCtrlA,
39 TestAccelEsc
40};
41
7de7aed4
VZ
42// Define a new frame type: this is going to be our main frame
43class MyFrame : public wxFrame
44{
45public:
a96bab36 46 MyFrame(const wxString& title);
7de7aed4
VZ
47
48private:
a96bab36
VZ
49 // event handlers
50 void OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(true); }
51 void OnAbout(wxCommandEvent& event);
a30305f8 52
c77ef57e 53 void OnInputWindowKind(wxCommandEvent& event);
7c8bfa87 54
a30305f8
VZ
55 void OnTestAccelA(wxCommandEvent& WXUNUSED(event))
56 { m_logText->AppendText("Test accelerator \"A\" used.\n"); }
57 void OnTestAccelCtrlA(wxCommandEvent& WXUNUSED(event))
58 { m_logText->AppendText("Test accelerator \"Ctrl-A\" used.\n"); }
59 void OnTestAccelEsc(wxCommandEvent& WXUNUSED(event))
60 { m_logText->AppendText("Test accelerator \"Esc\" used.\n"); }
61
a96bab36 62 void OnClear(wxCommandEvent& WXUNUSED(event)) { m_logText->Clear(); }
edf5822a
VZ
63 void OnSkipDown(wxCommandEvent& event) { m_skipDown = event.IsChecked(); }
64 void OnSkipHook(wxCommandEvent& event) { m_skipHook = event.IsChecked(); }
a30305f8 65
edf5822a
VZ
66 void OnKeyDown(wxKeyEvent& event)
67 {
68 LogEvent("KeyDown", event);
69 if ( m_skipDown )
70 event.Skip();
71 }
a96bab36 72 void OnKeyUp(wxKeyEvent& event) { LogEvent("KeyUp", event); }
819638a7 73 void OnChar(wxKeyEvent& event) { LogEvent("Char", event); event.Skip(); }
edf5822a
VZ
74 void OnCharHook(wxKeyEvent& event)
75 {
c4f49d69
VZ
76 // The logged messages can be confusing if the input window doesn't
77 // have focus so warn about this.
78 if ( !m_inputWin->HasFocus() )
79 {
80 m_logText->SetDefaultStyle(*wxRED);
81 m_logText->AppendText("WARNING: focus is not on input window, "
82 "non-hook events won't be logged.\n");
83 m_logText->SetDefaultStyle(wxTextAttr());
84 }
85
edf5822a
VZ
86 LogEvent("Hook", event);
87 if ( m_skipHook )
88 event.Skip();
89 }
90
a96bab36 91 void OnPaintInputWin(wxPaintEvent& event);
7de7aed4 92
a96bab36 93 void LogEvent(const wxString& name, wxKeyEvent& event);
7de7aed4 94
c77ef57e
VZ
95 // Set m_inputWin to either a new window of the given kind:
96 enum InputKind
97 {
98 Input_Custom, // Just a plain wxWindow
99 Input_Entry, // Single-line wxTextCtrl
100 Input_Text // Multi-line wxTextCtrl
101 };
102
103 void DoCreateInputWindow(InputKind inputKind);
7c8bfa87 104
a96bab36
VZ
105 wxTextCtrl *m_logText;
106 wxWindow *m_inputWin;
edf5822a
VZ
107 bool m_skipHook,
108 m_skipDown;
7de7aed4
VZ
109};
110
7de7aed4 111
a96bab36
VZ
112// Define a new application type, each program should derive a class from wxApp
113class MyApp : public wxApp
7de7aed4
VZ
114{
115public:
a96bab36
VZ
116 // 'Main program' equivalent: the program execution "starts" here
117 virtual bool OnInit()
7de7aed4 118 {
a96bab36
VZ
119 // create the main application window
120 new MyFrame("Keyboard wxWidgets App");
7de7aed4 121
a96bab36
VZ
122 // If we returned false here, the application would exit immediately.
123 return true;
7de7aed4 124 }
7de7aed4
VZ
125};
126
be5a51fb 127// Create a new application object: this macro will allow wxWidgets to create
7de7aed4
VZ
128// the application object during program execution (it's better than using a
129// static object for many reasons) and also declares the accessor function
130// wxGetApp() which will return the reference of the right type (i.e. MyApp and
131// not wxApp)
132IMPLEMENT_APP(MyApp)
133
a96bab36 134
7de7aed4
VZ
135// ============================================================================
136// implementation
137// ============================================================================
138
7de7aed4 139// frame constructor
a96bab36
VZ
140MyFrame::MyFrame(const wxString& title)
141 : wxFrame(NULL, wxID_ANY, title),
142 m_inputWin(NULL),
edf5822a
VZ
143 m_skipHook(true),
144 m_skipDown(true)
7de7aed4 145{
41f02b9a
FM
146 SetIcon(wxICON(sample));
147
7de7aed4
VZ
148 // create a menu bar
149 wxMenu *menuFile = new wxMenu;
a96bab36
VZ
150
151 menuFile->Append(ClearID, "&Clear log\tCtrl-L");
152 menuFile->AppendSeparator();
153
a30305f8
VZ
154 menuFile->Append(TestAccelA, "Test accelerator &1\tA");
155 menuFile->Append(TestAccelCtrlA, "Test accelerator &2\tCtrl-A");
156 menuFile->Append(TestAccelEsc, "Test accelerator &3\tEsc");
157 menuFile->AppendSeparator();
158
edf5822a
VZ
159 menuFile->AppendCheckItem(SkipHook, "Skip CHAR_HOOK event",
160 "Not skipping this event disables both KEY_DOWN and CHAR events"
161 );
162 menuFile->Check(SkipHook, true);
163 menuFile->AppendCheckItem(SkipDown, "Skip KEY_DOWN event",
164 "Not skipping this event disables CHAR event generation"
165 );
166 menuFile->Check(SkipDown, true);
7de7aed4 167 menuFile->AppendSeparator();
7de7aed4 168
c77ef57e
VZ
169 menuFile->AppendRadioItem(IDInputCustom, "Use &custom control\tCtrl-C",
170 "Use custom wxWindow for input window"
171 );
172 menuFile->AppendRadioItem(IDInputEntry, "Use text &entry\tCtrl-E",
173 "Use single-line wxTextCtrl for input window"
174 );
175 menuFile->AppendRadioItem(IDInputText, "Use &text control\tCtrl-T",
176 "Use multi-line wxTextCtrl for input window"
7c8bfa87
VZ
177 );
178 menuFile->AppendSeparator();
179
a96bab36 180 menuFile->Append(QuitID, "E&xit\tAlt-X", "Quit this program");
7de7aed4
VZ
181
182 // the "About" item should be in the help menu
183 wxMenu *menuHelp = new wxMenu;
2d143b66 184 menuHelp->Append(wxID_ABOUT, "&About\tF1", "Show about dialog");
7de7aed4
VZ
185
186 // now append the freshly created menu to the menu bar...
187 wxMenuBar *menuBar = new wxMenuBar();
a96bab36
VZ
188 menuBar->Append(menuFile, "&File");
189 menuBar->Append(menuHelp, "&Help");
7de7aed4
VZ
190
191 // ... and attach this menu bar to the frame
192 SetMenuBar(menuBar);
193
c77ef57e 194 DoCreateInputWindow(Input_Custom);
a96bab36
VZ
195
196 wxTextCtrl *headerText = new wxTextCtrl(this, wxID_ANY, "",
197 wxDefaultPosition, wxDefaultSize,
198 wxTE_READONLY);
199 headerText->SetValue(
200 " event key KeyCode mod UnicodeKey "
2f7baaec 201 " RawKeyCode RawKeyFlags Position");
a96bab36
VZ
202
203
41f02b9a 204 m_logText = new wxTextCtrl(this, wxID_ANY, "",
a96bab36 205 wxDefaultPosition, wxDefaultSize,
c4f49d69 206 wxTE_MULTILINE|wxTE_READONLY|wxTE_RICH|wxHSCROLL);
a96bab36
VZ
207
208 // set monospace font to have output in nice columns
41f02b9a 209 wxFont font(10, wxFONTFAMILY_TELETYPE,
a96bab36
VZ
210 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
211 headerText->SetFont(font);
212 m_logText->SetFont(font);
213
214 // layout
215 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
216 sizer->Add(m_inputWin, wxSizerFlags().Expand());
217 sizer->Add(headerText, wxSizerFlags().Expand());
218 sizer->Add(m_logText, wxSizerFlags(1).Expand());
219 SetSizerAndFit(sizer);
220
221 // set size and position on screen
222 SetSize(700, 340);
223 CentreOnScreen();
224
225 // connect menu event handlers
226
ce7fe42e 227 Connect(QuitID, wxEVT_MENU,
a96bab36
VZ
228 wxCommandEventHandler(MyFrame::OnQuit));
229
ce7fe42e 230 Connect(wxID_ABOUT, wxEVT_MENU,
a96bab36
VZ
231 wxCommandEventHandler(MyFrame::OnAbout));
232
ce7fe42e 233 Connect(ClearID, wxEVT_MENU,
a96bab36
VZ
234 wxCommandEventHandler(MyFrame::OnClear));
235
ce7fe42e 236 Connect(SkipHook, wxEVT_MENU,
edf5822a 237 wxCommandEventHandler(MyFrame::OnSkipHook));
ce7fe42e 238 Connect(SkipDown, wxEVT_MENU,
edf5822a 239 wxCommandEventHandler(MyFrame::OnSkipDown));
a96bab36 240
ce7fe42e 241 Connect(IDInputCustom, IDInputText, wxEVT_MENU,
c77ef57e 242 wxCommandEventHandler(MyFrame::OnInputWindowKind));
7c8bfa87 243
ce7fe42e 244 Connect(TestAccelA, wxEVT_MENU,
a30305f8
VZ
245 wxCommandEventHandler(MyFrame::OnTestAccelA));
246
ce7fe42e 247 Connect(TestAccelCtrlA, wxEVT_MENU,
a30305f8
VZ
248 wxCommandEventHandler(MyFrame::OnTestAccelCtrlA));
249
ce7fe42e 250 Connect(TestAccelEsc, wxEVT_MENU,
a30305f8
VZ
251 wxCommandEventHandler(MyFrame::OnTestAccelEsc));
252
edf5822a
VZ
253 // notice that we don't connect OnCharHook() to the input window, unlike
254 // the usual key events this one is propagated upwards
255 Connect(wxEVT_CHAR_HOOK, wxKeyEventHandler(MyFrame::OnCharHook));
256
257 // status bar is useful for showing the menu items help strings
258 CreateStatusBar();
259
a96bab36
VZ
260 // and show itself (the frames, unlike simple controls, are not shown when
261 // created initially)
262 Show(true);
7de7aed4
VZ
263}
264
265// event handlers
266
7de7aed4
VZ
267void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
268{
a96bab36
VZ
269 wxMessageBox("Demonstrates keyboard event processing in wxWidgets\n"
270 "(c) 2002 Vadim Zeitlin\n"
271 "(c) 2008 Marcin Wojdyr",
272 "About wxWidgets Keyboard Sample",
273 wxOK | wxICON_INFORMATION, this);
7de7aed4
VZ
274}
275
c77ef57e 276void MyFrame::DoCreateInputWindow(InputKind inputKind)
7c8bfa87
VZ
277{
278 wxWindow* const oldWin = m_inputWin;
279
c77ef57e
VZ
280 switch ( inputKind )
281 {
282 case Input_Custom:
283 m_inputWin = new wxWindow(this, wxID_ANY,
284 wxDefaultPosition, wxSize(-1, 50),
285 wxRAISED_BORDER);
286 break;
287
288 case Input_Entry:
289 m_inputWin = new wxTextCtrl(this, wxID_ANY, "Press keys here");
290 break;
291
292 case Input_Text:
293 m_inputWin = new wxTextCtrl(this, wxID_ANY, "Press keys here",
294 wxDefaultPosition, wxSize(-1, 50),
295 wxTE_MULTILINE);
296 break;
297 }
298
7c8bfa87
VZ
299 m_inputWin->SetBackgroundColour(*wxBLUE);
300 m_inputWin->SetForegroundColour(*wxWHITE);
301
302 // connect event handlers for the blue input window
303 m_inputWin->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MyFrame::OnKeyDown),
304 NULL, this);
305 m_inputWin->Connect(wxEVT_KEY_UP, wxKeyEventHandler(MyFrame::OnKeyUp),
306 NULL, this);
307 m_inputWin->Connect(wxEVT_CHAR, wxKeyEventHandler(MyFrame::OnChar),
308 NULL, this);
309
c77ef57e 310 if ( inputKind == Input_Custom )
7c8bfa87
VZ
311 {
312 m_inputWin->Connect(wxEVT_PAINT,
313 wxPaintEventHandler(MyFrame::OnPaintInputWin),
314 NULL, this);
315 }
316
317 if ( oldWin )
318 {
319 GetSizer()->Replace(oldWin, m_inputWin);
320 Layout();
321 delete oldWin;
322 }
323}
324
c77ef57e 325void MyFrame::OnInputWindowKind(wxCommandEvent& event)
7c8bfa87 326{
c77ef57e
VZ
327 DoCreateInputWindow(
328 static_cast<InputKind>(event.GetId() - IDInputCustom)
329 );
7c8bfa87
VZ
330}
331
a96bab36 332void MyFrame::OnPaintInputWin(wxPaintEvent& WXUNUSED(event))
7de7aed4 333{
a96bab36
VZ
334 wxPaintDC dc(m_inputWin);
335 dc.SetTextForeground(*wxWHITE);
336 wxFont font(*wxSWISS_FONT);
337 font.SetWeight(wxFONTWEIGHT_BOLD);
338 font.SetPointSize(font.GetPointSize() + 2);
339 dc.SetFont(font);
340
41f02b9a 341 dc.DrawLabel("Press keys here",
a96bab36 342 m_inputWin->GetClientRect(), wxALIGN_CENTER);
7de7aed4
VZ
343}
344
62fa9712 345
a96bab36
VZ
346// helper function that returns textual description of wx virtual keycode
347const char* GetVirtualKeyCodeName(int keycode)
7de7aed4 348{
a96bab36 349 switch ( keycode )
7de7aed4 350 {
a96bab36 351#define WXK_(x) \
41f02b9a 352 case WXK_##x: return #x;
a96bab36
VZ
353
354 WXK_(BACK)
355 WXK_(TAB)
356 WXK_(RETURN)
357 WXK_(ESCAPE)
358 WXK_(SPACE)
359 WXK_(DELETE)
360 WXK_(START)
361 WXK_(LBUTTON)
362 WXK_(RBUTTON)
363 WXK_(CANCEL)
364 WXK_(MBUTTON)
365 WXK_(CLEAR)
366 WXK_(SHIFT)
367 WXK_(ALT)
368 WXK_(CONTROL)
369 WXK_(MENU)
370 WXK_(PAUSE)
371 WXK_(CAPITAL)
372 WXK_(END)
373 WXK_(HOME)
374 WXK_(LEFT)
375 WXK_(UP)
376 WXK_(RIGHT)
377 WXK_(DOWN)
378 WXK_(SELECT)
379 WXK_(PRINT)
380 WXK_(EXECUTE)
381 WXK_(SNAPSHOT)
382 WXK_(INSERT)
383 WXK_(HELP)
384 WXK_(NUMPAD0)
385 WXK_(NUMPAD1)
386 WXK_(NUMPAD2)
387 WXK_(NUMPAD3)
388 WXK_(NUMPAD4)
389 WXK_(NUMPAD5)
390 WXK_(NUMPAD6)
391 WXK_(NUMPAD7)
392 WXK_(NUMPAD8)
393 WXK_(NUMPAD9)
394 WXK_(MULTIPLY)
395 WXK_(ADD)
396 WXK_(SEPARATOR)
397 WXK_(SUBTRACT)
398 WXK_(DECIMAL)
399 WXK_(DIVIDE)
400 WXK_(F1)
401 WXK_(F2)
402 WXK_(F3)
403 WXK_(F4)
404 WXK_(F5)
405 WXK_(F6)
406 WXK_(F7)
407 WXK_(F8)
408 WXK_(F9)
409 WXK_(F10)
410 WXK_(F11)
411 WXK_(F12)
412 WXK_(F13)
413 WXK_(F14)
414 WXK_(F15)
415 WXK_(F16)
416 WXK_(F17)
417 WXK_(F18)
418 WXK_(F19)
419 WXK_(F20)
420 WXK_(F21)
421 WXK_(F22)
422 WXK_(F23)
423 WXK_(F24)
424 WXK_(NUMLOCK)
425 WXK_(SCROLL)
426 WXK_(PAGEUP)
427 WXK_(PAGEDOWN)
428 WXK_(NUMPAD_SPACE)
429 WXK_(NUMPAD_TAB)
430 WXK_(NUMPAD_ENTER)
431 WXK_(NUMPAD_F1)
432 WXK_(NUMPAD_F2)
433 WXK_(NUMPAD_F3)
434 WXK_(NUMPAD_F4)
435 WXK_(NUMPAD_HOME)
436 WXK_(NUMPAD_LEFT)
437 WXK_(NUMPAD_UP)
438 WXK_(NUMPAD_RIGHT)
439 WXK_(NUMPAD_DOWN)
440 WXK_(NUMPAD_PAGEUP)
441 WXK_(NUMPAD_PAGEDOWN)
442 WXK_(NUMPAD_END)
443 WXK_(NUMPAD_BEGIN)
444 WXK_(NUMPAD_INSERT)
445 WXK_(NUMPAD_DELETE)
446 WXK_(NUMPAD_EQUAL)
447 WXK_(NUMPAD_MULTIPLY)
448 WXK_(NUMPAD_ADD)
449 WXK_(NUMPAD_SEPARATOR)
450 WXK_(NUMPAD_SUBTRACT)
451 WXK_(NUMPAD_DECIMAL)
452 WXK_(NUMPAD_DIVIDE)
e7b12eec
VZ
453
454 WXK_(WINDOWS_LEFT)
455 WXK_(WINDOWS_RIGHT)
d3f81b85
SC
456#ifdef __WXOSX__
457 WXK_(RAW_CONTROL)
458#endif
a96bab36 459#undef WXK_
e7b12eec 460
41f02b9a
FM
461 default:
462 return NULL;
7de7aed4
VZ
463 }
464}
465
a96bab36
VZ
466// helper function that returns textual description of key in the event
467wxString GetKeyName(const wxKeyEvent &event)
7de7aed4 468{
a96bab36
VZ
469 int keycode = event.GetKeyCode();
470 const char* virt = GetVirtualKeyCodeName(keycode);
471 if ( virt )
472 return virt;
b6885972 473 if ( keycode > 0 && keycode < 32 )
a96bab36 474 return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode - 1));
b6885972 475 if ( keycode >= 32 && keycode < 128 )
a96bab36 476 return wxString::Format("'%c'", (unsigned char)keycode);
e7b12eec 477
52bbab36 478#if wxUSE_UNICODE
e7b12eec
VZ
479 int uc = event.GetUnicodeKey();
480 if ( uc != WXK_NONE )
481 return wxString::Format("'%c'", uc);
52bbab36 482#endif
e7b12eec
VZ
483
484 return "unknown";
a96bab36
VZ
485}
486
7de7aed4 487
a96bab36
VZ
488void MyFrame::LogEvent(const wxString& name, wxKeyEvent& event)
489{
62fa9712 490 wxString msg;
2f7baaec 491 // event key_name KeyCode modifiers Unicode raw_code raw_flags pos
a96bab36
VZ
492 msg.Printf("%7s %15s %5d %c%c%c%c"
493#if wxUSE_UNICODE
494 "%5d (U+%04x)"
495#else
496 " none "
497#endif
498#ifdef wxHAS_RAW_KEY_CODES
b6885972 499 " %7lu 0x%08lx"
a96bab36
VZ
500#else
501 " not-set not-set"
502#endif
2f7baaec 503 " (%5d,%5d)"
a96bab36 504 "\n",
62fa9712 505 name,
a96bab36
VZ
506 GetKeyName(event),
507 event.GetKeyCode(),
508 event.ControlDown() ? 'C' : '-',
509 event.AltDown() ? 'A' : '-',
510 event.ShiftDown() ? 'S' : '-',
66ad7d6a 511 event.MetaDown() ? 'M' : '-'
a96bab36 512#if wxUSE_UNICODE
66ad7d6a
VZ
513 , event.GetUnicodeKey()
514 , event.GetUnicodeKey()
a96bab36
VZ
515#endif
516#ifdef wxHAS_RAW_KEY_CODES
66ad7d6a
VZ
517 , (unsigned long) event.GetRawKeyCode()
518 , (unsigned long) event.GetRawKeyFlags()
a96bab36 519#endif
2f7baaec
VZ
520 , event.GetX()
521 , event.GetY()
a96bab36 522 );
62fa9712 523
a96bab36 524 m_logText->AppendText(msg);
7de7aed4 525}
a96bab36
VZ
526
527