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