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 // Define a new frame type: this is going to be our main frame
23 class MyFrame
: public wxFrame
26 MyFrame(const wxString
& title
);
30 void OnQuit(wxCommandEvent
& WXUNUSED(event
)) { Close(true); }
31 void OnAbout(wxCommandEvent
& event
);
32 void OnClear(wxCommandEvent
& WXUNUSED(event
)) { m_logText
->Clear(); }
33 void OnSkip(wxCommandEvent
& event
) { m_skip
= event
.IsChecked(); }
34 void OnKeyDown(wxKeyEvent
& event
) { LogEvent("KeyDown", event
); }
35 void OnKeyUp(wxKeyEvent
& event
) { LogEvent("KeyUp", event
); }
36 void OnChar(wxKeyEvent
& event
) { LogEvent("Char", event
); }
37 void OnPaintInputWin(wxPaintEvent
& event
);
39 void LogEvent(const wxString
& name
, wxKeyEvent
& event
);
41 wxTextCtrl
*m_logText
;
47 // Define a new application type, each program should derive a class from wxApp
48 class MyApp
: public wxApp
51 // 'Main program' equivalent: the program execution "starts" here
54 // create the main application window
55 new MyFrame("Keyboard wxWidgets App");
57 // If we returned false here, the application would exit immediately.
62 // Create a new application object: this macro will allow wxWidgets to create
63 // the application object during program execution (it's better than using a
64 // static object for many reasons) and also declares the accessor function
65 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
70 // ============================================================================
72 // ============================================================================
75 MyFrame::MyFrame(const wxString
& title
)
76 : wxFrame(NULL
, wxID_ANY
, title
),
89 wxMenu
*menuFile
= new wxMenu
;
91 menuFile
->Append(ClearID
, "&Clear log\tCtrl-L");
92 menuFile
->AppendSeparator();
94 menuFile
->AppendCheckItem(SkipID
, "Call event.&Skip()\tCtrl-S");
95 menuFile
->Check(SkipID
, true);
96 menuFile
->AppendSeparator();
98 menuFile
->Append(QuitID
, "E&xit\tAlt-X", "Quit this program");
100 // the "About" item should be in the help menu
101 wxMenu
*menuHelp
= new wxMenu
;
102 menuHelp
->Append(wxID_ABOUT
, "&About...\tF1", "Show about dialog");
104 // now append the freshly created menu to the menu bar...
105 wxMenuBar
*menuBar
= new wxMenuBar();
106 menuBar
->Append(menuFile
, "&File");
107 menuBar
->Append(menuHelp
, "&Help");
109 // ... and attach this menu bar to the frame
112 m_inputWin
= new wxWindow(this, wxID_ANY
, wxDefaultPosition
, wxSize(-1, 50),
114 m_inputWin
->SetBackgroundColour(*wxBLUE
);
116 wxTextCtrl
*headerText
= new wxTextCtrl(this, wxID_ANY
, "",
117 wxDefaultPosition
, wxDefaultSize
,
119 headerText
->SetValue(
120 " event key KeyCode mod UnicodeKey "
121 " RawKeyCode RawKeyFlags");
124 m_logText
= new wxTextCtrl(this, wxID_ANY
, "",
125 wxDefaultPosition
, wxDefaultSize
,
126 wxTE_MULTILINE
|wxTE_READONLY
|wxHSCROLL
);
128 // set monospace font to have output in nice columns
129 wxFont
font(10, wxFONTFAMILY_TELETYPE
,
130 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
);
131 headerText
->SetFont(font
);
132 m_logText
->SetFont(font
);
135 wxBoxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
136 sizer
->Add(m_inputWin
, wxSizerFlags().Expand());
137 sizer
->Add(headerText
, wxSizerFlags().Expand());
138 sizer
->Add(m_logText
, wxSizerFlags(1).Expand());
139 SetSizerAndFit(sizer
);
141 // set size and position on screen
145 // connect menu event handlers
147 Connect(QuitID
, wxEVT_COMMAND_MENU_SELECTED
,
148 wxCommandEventHandler(MyFrame::OnQuit
));
150 Connect(wxID_ABOUT
, wxEVT_COMMAND_MENU_SELECTED
,
151 wxCommandEventHandler(MyFrame::OnAbout
));
153 Connect(ClearID
, wxEVT_COMMAND_MENU_SELECTED
,
154 wxCommandEventHandler(MyFrame::OnClear
));
156 Connect(SkipID
, wxEVT_COMMAND_MENU_SELECTED
,
157 wxCommandEventHandler(MyFrame::OnSkip
));
159 // connect event handlers for the blue input window
160 m_inputWin
->Connect(wxEVT_KEY_DOWN
, wxKeyEventHandler(MyFrame::OnKeyDown
),
162 m_inputWin
->Connect(wxEVT_KEY_UP
, wxKeyEventHandler(MyFrame::OnKeyUp
),
164 m_inputWin
->Connect(wxEVT_CHAR
, wxKeyEventHandler(MyFrame::OnChar
),
166 m_inputWin
->Connect(wxEVT_PAINT
,
167 wxPaintEventHandler(MyFrame::OnPaintInputWin
),
170 // and show itself (the frames, unlike simple controls, are not shown when
171 // created initially)
177 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
179 wxMessageBox("Demonstrates keyboard event processing in wxWidgets\n"
180 "(c) 2002 Vadim Zeitlin\n"
181 "(c) 2008 Marcin Wojdyr",
182 "About wxWidgets Keyboard Sample",
183 wxOK
| wxICON_INFORMATION
, this);
186 void MyFrame::OnPaintInputWin(wxPaintEvent
& WXUNUSED(event
))
188 wxPaintDC
dc(m_inputWin
);
189 dc
.SetTextForeground(*wxWHITE
);
190 wxFont
font(*wxSWISS_FONT
);
191 font
.SetWeight(wxFONTWEIGHT_BOLD
);
192 font
.SetPointSize(font
.GetPointSize() + 2);
195 dc
.DrawLabel("Press keys here",
196 m_inputWin
->GetClientRect(), wxALIGN_CENTER
);
200 // helper function that returns textual description of wx virtual keycode
201 const char* GetVirtualKeyCodeName(int keycode
)
206 case WXK_##x: return #x;
295 WXK_(NUMPAD_PAGEDOWN
)
301 WXK_(NUMPAD_MULTIPLY
)
303 WXK_(NUMPAD_SEPARATOR
)
304 WXK_(NUMPAD_SUBTRACT
)
313 // helper function that returns textual description of key in the event
314 wxString
GetKeyName(const wxKeyEvent
&event
)
316 int keycode
= event
.GetKeyCode();
317 const char* virt
= GetVirtualKeyCodeName(keycode
);
320 if ( keycode
> 0 && keycode
< 27 )
321 return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode
- 1));
322 if ( keycode
>= 27 && keycode
< 128 )
323 return wxString::Format("'%c'", (unsigned char)keycode
);
325 return wxString::Format("'%c'", event
.GetUnicodeKey());
332 void MyFrame::LogEvent(const wxString
& name
, wxKeyEvent
& event
)
335 // event key_name KeyCode modifiers Unicode raw_code raw_flags
336 msg
.Printf("%7s %15s %5d %c%c%c%c"
342 #ifdef wxHAS_RAW_KEY_CODES
351 event
.ControlDown() ? 'C' : '-',
352 event
.AltDown() ? 'A' : '-',
353 event
.ShiftDown() ? 'S' : '-',
354 event
.MetaDown() ? 'M' : '-'
356 , event
.GetUnicodeKey()
357 , event
.GetUnicodeKey()
359 #ifdef wxHAS_RAW_KEY_CODES
360 , (unsigned long) event
.GetRawKeyCode()
361 , (unsigned long) event
.GetRawKeyFlags()
365 m_logText
->AppendText(msg
);