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 OnSkip(wxCommandEvent
& event
) { m_skip
= event
.IsChecked(); }
46 void OnKeyDown(wxKeyEvent
& event
) { LogEvent("KeyDown", event
); }
47 void OnKeyUp(wxKeyEvent
& event
) { LogEvent("KeyUp", event
); }
48 void OnChar(wxKeyEvent
& event
) { LogEvent("Char", event
); }
49 void OnPaintInputWin(wxPaintEvent
& event
);
51 void LogEvent(const wxString
& name
, wxKeyEvent
& event
);
53 wxTextCtrl
*m_logText
;
59 // Define a new application type, each program should derive a class from wxApp
60 class MyApp
: public wxApp
63 // 'Main program' equivalent: the program execution "starts" here
66 // create the main application window
67 new MyFrame("Keyboard wxWidgets App");
69 // If we returned false here, the application would exit immediately.
74 // Create a new application object: this macro will allow wxWidgets to create
75 // the application object during program execution (it's better than using a
76 // static object for many reasons) and also declares the accessor function
77 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
82 // ============================================================================
84 // ============================================================================
87 MyFrame::MyFrame(const wxString
& title
)
88 : wxFrame(NULL
, wxID_ANY
, title
),
92 SetIcon(wxICON(sample
));
106 wxMenu
*menuFile
= new wxMenu
;
108 menuFile
->Append(ClearID
, "&Clear log\tCtrl-L");
109 menuFile
->AppendSeparator();
111 menuFile
->Append(TestAccelA
, "Test accelerator &1\tA");
112 menuFile
->Append(TestAccelCtrlA
, "Test accelerator &2\tCtrl-A");
113 menuFile
->Append(TestAccelEsc
, "Test accelerator &3\tEsc");
114 menuFile
->AppendSeparator();
116 menuFile
->AppendCheckItem(SkipID
, "Call event.&Skip()\tCtrl-S");
117 menuFile
->Check(SkipID
, true);
118 menuFile
->AppendSeparator();
120 menuFile
->Append(QuitID
, "E&xit\tAlt-X", "Quit this program");
122 // the "About" item should be in the help menu
123 wxMenu
*menuHelp
= new wxMenu
;
124 menuHelp
->Append(wxID_ABOUT
, "&About...\tF1", "Show about dialog");
126 // now append the freshly created menu to the menu bar...
127 wxMenuBar
*menuBar
= new wxMenuBar();
128 menuBar
->Append(menuFile
, "&File");
129 menuBar
->Append(menuHelp
, "&Help");
131 // ... and attach this menu bar to the frame
134 m_inputWin
= new wxWindow(this, wxID_ANY
, wxDefaultPosition
, wxSize(-1, 50),
136 m_inputWin
->SetBackgroundColour(*wxBLUE
);
138 wxTextCtrl
*headerText
= new wxTextCtrl(this, wxID_ANY
, "",
139 wxDefaultPosition
, wxDefaultSize
,
141 headerText
->SetValue(
142 " event key KeyCode mod UnicodeKey "
143 " RawKeyCode RawKeyFlags");
146 m_logText
= new wxTextCtrl(this, wxID_ANY
, "",
147 wxDefaultPosition
, wxDefaultSize
,
148 wxTE_MULTILINE
|wxTE_READONLY
|wxHSCROLL
);
150 // set monospace font to have output in nice columns
151 wxFont
font(10, wxFONTFAMILY_TELETYPE
,
152 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
);
153 headerText
->SetFont(font
);
154 m_logText
->SetFont(font
);
157 wxBoxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
158 sizer
->Add(m_inputWin
, wxSizerFlags().Expand());
159 sizer
->Add(headerText
, wxSizerFlags().Expand());
160 sizer
->Add(m_logText
, wxSizerFlags(1).Expand());
161 SetSizerAndFit(sizer
);
163 // set size and position on screen
167 // connect menu event handlers
169 Connect(QuitID
, wxEVT_COMMAND_MENU_SELECTED
,
170 wxCommandEventHandler(MyFrame::OnQuit
));
172 Connect(wxID_ABOUT
, wxEVT_COMMAND_MENU_SELECTED
,
173 wxCommandEventHandler(MyFrame::OnAbout
));
175 Connect(ClearID
, wxEVT_COMMAND_MENU_SELECTED
,
176 wxCommandEventHandler(MyFrame::OnClear
));
178 Connect(SkipID
, wxEVT_COMMAND_MENU_SELECTED
,
179 wxCommandEventHandler(MyFrame::OnSkip
));
181 Connect(TestAccelA
, wxEVT_COMMAND_MENU_SELECTED
,
182 wxCommandEventHandler(MyFrame::OnTestAccelA
));
184 Connect(TestAccelCtrlA
, wxEVT_COMMAND_MENU_SELECTED
,
185 wxCommandEventHandler(MyFrame::OnTestAccelCtrlA
));
187 Connect(TestAccelEsc
, wxEVT_COMMAND_MENU_SELECTED
,
188 wxCommandEventHandler(MyFrame::OnTestAccelEsc
));
190 // connect event handlers for the blue input window
191 m_inputWin
->Connect(wxEVT_KEY_DOWN
, wxKeyEventHandler(MyFrame::OnKeyDown
),
193 m_inputWin
->Connect(wxEVT_KEY_UP
, wxKeyEventHandler(MyFrame::OnKeyUp
),
195 m_inputWin
->Connect(wxEVT_CHAR
, wxKeyEventHandler(MyFrame::OnChar
),
197 m_inputWin
->Connect(wxEVT_PAINT
,
198 wxPaintEventHandler(MyFrame::OnPaintInputWin
),
201 // and show itself (the frames, unlike simple controls, are not shown when
202 // created initially)
208 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
210 wxMessageBox("Demonstrates keyboard event processing in wxWidgets\n"
211 "(c) 2002 Vadim Zeitlin\n"
212 "(c) 2008 Marcin Wojdyr",
213 "About wxWidgets Keyboard Sample",
214 wxOK
| wxICON_INFORMATION
, this);
217 void MyFrame::OnPaintInputWin(wxPaintEvent
& WXUNUSED(event
))
219 wxPaintDC
dc(m_inputWin
);
220 dc
.SetTextForeground(*wxWHITE
);
221 wxFont
font(*wxSWISS_FONT
);
222 font
.SetWeight(wxFONTWEIGHT_BOLD
);
223 font
.SetPointSize(font
.GetPointSize() + 2);
226 dc
.DrawLabel("Press keys here",
227 m_inputWin
->GetClientRect(), wxALIGN_CENTER
);
231 // helper function that returns textual description of wx virtual keycode
232 const char* GetVirtualKeyCodeName(int keycode
)
237 case WXK_##x: return #x;
326 WXK_(NUMPAD_PAGEDOWN
)
332 WXK_(NUMPAD_MULTIPLY
)
334 WXK_(NUMPAD_SEPARATOR
)
335 WXK_(NUMPAD_SUBTRACT
)
344 // helper function that returns textual description of key in the event
345 wxString
GetKeyName(const wxKeyEvent
&event
)
347 int keycode
= event
.GetKeyCode();
348 const char* virt
= GetVirtualKeyCodeName(keycode
);
351 if ( keycode
> 0 && keycode
< 27 )
352 return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode
- 1));
353 if ( keycode
>= 27 && keycode
< 128 )
354 return wxString::Format("'%c'", (unsigned char)keycode
);
356 return wxString::Format("'%c'", event
.GetUnicodeKey());
363 void MyFrame::LogEvent(const wxString
& name
, wxKeyEvent
& event
)
366 // event key_name KeyCode modifiers Unicode raw_code raw_flags
367 msg
.Printf("%7s %15s %5d %c%c%c%c"
373 #ifdef wxHAS_RAW_KEY_CODES
382 event
.ControlDown() ? 'C' : '-',
383 event
.AltDown() ? 'A' : '-',
384 event
.ShiftDown() ? 'S' : '-',
385 event
.MetaDown() ? 'M' : '-'
387 , event
.GetUnicodeKey()
388 , event
.GetUnicodeKey()
390 #ifdef wxHAS_RAW_KEY_CODES
391 , (unsigned long) event
.GetRawKeyCode()
392 , (unsigned long) event
.GetRawKeyFlags()
396 m_logText
->AppendText(msg
);