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
);
35 void OnClear(wxCommandEvent
& WXUNUSED(event
)) { m_logText
->Clear(); }
36 void OnSkip(wxCommandEvent
& event
) { m_skip
= event
.IsChecked(); }
37 void OnKeyDown(wxKeyEvent
& event
) { LogEvent("KeyDown", event
); }
38 void OnKeyUp(wxKeyEvent
& event
) { LogEvent("KeyUp", event
); }
39 void OnChar(wxKeyEvent
& event
) { LogEvent("Char", event
); }
40 void OnPaintInputWin(wxPaintEvent
& event
);
42 void LogEvent(const wxString
& name
, wxKeyEvent
& event
);
44 wxTextCtrl
*m_logText
;
50 // Define a new application type, each program should derive a class from wxApp
51 class MyApp
: public wxApp
54 // 'Main program' equivalent: the program execution "starts" here
57 // create the main application window
58 new MyFrame("Keyboard wxWidgets App");
60 // If we returned false here, the application would exit immediately.
65 // Create a new application object: this macro will allow wxWidgets to create
66 // the application object during program execution (it's better than using a
67 // static object for many reasons) and also declares the accessor function
68 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
73 // ============================================================================
75 // ============================================================================
78 MyFrame::MyFrame(const wxString
& title
)
79 : wxFrame(NULL
, wxID_ANY
, title
),
83 SetIcon(wxICON(sample
));
94 wxMenu
*menuFile
= new wxMenu
;
96 menuFile
->Append(ClearID
, "&Clear log\tCtrl-L");
97 menuFile
->AppendSeparator();
99 menuFile
->AppendCheckItem(SkipID
, "Call event.&Skip()\tCtrl-S");
100 menuFile
->Check(SkipID
, true);
101 menuFile
->AppendSeparator();
103 menuFile
->Append(QuitID
, "E&xit\tAlt-X", "Quit this program");
105 // the "About" item should be in the help menu
106 wxMenu
*menuHelp
= new wxMenu
;
107 menuHelp
->Append(wxID_ABOUT
, "&About...\tF1", "Show about dialog");
109 // now append the freshly created menu to the menu bar...
110 wxMenuBar
*menuBar
= new wxMenuBar();
111 menuBar
->Append(menuFile
, "&File");
112 menuBar
->Append(menuHelp
, "&Help");
114 // ... and attach this menu bar to the frame
117 m_inputWin
= new wxWindow(this, wxID_ANY
, wxDefaultPosition
, wxSize(-1, 50),
119 m_inputWin
->SetBackgroundColour(*wxBLUE
);
121 wxTextCtrl
*headerText
= new wxTextCtrl(this, wxID_ANY
, "",
122 wxDefaultPosition
, wxDefaultSize
,
124 headerText
->SetValue(
125 " event key KeyCode mod UnicodeKey "
126 " RawKeyCode RawKeyFlags");
129 m_logText
= new wxTextCtrl(this, wxID_ANY
, "",
130 wxDefaultPosition
, wxDefaultSize
,
131 wxTE_MULTILINE
|wxTE_READONLY
|wxHSCROLL
);
133 // set monospace font to have output in nice columns
134 wxFont
font(10, wxFONTFAMILY_TELETYPE
,
135 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
);
136 headerText
->SetFont(font
);
137 m_logText
->SetFont(font
);
140 wxBoxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
141 sizer
->Add(m_inputWin
, wxSizerFlags().Expand());
142 sizer
->Add(headerText
, wxSizerFlags().Expand());
143 sizer
->Add(m_logText
, wxSizerFlags(1).Expand());
144 SetSizerAndFit(sizer
);
146 // set size and position on screen
150 // connect menu event handlers
152 Connect(QuitID
, wxEVT_COMMAND_MENU_SELECTED
,
153 wxCommandEventHandler(MyFrame::OnQuit
));
155 Connect(wxID_ABOUT
, wxEVT_COMMAND_MENU_SELECTED
,
156 wxCommandEventHandler(MyFrame::OnAbout
));
158 Connect(ClearID
, wxEVT_COMMAND_MENU_SELECTED
,
159 wxCommandEventHandler(MyFrame::OnClear
));
161 Connect(SkipID
, wxEVT_COMMAND_MENU_SELECTED
,
162 wxCommandEventHandler(MyFrame::OnSkip
));
164 // connect event handlers for the blue input window
165 m_inputWin
->Connect(wxEVT_KEY_DOWN
, wxKeyEventHandler(MyFrame::OnKeyDown
),
167 m_inputWin
->Connect(wxEVT_KEY_UP
, wxKeyEventHandler(MyFrame::OnKeyUp
),
169 m_inputWin
->Connect(wxEVT_CHAR
, wxKeyEventHandler(MyFrame::OnChar
),
171 m_inputWin
->Connect(wxEVT_PAINT
,
172 wxPaintEventHandler(MyFrame::OnPaintInputWin
),
175 // and show itself (the frames, unlike simple controls, are not shown when
176 // created initially)
182 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
184 wxMessageBox("Demonstrates keyboard event processing in wxWidgets\n"
185 "(c) 2002 Vadim Zeitlin\n"
186 "(c) 2008 Marcin Wojdyr",
187 "About wxWidgets Keyboard Sample",
188 wxOK
| wxICON_INFORMATION
, this);
191 void MyFrame::OnPaintInputWin(wxPaintEvent
& WXUNUSED(event
))
193 wxPaintDC
dc(m_inputWin
);
194 dc
.SetTextForeground(*wxWHITE
);
195 wxFont
font(*wxSWISS_FONT
);
196 font
.SetWeight(wxFONTWEIGHT_BOLD
);
197 font
.SetPointSize(font
.GetPointSize() + 2);
200 dc
.DrawLabel("Press keys here",
201 m_inputWin
->GetClientRect(), wxALIGN_CENTER
);
205 // helper function that returns textual description of wx virtual keycode
206 const char* GetVirtualKeyCodeName(int keycode
)
211 case WXK_##x: return #x;
300 WXK_(NUMPAD_PAGEDOWN
)
306 WXK_(NUMPAD_MULTIPLY
)
308 WXK_(NUMPAD_SEPARATOR
)
309 WXK_(NUMPAD_SUBTRACT
)
318 // helper function that returns textual description of key in the event
319 wxString
GetKeyName(const wxKeyEvent
&event
)
321 int keycode
= event
.GetKeyCode();
322 const char* virt
= GetVirtualKeyCodeName(keycode
);
325 if ( keycode
> 0 && keycode
< 27 )
326 return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode
- 1));
327 if ( keycode
>= 27 && keycode
< 128 )
328 return wxString::Format("'%c'", (unsigned char)keycode
);
330 return wxString::Format("'%c'", event
.GetUnicodeKey());
337 void MyFrame::LogEvent(const wxString
& name
, wxKeyEvent
& event
)
340 // event key_name KeyCode modifiers Unicode raw_code raw_flags
341 msg
.Printf("%7s %15s %5d %c%c%c%c"
347 #ifdef wxHAS_RAW_KEY_CODES
356 event
.ControlDown() ? 'C' : '-',
357 event
.AltDown() ? 'A' : '-',
358 event
.ShiftDown() ? 'S' : '-',
359 event
.MetaDown() ? 'M' : '-'
361 , event
.GetUnicodeKey()
362 , event
.GetUnicodeKey()
364 #ifdef wxHAS_RAW_KEY_CODES
365 , (unsigned long) event
.GetRawKeyCode()
366 , (unsigned long) event
.GetRawKeyFlags()
370 m_logText
->AppendText(msg
);