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"
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 LogEvent("Hook", event
);
62 void OnPaintInputWin(wxPaintEvent
& event
);
64 void LogEvent(const wxString
& name
, wxKeyEvent
& event
);
66 wxTextCtrl
*m_logText
;
73 // Define a new application type, each program should derive a class from wxApp
74 class MyApp
: public wxApp
77 // 'Main program' equivalent: the program execution "starts" here
80 // create the main application window
81 new MyFrame("Keyboard wxWidgets App");
83 // If we returned false here, the application would exit immediately.
88 // Create a new application object: this macro will allow wxWidgets to create
89 // the application object during program execution (it's better than using a
90 // static object for many reasons) and also declares the accessor function
91 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
96 // ============================================================================
98 // ============================================================================
101 MyFrame::MyFrame(const wxString
& title
)
102 : wxFrame(NULL
, wxID_ANY
, title
),
107 SetIcon(wxICON(sample
));
109 // IDs for menu items
113 ClearID
= wxID_CLEAR
,
122 wxMenu
*menuFile
= new wxMenu
;
124 menuFile
->Append(ClearID
, "&Clear log\tCtrl-L");
125 menuFile
->AppendSeparator();
127 menuFile
->Append(TestAccelA
, "Test accelerator &1\tA");
128 menuFile
->Append(TestAccelCtrlA
, "Test accelerator &2\tCtrl-A");
129 menuFile
->Append(TestAccelEsc
, "Test accelerator &3\tEsc");
130 menuFile
->AppendSeparator();
132 menuFile
->AppendCheckItem(SkipHook
, "Skip CHAR_HOOK event",
133 "Not skipping this event disables both KEY_DOWN and CHAR events"
135 menuFile
->Check(SkipHook
, true);
136 menuFile
->AppendCheckItem(SkipDown
, "Skip KEY_DOWN event",
137 "Not skipping this event disables CHAR event generation"
139 menuFile
->Check(SkipDown
, true);
140 menuFile
->AppendSeparator();
142 menuFile
->Append(QuitID
, "E&xit\tAlt-X", "Quit this program");
144 // the "About" item should be in the help menu
145 wxMenu
*menuHelp
= new wxMenu
;
146 menuHelp
->Append(wxID_ABOUT
, "&About\tF1", "Show about dialog");
148 // now append the freshly created menu to the menu bar...
149 wxMenuBar
*menuBar
= new wxMenuBar();
150 menuBar
->Append(menuFile
, "&File");
151 menuBar
->Append(menuHelp
, "&Help");
153 // ... and attach this menu bar to the frame
156 m_inputWin
= new wxWindow(this, wxID_ANY
, wxDefaultPosition
, wxSize(-1, 50),
158 m_inputWin
->SetBackgroundColour(*wxBLUE
);
160 wxTextCtrl
*headerText
= new wxTextCtrl(this, wxID_ANY
, "",
161 wxDefaultPosition
, wxDefaultSize
,
163 headerText
->SetValue(
164 " event key KeyCode mod UnicodeKey "
165 " RawKeyCode RawKeyFlags");
168 m_logText
= new wxTextCtrl(this, wxID_ANY
, "",
169 wxDefaultPosition
, wxDefaultSize
,
170 wxTE_MULTILINE
|wxTE_READONLY
|wxHSCROLL
);
172 // set monospace font to have output in nice columns
173 wxFont
font(10, wxFONTFAMILY_TELETYPE
,
174 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
);
175 headerText
->SetFont(font
);
176 m_logText
->SetFont(font
);
179 wxBoxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
180 sizer
->Add(m_inputWin
, wxSizerFlags().Expand());
181 sizer
->Add(headerText
, wxSizerFlags().Expand());
182 sizer
->Add(m_logText
, wxSizerFlags(1).Expand());
183 SetSizerAndFit(sizer
);
185 // set size and position on screen
189 // connect menu event handlers
191 Connect(QuitID
, wxEVT_COMMAND_MENU_SELECTED
,
192 wxCommandEventHandler(MyFrame::OnQuit
));
194 Connect(wxID_ABOUT
, wxEVT_COMMAND_MENU_SELECTED
,
195 wxCommandEventHandler(MyFrame::OnAbout
));
197 Connect(ClearID
, wxEVT_COMMAND_MENU_SELECTED
,
198 wxCommandEventHandler(MyFrame::OnClear
));
200 Connect(SkipHook
, wxEVT_COMMAND_MENU_SELECTED
,
201 wxCommandEventHandler(MyFrame::OnSkipHook
));
202 Connect(SkipDown
, wxEVT_COMMAND_MENU_SELECTED
,
203 wxCommandEventHandler(MyFrame::OnSkipDown
));
205 Connect(TestAccelA
, wxEVT_COMMAND_MENU_SELECTED
,
206 wxCommandEventHandler(MyFrame::OnTestAccelA
));
208 Connect(TestAccelCtrlA
, wxEVT_COMMAND_MENU_SELECTED
,
209 wxCommandEventHandler(MyFrame::OnTestAccelCtrlA
));
211 Connect(TestAccelEsc
, wxEVT_COMMAND_MENU_SELECTED
,
212 wxCommandEventHandler(MyFrame::OnTestAccelEsc
));
214 // connect event handlers for the blue input window
215 m_inputWin
->Connect(wxEVT_KEY_DOWN
, wxKeyEventHandler(MyFrame::OnKeyDown
),
217 m_inputWin
->Connect(wxEVT_KEY_UP
, wxKeyEventHandler(MyFrame::OnKeyUp
),
219 m_inputWin
->Connect(wxEVT_CHAR
, wxKeyEventHandler(MyFrame::OnChar
),
221 m_inputWin
->Connect(wxEVT_PAINT
,
222 wxPaintEventHandler(MyFrame::OnPaintInputWin
),
225 // notice that we don't connect OnCharHook() to the input window, unlike
226 // the usual key events this one is propagated upwards
227 Connect(wxEVT_CHAR_HOOK
, wxKeyEventHandler(MyFrame::OnCharHook
));
229 // status bar is useful for showing the menu items help strings
232 // and show itself (the frames, unlike simple controls, are not shown when
233 // created initially)
239 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
241 wxMessageBox("Demonstrates keyboard event processing in wxWidgets\n"
242 "(c) 2002 Vadim Zeitlin\n"
243 "(c) 2008 Marcin Wojdyr",
244 "About wxWidgets Keyboard Sample",
245 wxOK
| wxICON_INFORMATION
, this);
248 void MyFrame::OnPaintInputWin(wxPaintEvent
& WXUNUSED(event
))
250 wxPaintDC
dc(m_inputWin
);
251 dc
.SetTextForeground(*wxWHITE
);
252 wxFont
font(*wxSWISS_FONT
);
253 font
.SetWeight(wxFONTWEIGHT_BOLD
);
254 font
.SetPointSize(font
.GetPointSize() + 2);
257 dc
.DrawLabel("Press keys here",
258 m_inputWin
->GetClientRect(), wxALIGN_CENTER
);
262 // helper function that returns textual description of wx virtual keycode
263 const char* GetVirtualKeyCodeName(int keycode
)
268 case WXK_##x: return #x;
357 WXK_(NUMPAD_PAGEDOWN
)
363 WXK_(NUMPAD_MULTIPLY
)
365 WXK_(NUMPAD_SEPARATOR
)
366 WXK_(NUMPAD_SUBTRACT
)
382 // helper function that returns textual description of key in the event
383 wxString
GetKeyName(const wxKeyEvent
&event
)
385 int keycode
= event
.GetKeyCode();
386 const char* virt
= GetVirtualKeyCodeName(keycode
);
389 if ( keycode
> 0 && keycode
< 32 )
390 return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode
- 1));
391 if ( keycode
>= 32 && keycode
< 128 )
392 return wxString::Format("'%c'", (unsigned char)keycode
);
395 int uc
= event
.GetUnicodeKey();
396 if ( uc
!= WXK_NONE
)
397 return wxString::Format("'%c'", uc
);
404 void MyFrame::LogEvent(const wxString
& name
, wxKeyEvent
& event
)
407 // event key_name KeyCode modifiers Unicode raw_code raw_flags
408 msg
.Printf("%7s %15s %5d %c%c%c%c"
414 #ifdef wxHAS_RAW_KEY_CODES
423 event
.ControlDown() ? 'C' : '-',
424 event
.AltDown() ? 'A' : '-',
425 event
.ShiftDown() ? 'S' : '-',
426 event
.MetaDown() ? 'M' : '-'
428 , event
.GetUnicodeKey()
429 , event
.GetUnicodeKey()
431 #ifdef wxHAS_RAW_KEY_CODES
432 , (unsigned long) event
.GetRawKeyCode()
433 , (unsigned long) event
.GetRawKeyFlags()
437 m_logText
->AppendText(msg
);