]>
git.saurik.com Git - wxWidgets.git/blob - samples/keyboard/keyboard.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Keyboard wxWidgets sample
4 // Author: Vadim Zeitlin
5 // Modified by: Marcin Wojdyr
7 // Copyright: (c) 2002 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
20 #ifndef wxHAS_IMAGES_IN_RESOURCES
21 #include "../sample.xpm"
32 // These IDs must be in the same order as MyFrame::InputKind enum elements.
42 // Define a new frame type: this is going to be our main frame
43 class MyFrame
: public wxFrame
46 MyFrame(const wxString
& title
);
50 void OnQuit(wxCommandEvent
& WXUNUSED(event
)) { Close(true); }
51 void OnAbout(wxCommandEvent
& event
);
53 void OnInputWindowKind(wxCommandEvent
& event
);
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"); }
62 void OnClear(wxCommandEvent
& WXUNUSED(event
)) { m_logText
->Clear(); }
63 void OnSkipDown(wxCommandEvent
& event
) { m_skipDown
= event
.IsChecked(); }
64 void OnSkipHook(wxCommandEvent
& event
) { m_skipHook
= event
.IsChecked(); }
66 void OnKeyDown(wxKeyEvent
& event
)
68 LogEvent("KeyDown", event
);
72 void OnKeyUp(wxKeyEvent
& event
) { LogEvent("KeyUp", event
); }
73 void OnChar(wxKeyEvent
& event
) { LogEvent("Char", event
); event
.Skip(); }
74 void OnCharHook(wxKeyEvent
& event
)
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() )
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());
86 LogEvent("Hook", event
);
91 void OnPaintInputWin(wxPaintEvent
& event
);
93 void LogEvent(const wxString
& name
, wxKeyEvent
& event
);
95 // Set m_inputWin to either a new window of the given kind:
98 Input_Custom
, // Just a plain wxWindow
99 Input_Entry
, // Single-line wxTextCtrl
100 Input_Text
// Multi-line wxTextCtrl
103 void DoCreateInputWindow(InputKind inputKind
);
105 wxTextCtrl
*m_logText
;
106 wxWindow
*m_inputWin
;
112 // Define a new application type, each program should derive a class from wxApp
113 class MyApp
: public wxApp
116 // 'Main program' equivalent: the program execution "starts" here
117 virtual bool OnInit()
119 // create the main application window
120 new MyFrame("Keyboard wxWidgets App");
122 // If we returned false here, the application would exit immediately.
127 // Create a new application object: this macro will allow wxWidgets to create
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
135 // ============================================================================
137 // ============================================================================
140 MyFrame::MyFrame(const wxString
& title
)
141 : wxFrame(NULL
, wxID_ANY
, title
),
146 SetIcon(wxICON(sample
));
149 wxMenu
*menuFile
= new wxMenu
;
151 menuFile
->Append(ClearID
, "&Clear log\tCtrl-L");
152 menuFile
->AppendSeparator();
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();
159 menuFile
->AppendCheckItem(SkipHook
, "Skip CHAR_HOOK event",
160 "Not skipping this event disables both KEY_DOWN and CHAR events"
162 menuFile
->Check(SkipHook
, true);
163 menuFile
->AppendCheckItem(SkipDown
, "Skip KEY_DOWN event",
164 "Not skipping this event disables CHAR event generation"
166 menuFile
->Check(SkipDown
, true);
167 menuFile
->AppendSeparator();
169 menuFile
->AppendRadioItem(IDInputCustom
, "Use &custom control\tCtrl-C",
170 "Use custom wxWindow for input window"
172 menuFile
->AppendRadioItem(IDInputEntry
, "Use text &entry\tCtrl-E",
173 "Use single-line wxTextCtrl for input window"
175 menuFile
->AppendRadioItem(IDInputText
, "Use &text control\tCtrl-T",
176 "Use multi-line wxTextCtrl for input window"
178 menuFile
->AppendSeparator();
180 menuFile
->Append(QuitID
, "E&xit\tAlt-X", "Quit this program");
182 // the "About" item should be in the help menu
183 wxMenu
*menuHelp
= new wxMenu
;
184 menuHelp
->Append(wxID_ABOUT
, "&About\tF1", "Show about dialog");
186 // now append the freshly created menu to the menu bar...
187 wxMenuBar
*menuBar
= new wxMenuBar();
188 menuBar
->Append(menuFile
, "&File");
189 menuBar
->Append(menuHelp
, "&Help");
191 // ... and attach this menu bar to the frame
194 DoCreateInputWindow(Input_Custom
);
196 wxTextCtrl
*headerText
= new wxTextCtrl(this, wxID_ANY
, "",
197 wxDefaultPosition
, wxDefaultSize
,
199 headerText
->SetValue(
200 " event key KeyCode mod UnicodeKey "
201 " RawKeyCode RawKeyFlags Position");
204 m_logText
= new wxTextCtrl(this, wxID_ANY
, "",
205 wxDefaultPosition
, wxDefaultSize
,
206 wxTE_MULTILINE
|wxTE_READONLY
|wxTE_RICH
|wxHSCROLL
);
208 // set monospace font to have output in nice columns
209 wxFont
font(10, wxFONTFAMILY_TELETYPE
,
210 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
);
211 headerText
->SetFont(font
);
212 m_logText
->SetFont(font
);
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
);
221 // set size and position on screen
225 // connect menu event handlers
227 Connect(QuitID
, wxEVT_MENU
,
228 wxCommandEventHandler(MyFrame::OnQuit
));
230 Connect(wxID_ABOUT
, wxEVT_MENU
,
231 wxCommandEventHandler(MyFrame::OnAbout
));
233 Connect(ClearID
, wxEVT_MENU
,
234 wxCommandEventHandler(MyFrame::OnClear
));
236 Connect(SkipHook
, wxEVT_MENU
,
237 wxCommandEventHandler(MyFrame::OnSkipHook
));
238 Connect(SkipDown
, wxEVT_MENU
,
239 wxCommandEventHandler(MyFrame::OnSkipDown
));
241 Connect(IDInputCustom
, IDInputText
, wxEVT_MENU
,
242 wxCommandEventHandler(MyFrame::OnInputWindowKind
));
244 Connect(TestAccelA
, wxEVT_MENU
,
245 wxCommandEventHandler(MyFrame::OnTestAccelA
));
247 Connect(TestAccelCtrlA
, wxEVT_MENU
,
248 wxCommandEventHandler(MyFrame::OnTestAccelCtrlA
));
250 Connect(TestAccelEsc
, wxEVT_MENU
,
251 wxCommandEventHandler(MyFrame::OnTestAccelEsc
));
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
));
257 // status bar is useful for showing the menu items help strings
260 // and show itself (the frames, unlike simple controls, are not shown when
261 // created initially)
267 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
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);
276 void MyFrame::DoCreateInputWindow(InputKind inputKind
)
278 wxWindow
* const oldWin
= m_inputWin
;
283 m_inputWin
= new wxWindow(this, wxID_ANY
,
284 wxDefaultPosition
, wxSize(-1, 50),
289 m_inputWin
= new wxTextCtrl(this, wxID_ANY
, "Press keys here");
293 m_inputWin
= new wxTextCtrl(this, wxID_ANY
, "Press keys here",
294 wxDefaultPosition
, wxSize(-1, 50),
299 m_inputWin
->SetBackgroundColour(*wxBLUE
);
300 m_inputWin
->SetForegroundColour(*wxWHITE
);
302 // connect event handlers for the blue input window
303 m_inputWin
->Connect(wxEVT_KEY_DOWN
, wxKeyEventHandler(MyFrame::OnKeyDown
),
305 m_inputWin
->Connect(wxEVT_KEY_UP
, wxKeyEventHandler(MyFrame::OnKeyUp
),
307 m_inputWin
->Connect(wxEVT_CHAR
, wxKeyEventHandler(MyFrame::OnChar
),
310 if ( inputKind
== Input_Custom
)
312 m_inputWin
->Connect(wxEVT_PAINT
,
313 wxPaintEventHandler(MyFrame::OnPaintInputWin
),
319 GetSizer()->Replace(oldWin
, m_inputWin
);
325 void MyFrame::OnInputWindowKind(wxCommandEvent
& event
)
328 static_cast<InputKind
>(event
.GetId() - IDInputCustom
)
332 void MyFrame::OnPaintInputWin(wxPaintEvent
& WXUNUSED(event
))
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);
341 dc
.DrawLabel("Press keys here",
342 m_inputWin
->GetClientRect(), wxALIGN_CENTER
);
346 // helper function that returns textual description of wx virtual keycode
347 const char* GetVirtualKeyCodeName(int keycode
)
352 case WXK_##x: return #x;
441 WXK_(NUMPAD_PAGEDOWN
)
447 WXK_(NUMPAD_MULTIPLY
)
449 WXK_(NUMPAD_SEPARATOR
)
450 WXK_(NUMPAD_SUBTRACT
)
466 // helper function that returns textual description of key in the event
467 wxString
GetKeyName(const wxKeyEvent
&event
)
469 int keycode
= event
.GetKeyCode();
470 const char* virt
= GetVirtualKeyCodeName(keycode
);
473 if ( keycode
> 0 && keycode
< 32 )
474 return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode
- 1));
475 if ( keycode
>= 32 && keycode
< 128 )
476 return wxString::Format("'%c'", (unsigned char)keycode
);
479 int uc
= event
.GetUnicodeKey();
480 if ( uc
!= WXK_NONE
)
481 return wxString::Format("'%c'", uc
);
488 void MyFrame::LogEvent(const wxString
& name
, wxKeyEvent
& event
)
491 // event key_name KeyCode modifiers Unicode raw_code raw_flags pos
492 msg
.Printf("%7s %15s %5d %c%c%c%c"
498 #ifdef wxHAS_RAW_KEY_CODES
508 event
.ControlDown() ? 'C' : '-',
509 event
.AltDown() ? 'A' : '-',
510 event
.ShiftDown() ? 'S' : '-',
511 event
.MetaDown() ? 'M' : '-'
513 , event
.GetUnicodeKey()
514 , event
.GetUnicodeKey()
516 #ifdef wxHAS_RAW_KEY_CODES
517 , (unsigned long) event
.GetRawKeyCode()
518 , (unsigned long) event
.GetRawKeyFlags()
524 m_logText
->AppendText(msg
);