]>
git.saurik.com Git - wxWidgets.git/blob - samples/keyboard/keyboard.cpp
0276694acaa05c09b32de41e34271fa0a27f218f
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Keyboard wxWindows sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2002 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWindows headers)
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // Define a new application type, each program should derive a class from wxApp
38 class MyApp
: public wxApp
41 // override base class virtuals
42 // ----------------------------
44 // this one is called on application startup and is a good place for the app
45 // initialization (doing it here and not in the ctor allows to have an error
46 // return: if OnInit() returns false, the application terminates)
47 virtual bool OnInit();
50 // Define a new frame type: this is going to be our main frame
51 class MyFrame
: public wxFrame
55 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
56 long style
= wxDEFAULT_FRAME_STYLE
);
58 ~MyFrame() { delete m_logTarget
; }
60 // event handlers (these functions should _not_ be virtual)
61 void OnQuit(wxCommandEvent
& event
);
62 void OnAbout(wxCommandEvent
& event
);
63 void OnClear(wxCommandEvent
& event
);
64 void OnSkip(wxCommandEvent
& event
);
66 void OnSize(wxSizeEvent
& event
);
71 class TextWindow
*m_winText
;
74 // any class wishing to process wxWindows events must use this macro
78 // A log target which just redirects the messages to a listbox
79 class LboxLogger
: public wxLog
82 LboxLogger(wxListBox
*lbox
, wxLog
*logOld
)
85 //m_lbox->Disable(); -- looks ugly under MSW
91 wxLog::SetActiveTarget(m_logOld
);
95 // implement sink functions
96 virtual void DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
98 // don't put trace messages into listbox or we can get into infinite
100 if ( level
== wxLOG_Trace
)
104 // cast is needed to call protected method
105 ((LboxLogger
*)m_logOld
)->DoLog(level
, szString
, t
);
110 wxLog::DoLog(level
, szString
, t
);
114 virtual void DoLogString(const wxChar
*szString
, time_t t
)
120 #ifdef __WXUNIVERSAL__
121 m_lbox
->AppendAndEnsureVisible(msg
);
122 #else // other ports don't have this method yet
124 m_lbox
->SetFirstItem(m_lbox
->GetCount() - 1);
128 // the control we use
131 // the old log target
135 class TextWindow
: public wxWindow
138 TextWindow(wxWindow
*parent
)
139 : wxWindow(parent
, -1, wxDefaultPosition
, wxDefaultSize
,
144 SetBackgroundColour(*wxBLUE
);
147 void SetSkip(bool skip
) { m_skip
= skip
; }
150 void OnKeyDown(wxKeyEvent
& event
) { LogEvent(_T("Key down"), event
); }
151 void OnKeyUp(wxKeyEvent
& event
) { LogEvent(_T("Key up"), event
); }
152 void OnChar(wxKeyEvent
& event
) { LogEvent(_T("Char"), event
); }
154 void OnPaint(wxPaintEvent
& event
)
157 dc
.SetTextForeground(*wxWHITE
);
158 dc
.DrawLabel(_T("Press keys here"), GetClientRect(), wxALIGN_CENTER
);
162 static inline wxChar
GetChar(bool on
, wxChar c
) { return on
? c
: _T('-'); }
164 void LogEvent(const wxChar
*name
, wxKeyEvent
& event
);
168 DECLARE_EVENT_TABLE()
171 BEGIN_EVENT_TABLE(TextWindow
, wxWindow
)
172 EVT_KEY_DOWN(TextWindow::OnKeyDown
)
173 EVT_KEY_UP(TextWindow::OnKeyUp
)
174 EVT_CHAR(TextWindow::OnChar
)
176 EVT_PAINT(TextWindow::OnPaint
)
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 // IDs for the controls and the menu commands
192 // it is important for the id corresponding to the "About" command to have
193 // this standard value as otherwise it won't be handled properly under Mac
194 // (where it is special and put into the "Apple" menu)
195 Keyboard_About
= wxID_ABOUT
198 // ----------------------------------------------------------------------------
199 // event tables and other macros for wxWindows
200 // ----------------------------------------------------------------------------
202 // the event tables connect the wxWindows events with the functions (event
203 // handlers) which process them. It can be also done at run-time, but for the
204 // simple menu events like this the static method is much simpler.
205 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
206 EVT_MENU(Keyboard_Quit
, MyFrame::OnQuit
)
207 EVT_MENU(Keyboard_About
, MyFrame::OnAbout
)
209 EVT_MENU(Keyboard_Clear
, MyFrame::OnClear
)
210 EVT_MENU(Keyboard_Skip
, MyFrame::OnSkip
)
212 EVT_SIZE(MyFrame::OnSize
)
215 // Create a new application object: this macro will allow wxWindows to create
216 // the application object during program execution (it's better than using a
217 // static object for many reasons) and also declares the accessor function
218 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
222 // ============================================================================
224 // ============================================================================
226 // ----------------------------------------------------------------------------
227 // the application class
228 // ----------------------------------------------------------------------------
230 // 'Main program' equivalent: the program execution "starts" here
233 // create the main application window
234 MyFrame
*frame
= new MyFrame(_T("Keyboard wxWindows App"),
235 wxPoint(50, 50), wxSize(450, 340));
237 // and show it (the frames, unlike simple controls, are not shown when
238 // created initially)
241 // success: wxApp::OnRun() will be called which will enter the main message
242 // loop and the application will run. If we returned FALSE here, the
243 // application would exit immediately.
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
252 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
253 : wxFrame(NULL
, -1, title
, pos
, size
, style
),
258 wxMenu
*menuFile
= new wxMenu
;
259 menuFile
->Append(Keyboard_Clear
, _T("&Clear log\tCtrl-L"));
260 menuFile
->AppendSeparator();
261 menuFile
->Append(Keyboard_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
263 wxMenu
*menuKeys
= new wxMenu
;
264 menuKeys
->AppendCheckItem(Keyboard_Skip
, _T("&Skip key down\tCtrl-S"));
266 // the "About" item should be in the help menu
267 wxMenu
*menuHelp
= new wxMenu
;
268 menuHelp
->Append(Keyboard_About
, _T("&About...\tF1"), _T("Show about dialog"));
270 // now append the freshly created menu to the menu bar...
271 wxMenuBar
*menuBar
= new wxMenuBar();
272 menuBar
->Append(menuFile
, _T("&File"));
273 menuBar
->Append(menuKeys
, _T("&Keys"));
274 menuBar
->Append(menuHelp
, _T("&Help"));
276 // ... and attach this menu bar to the frame
279 menuBar
->Check(Keyboard_Skip
, TRUE
);
280 #endif // wxUSE_MENUS
282 m_winText
= new TextWindow(this);
283 m_lboxLog
= new wxListBox(this, -1);
285 m_logTarget
= new LboxLogger(m_lboxLog
, wxLog::GetActiveTarget());
286 wxLog::SetActiveTarget(m_logTarget
);
289 // create a status bar just for fun (by default with 1 pane only)
291 SetStatusText(_T("Welcome to wxWindows!"));
292 #endif // wxUSE_STATUSBAR
297 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
299 // TRUE is to force the frame to close
303 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
306 msg
.Printf( _T("This is the about dialog of keyboard sample.\n")
307 _T("Welcome to %s"), wxVERSION_STRING
);
309 wxMessageBox(msg
, _T("About Keyboard"), wxOK
| wxICON_INFORMATION
, this);
312 void MyFrame::OnClear(wxCommandEvent
& event
)
317 void MyFrame::OnSkip(wxCommandEvent
& event
)
319 m_winText
->SetSkip(event
.IsChecked());
322 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
326 wxSize size
= GetClientSize();
327 m_winText
->SetSize(0, 0, size
.x
, 50);
328 m_lboxLog
->SetSize(0, 51, size
.x
, size
.y
- 50);
332 // ----------------------------------------------------------------------------
334 // ----------------------------------------------------------------------------
336 void TextWindow::LogEvent(const wxChar
*name
, wxKeyEvent
& event
)
339 long keycode
= event
.KeyCode();
343 case WXK_BACK
: key
= "BACK"; break;
344 case WXK_TAB
: key
= "TAB"; break;
345 case WXK_RETURN
: key
= "RETURN"; break;
346 case WXK_ESCAPE
: key
= "ESCAPE"; break;
347 case WXK_SPACE
: key
= "SPACE"; break;
348 case WXK_DELETE
: key
= "DELETE"; break;
349 case WXK_START
: key
= "START"; break;
350 case WXK_LBUTTON
: key
= "LBUTTON"; break;
351 case WXK_RBUTTON
: key
= "RBUTTON"; break;
352 case WXK_CANCEL
: key
= "CANCEL"; break;
353 case WXK_MBUTTON
: key
= "MBUTTON"; break;
354 case WXK_CLEAR
: key
= "CLEAR"; break;
355 case WXK_SHIFT
: key
= "SHIFT"; break;
356 case WXK_ALT
: key
= "ALT"; break;
357 case WXK_CONTROL
: key
= "CONTROL"; break;
358 case WXK_MENU
: key
= "MENU"; break;
359 case WXK_PAUSE
: key
= "PAUSE"; break;
360 case WXK_CAPITAL
: key
= "CAPITAL"; break;
361 case WXK_PRIOR
: key
= "PRIOR"; break;
362 case WXK_NEXT
: key
= "NEXT"; break;
363 case WXK_END
: key
= "END"; break;
364 case WXK_HOME
: key
= "HOME"; break;
365 case WXK_LEFT
: key
= "LEFT"; break;
366 case WXK_UP
: key
= "UP"; break;
367 case WXK_RIGHT
: key
= "RIGHT"; break;
368 case WXK_DOWN
: key
= "DOWN"; break;
369 case WXK_SELECT
: key
= "SELECT"; break;
370 case WXK_PRINT
: key
= "PRINT"; break;
371 case WXK_EXECUTE
: key
= "EXECUTE"; break;
372 case WXK_SNAPSHOT
: key
= "SNAPSHOT"; break;
373 case WXK_INSERT
: key
= "INSERT"; break;
374 case WXK_HELP
: key
= "HELP"; break;
375 case WXK_NUMPAD0
: key
= "NUMPAD0"; break;
376 case WXK_NUMPAD1
: key
= "NUMPAD1"; break;
377 case WXK_NUMPAD2
: key
= "NUMPAD2"; break;
378 case WXK_NUMPAD3
: key
= "NUMPAD3"; break;
379 case WXK_NUMPAD4
: key
= "NUMPAD4"; break;
380 case WXK_NUMPAD5
: key
= "NUMPAD5"; break;
381 case WXK_NUMPAD6
: key
= "NUMPAD6"; break;
382 case WXK_NUMPAD7
: key
= "NUMPAD7"; break;
383 case WXK_NUMPAD8
: key
= "NUMPAD8"; break;
384 case WXK_NUMPAD9
: key
= "NUMPAD9"; break;
385 case WXK_MULTIPLY
: key
= "MULTIPLY"; break;
386 case WXK_ADD
: key
= "ADD"; break;
387 case WXK_SEPARATOR
: key
= "SEPARATOR"; break;
388 case WXK_SUBTRACT
: key
= "SUBTRACT"; break;
389 case WXK_DECIMAL
: key
= "DECIMAL"; break;
390 case WXK_DIVIDE
: key
= "DIVIDE"; break;
391 case WXK_F1
: key
= "F1"; break;
392 case WXK_F2
: key
= "F2"; break;
393 case WXK_F3
: key
= "F3"; break;
394 case WXK_F4
: key
= "F4"; break;
395 case WXK_F5
: key
= "F5"; break;
396 case WXK_F6
: key
= "F6"; break;
397 case WXK_F7
: key
= "F7"; break;
398 case WXK_F8
: key
= "F8"; break;
399 case WXK_F9
: key
= "F9"; break;
400 case WXK_F10
: key
= "F10"; break;
401 case WXK_F11
: key
= "F11"; break;
402 case WXK_F12
: key
= "F12"; break;
403 case WXK_F13
: key
= "F13"; break;
404 case WXK_F14
: key
= "F14"; break;
405 case WXK_F15
: key
= "F15"; break;
406 case WXK_F16
: key
= "F16"; break;
407 case WXK_F17
: key
= "F17"; break;
408 case WXK_F18
: key
= "F18"; break;
409 case WXK_F19
: key
= "F19"; break;
410 case WXK_F20
: key
= "F20"; break;
411 case WXK_F21
: key
= "F21"; break;
412 case WXK_F22
: key
= "F22"; break;
413 case WXK_F23
: key
= "F23"; break;
414 case WXK_F24
: key
= "F24"; break;
415 case WXK_NUMLOCK
: key
= "NUMLOCK"; break;
416 case WXK_SCROLL
: key
= "SCROLL"; break;
417 case WXK_PAGEUP
: key
= "PAGEUP"; break;
418 case WXK_PAGEDOWN
: key
= "PAGEDOWN"; break;
419 case WXK_NUMPAD_SPACE
: key
= "NUMPAD_SPACE"; break;
420 case WXK_NUMPAD_TAB
: key
= "NUMPAD_TAB"; break;
421 case WXK_NUMPAD_ENTER
: key
= "NUMPAD_ENTER"; break;
422 case WXK_NUMPAD_F1
: key
= "NUMPAD_F1"; break;
423 case WXK_NUMPAD_F2
: key
= "NUMPAD_F2"; break;
424 case WXK_NUMPAD_F3
: key
= "NUMPAD_F3"; break;
425 case WXK_NUMPAD_F4
: key
= "NUMPAD_F4"; break;
426 case WXK_NUMPAD_HOME
: key
= "NUMPAD_HOME"; break;
427 case WXK_NUMPAD_LEFT
: key
= "NUMPAD_LEFT"; break;
428 case WXK_NUMPAD_UP
: key
= "NUMPAD_UP"; break;
429 case WXK_NUMPAD_RIGHT
: key
= "NUMPAD_RIGHT"; break;
430 case WXK_NUMPAD_DOWN
: key
= "NUMPAD_DOWN"; break;
431 case WXK_NUMPAD_PRIOR
: key
= "NUMPAD_PRIOR"; break;
432 case WXK_NUMPAD_PAGEUP
: key
= "NUMPAD_PAGEUP"; break;
433 case WXK_NUMPAD_PAGEDOWN
: key
= "NUMPAD_PAGEDOWN"; break;
434 case WXK_NUMPAD_END
: key
= "NUMPAD_END"; break;
435 case WXK_NUMPAD_BEGIN
: key
= "NUMPAD_BEGIN"; break;
436 case WXK_NUMPAD_INSERT
: key
= "NUMPAD_INSERT"; break;
437 case WXK_NUMPAD_DELETE
: key
= "NUMPAD_DELETE"; break;
438 case WXK_NUMPAD_EQUAL
: key
= "NUMPAD_EQUAL"; break;
439 case WXK_NUMPAD_MULTIPLY
: key
= "NUMPAD_MULTIPLY"; break;
440 case WXK_NUMPAD_ADD
: key
= "NUMPAD_ADD"; break;
441 case WXK_NUMPAD_SEPARATOR
: key
= "NUMPAD_SEPARATOR"; break;
442 case WXK_NUMPAD_SUBTRACT
: key
= "NUMPAD_SUBTRACT"; break;
443 case WXK_NUMPAD_DECIMAL
: key
= "NUMPAD_DECIMAL"; break;
447 if ( wxIsprint((int)keycode
) )
448 key
.Printf(_T("'%c'"), (char)keycode
);
449 else if ( keycode
> 0 && keycode
< 27 )
450 key
.Printf(_("Ctrl-%c"), _T('A') + keycode
- 1);
452 key
.Printf(_T("unknown (%ld)"), keycode
);
457 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
460 GetChar( event
.ControlDown(), _T('C') ),
461 GetChar( event
.AltDown(), _T('A') ),
462 GetChar( event
.ShiftDown(), _T('S') ),
463 GetChar( event
.MetaDown(), _T('M') ) );