1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Keyboard wxWidgets sample
4 // Author: Vadim Zeitlin
5 // Modified by: Marcin Wojdyr
8 // Copyright: (c) 2002 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
21 #ifndef wxHAS_IMAGES_IN_RESOURCES
22 #include "../sample.xpm"
25 // Define a new frame type: this is going to be our main frame
26 class MyFrame
: public wxFrame
29 MyFrame(const wxString
& title
);
33 void OnQuit(wxCommandEvent
& WXUNUSED(event
)) { Close(true); }
34 void OnAbout(wxCommandEvent
& event
);
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"); }
43 void OnClear(wxCommandEvent
& WXUNUSED(event
)) { m_logText
->Clear(); }
44 void OnSkipDown(wxCommandEvent
& event
) { m_skipDown
= event
.IsChecked(); }
45 void OnSkipHook(wxCommandEvent
& event
) { m_skipHook
= event
.IsChecked(); }
47 void OnKeyDown(wxKeyEvent
& event
)
49 LogEvent("KeyDown", event
);
53 void OnKeyUp(wxKeyEvent
& event
) { LogEvent("KeyUp", event
); }
54 void OnChar(wxKeyEvent
& event
) { LogEvent("Char", event
); }
55 void OnCharHook(wxKeyEvent
& event
)
57 // The logged messages can be confusing if the input window doesn't
58 // have focus so warn about this.
59 if ( !m_inputWin
->HasFocus() )
61 m_logText
->SetDefaultStyle(*wxRED
);
62 m_logText
->AppendText("WARNING: focus is not on input window, "
63 "non-hook events won't be logged.\n");
64 m_logText
->SetDefaultStyle(wxTextAttr());
67 LogEvent("Hook", event
);
72 void OnPaintInputWin(wxPaintEvent
& event
);
74 void LogEvent(const wxString
& name
, wxKeyEvent
& event
);
76 wxTextCtrl
*m_logText
;
83 // Define a new application type, each program should derive a class from wxApp
84 class MyApp
: public wxApp
87 // 'Main program' equivalent: the program execution "starts" here
90 // create the main application window
91 new MyFrame("Keyboard wxWidgets App");
93 // If we returned false here, the application would exit immediately.
98 // Create a new application object: this macro will allow wxWidgets to create
99 // the application object during program execution (it's better than using a
100 // static object for many reasons) and also declares the accessor function
101 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
106 // ============================================================================
108 // ============================================================================
111 MyFrame::MyFrame(const wxString
& title
)
112 : wxFrame(NULL
, wxID_ANY
, title
),
117 SetIcon(wxICON(sample
));
119 // IDs for menu items
123 ClearID
= wxID_CLEAR
,
132 wxMenu
*menuFile
= new wxMenu
;
134 menuFile
->Append(ClearID
, "&Clear log\tCtrl-L");
135 menuFile
->AppendSeparator();
137 menuFile
->Append(TestAccelA
, "Test accelerator &1\tA");
138 menuFile
->Append(TestAccelCtrlA
, "Test accelerator &2\tCtrl-A");
139 menuFile
->Append(TestAccelEsc
, "Test accelerator &3\tEsc");
140 menuFile
->AppendSeparator();
142 menuFile
->AppendCheckItem(SkipHook
, "Skip CHAR_HOOK event",
143 "Not skipping this event disables both KEY_DOWN and CHAR events"
145 menuFile
->Check(SkipHook
, true);
146 menuFile
->AppendCheckItem(SkipDown
, "Skip KEY_DOWN event",
147 "Not skipping this event disables CHAR event generation"
149 menuFile
->Check(SkipDown
, true);
150 menuFile
->AppendSeparator();
152 menuFile
->Append(QuitID
, "E&xit\tAlt-X", "Quit this program");
154 // the "About" item should be in the help menu
155 wxMenu
*menuHelp
= new wxMenu
;
156 menuHelp
->Append(wxID_ABOUT
, "&About\tF1", "Show about dialog");
158 // now append the freshly created menu to the menu bar...
159 wxMenuBar
*menuBar
= new wxMenuBar();
160 menuBar
->Append(menuFile
, "&File");
161 menuBar
->Append(menuHelp
, "&Help");
163 // ... and attach this menu bar to the frame
166 m_inputWin
= new wxWindow(this, wxID_ANY
, wxDefaultPosition
, wxSize(-1, 50),
168 m_inputWin
->SetBackgroundColour(*wxBLUE
);
170 wxTextCtrl
*headerText
= new wxTextCtrl(this, wxID_ANY
, "",
171 wxDefaultPosition
, wxDefaultSize
,
173 headerText
->SetValue(
174 " event key KeyCode mod UnicodeKey "
175 " RawKeyCode RawKeyFlags Position");
178 m_logText
= new wxTextCtrl(this, wxID_ANY
, "",
179 wxDefaultPosition
, wxDefaultSize
,
180 wxTE_MULTILINE
|wxTE_READONLY
|wxTE_RICH
|wxHSCROLL
);
182 // set monospace font to have output in nice columns
183 wxFont
font(10, wxFONTFAMILY_TELETYPE
,
184 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
);
185 headerText
->SetFont(font
);
186 m_logText
->SetFont(font
);
189 wxBoxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
190 sizer
->Add(m_inputWin
, wxSizerFlags().Expand());
191 sizer
->Add(headerText
, wxSizerFlags().Expand());
192 sizer
->Add(m_logText
, wxSizerFlags(1).Expand());
193 SetSizerAndFit(sizer
);
195 // set size and position on screen
199 // connect menu event handlers
201 Connect(QuitID
, wxEVT_COMMAND_MENU_SELECTED
,
202 wxCommandEventHandler(MyFrame::OnQuit
));
204 Connect(wxID_ABOUT
, wxEVT_COMMAND_MENU_SELECTED
,
205 wxCommandEventHandler(MyFrame::OnAbout
));
207 Connect(ClearID
, wxEVT_COMMAND_MENU_SELECTED
,
208 wxCommandEventHandler(MyFrame::OnClear
));
210 Connect(SkipHook
, wxEVT_COMMAND_MENU_SELECTED
,
211 wxCommandEventHandler(MyFrame::OnSkipHook
));
212 Connect(SkipDown
, wxEVT_COMMAND_MENU_SELECTED
,
213 wxCommandEventHandler(MyFrame::OnSkipDown
));
215 Connect(TestAccelA
, wxEVT_COMMAND_MENU_SELECTED
,
216 wxCommandEventHandler(MyFrame::OnTestAccelA
));
218 Connect(TestAccelCtrlA
, wxEVT_COMMAND_MENU_SELECTED
,
219 wxCommandEventHandler(MyFrame::OnTestAccelCtrlA
));
221 Connect(TestAccelEsc
, wxEVT_COMMAND_MENU_SELECTED
,
222 wxCommandEventHandler(MyFrame::OnTestAccelEsc
));
224 // connect event handlers for the blue input window
225 m_inputWin
->Connect(wxEVT_KEY_DOWN
, wxKeyEventHandler(MyFrame::OnKeyDown
),
227 m_inputWin
->Connect(wxEVT_KEY_UP
, wxKeyEventHandler(MyFrame::OnKeyUp
),
229 m_inputWin
->Connect(wxEVT_CHAR
, wxKeyEventHandler(MyFrame::OnChar
),
231 m_inputWin
->Connect(wxEVT_PAINT
,
232 wxPaintEventHandler(MyFrame::OnPaintInputWin
),
235 // notice that we don't connect OnCharHook() to the input window, unlike
236 // the usual key events this one is propagated upwards
237 Connect(wxEVT_CHAR_HOOK
, wxKeyEventHandler(MyFrame::OnCharHook
));
239 // status bar is useful for showing the menu items help strings
242 // and show itself (the frames, unlike simple controls, are not shown when
243 // created initially)
249 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
251 wxMessageBox("Demonstrates keyboard event processing in wxWidgets\n"
252 "(c) 2002 Vadim Zeitlin\n"
253 "(c) 2008 Marcin Wojdyr",
254 "About wxWidgets Keyboard Sample",
255 wxOK
| wxICON_INFORMATION
, this);
258 void MyFrame::OnPaintInputWin(wxPaintEvent
& WXUNUSED(event
))
260 wxPaintDC
dc(m_inputWin
);
261 dc
.SetTextForeground(*wxWHITE
);
262 wxFont
font(*wxSWISS_FONT
);
263 font
.SetWeight(wxFONTWEIGHT_BOLD
);
264 font
.SetPointSize(font
.GetPointSize() + 2);
267 dc
.DrawLabel("Press keys here",
268 m_inputWin
->GetClientRect(), wxALIGN_CENTER
);
272 // helper function that returns textual description of wx virtual keycode
273 const char* GetVirtualKeyCodeName(int keycode
)
278 case WXK_##x: return #x;
367 WXK_(NUMPAD_PAGEDOWN
)
373 WXK_(NUMPAD_MULTIPLY
)
375 WXK_(NUMPAD_SEPARATOR
)
376 WXK_(NUMPAD_SUBTRACT
)
392 // helper function that returns textual description of key in the event
393 wxString
GetKeyName(const wxKeyEvent
&event
)
395 int keycode
= event
.GetKeyCode();
396 const char* virt
= GetVirtualKeyCodeName(keycode
);
399 if ( keycode
> 0 && keycode
< 32 )
400 return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode
- 1));
401 if ( keycode
>= 32 && keycode
< 128 )
402 return wxString::Format("'%c'", (unsigned char)keycode
);
405 int uc
= event
.GetUnicodeKey();
406 if ( uc
!= WXK_NONE
)
407 return wxString::Format("'%c'", uc
);
414 void MyFrame::LogEvent(const wxString
& name
, wxKeyEvent
& event
)
417 // event key_name KeyCode modifiers Unicode raw_code raw_flags pos
418 msg
.Printf("%7s %15s %5d %c%c%c%c"
424 #ifdef wxHAS_RAW_KEY_CODES
434 event
.ControlDown() ? 'C' : '-',
435 event
.AltDown() ? 'A' : '-',
436 event
.ShiftDown() ? 'S' : '-',
437 event
.MetaDown() ? 'M' : '-'
439 , event
.GetUnicodeKey()
440 , event
.GetUnicodeKey()
442 #ifdef wxHAS_RAW_KEY_CODES
443 , (unsigned long) event
.GetRawKeyCode()
444 , (unsigned long) event
.GetRawKeyFlags()
450 m_logText
->AppendText(msg
);