Skip to content

Commit adbd395

Browse files
committed
Add KeyEvents and ProcessEvent examples
1 parent fd6e67a commit adbd395

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

src/Events/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ project(Events)
44
add_subdirectory(CustomEvent)
55
add_subdirectory(FrameClick)
66
add_subdirectory(FrameAndEvents)
7+
add_subdirectory(KeyEvents)
8+
add_subdirectory(ProcessEvent)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
project(KeyEvents)
3+
4+
find_package(wxWidgets REQUIRED)
5+
include(${wxWidgets_USE_FILE})
6+
link_libraries(${wxWidgets_LIBRARIES})
7+
8+
set(CMAKE_CXX_STANDARD 17)
9+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
10+
add_executable(${PROJECT_NAME} WIN32 MACOSX_BUNDLE KeyEvents.cpp)
11+
set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "Events")

src/Events/KeyEvents/KeyEvents.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <wx/wx.h>
2+
#include <wx/display.h>
3+
4+
namespace Examples {
5+
class Frame : public wxFrame {
6+
public:
7+
Frame() : wxFrame(nullptr, wxID_ANY, "KeyEvents", wxDefaultPosition, {300, 300}) {
8+
logWindow->GetFrame()->SetPosition({wxDisplay(wxDisplay::GetFromWindow(this)).GetClientArea().GetLeft() + wxDisplay(wxDisplay::GetFromWindow(this)).GetClientArea().GetWidth() - wxDisplay(wxDisplay::GetFromWindow(this)).GetClientArea().GetWidth() / 4, wxDisplay(wxDisplay::GetFromWindow(this)).GetClientArea().GetTop()});
9+
logWindow->GetFrame()->SetSize(wxDisplay(wxDisplay::GetFromWindow(this)).GetClientArea().GetWidth() / 4, wxDisplay(wxDisplay::GetFromWindow(this)).GetClientArea().GetHeight());
10+
11+
panel->Bind(wxEVT_KEY_DOWN, [&](wxKeyEvent& event) {
12+
logWindow->LogTextAtLevel(0, wxString::Format("KeyDown={KeyCode=0x%04x, Modifiers=[%s]}", event.GetKeyCode(), ModiiersToString(event.GetModifiers())));
13+
event.Skip();
14+
});
15+
16+
panel->Bind(wxEVT_CHAR, [&](wxKeyEvent& event) {
17+
logWindow->LogTextAtLevel(0, wxString::Format("Char={UnicodeKey=%s}", event.GetUnicodeKey()== WXK_NONE ? "[None]" : wxString::Format("'%c'", event.GetUnicodeKey())));
18+
});
19+
20+
panel->Bind(wxEVT_KEY_UP, [&](wxKeyEvent& event) {
21+
logWindow->LogTextAtLevel(0, wxString::Format("KeyUp={KeyCode=0x%04x, Modifiers=[%s]}%s", event.GetKeyCode(), ModiiersToString(event.GetModifiers()), event.GetModifiers() == WXK_NONE ? "\n" : ""));
22+
});
23+
}
24+
25+
private:
26+
static std::string ModiiersToString(int modifiers) {
27+
std::string result;
28+
if ((modifiers & wxMOD_SHIFT) == wxMOD_SHIFT) result += "Shift, ";
29+
if ((modifiers & wxMOD_RAW_CONTROL) == wxMOD_RAW_CONTROL) result += "Control, ";
30+
if ((modifiers & wxMOD_ALT) == wxMOD_ALT) result += "Alt, ";
31+
#if defined(__WXOSX__)
32+
if ((modifiers & wxMOD_CONTROL) == wxMOD_CONTROL) result += "Command, ";
33+
#endif
34+
if ((modifiers & wxMOD_META) == wxMOD_META) result += "Meta, ";
35+
if (result.size() > 1) result.resize(result.size() - 2);
36+
return result;
37+
}
38+
39+
wxPanel* panel = new wxPanel(this);
40+
wxLogWindow* logWindow = new wxLogWindow(this, "Debug");
41+
};
42+
43+
class Application : public wxApp {
44+
bool OnInit() override {
45+
(new Frame())->Show();
46+
return true;
47+
}
48+
};
49+
}
50+
51+
wxIMPLEMENT_APP(Examples::Application);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
project(ProcessEvent)
3+
4+
find_package(wxWidgets REQUIRED)
5+
include(${wxWidgets_USE_FILE})
6+
link_libraries(${wxWidgets_LIBRARIES})
7+
8+
set(CMAKE_CXX_STANDARD 17)
9+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
10+
add_executable(${PROJECT_NAME} WIN32 MACOSX_BUNDLE ProcessEvent.cpp)
11+
set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "Events")
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <wx/wx.h>
2+
#include <wx/msgout.h>
3+
4+
namespace Examples {
5+
class Frame1 : public wxFrame {
6+
public:
7+
Frame1() : wxFrame(nullptr, wxID_ANY, "Frame1") {
8+
SetClientSize(300, 300);
9+
Bind(wxEVT_PAINT, [&](wxPaintEvent& event) {
10+
wxPaintDC dc(this);
11+
if (AppActive()) {
12+
dc.SetBrush({wxSystemSettings::GetColour(wxSystemColour::wxSYS_COLOUR_MENUHILIGHT)});
13+
dc.SetPen(*wxTRANSPARENT_PEN);
14+
dc.DrawRectangle({10, 10, 280, 50});
15+
dc.DrawText("Application is active", 10, 10);
16+
} else {
17+
dc.SetBrush({wxSystemSettings::GetColour(wxSystemColour::wxSYS_COLOUR_BTNFACE)});
18+
dc.SetPen(*wxTRANSPARENT_PEN);
19+
dc.DrawRectangle({10, 10, 280, 50});
20+
dc.DrawText("Application is inactive", 10, 10);
21+
}
22+
});
23+
24+
}
25+
26+
bool AppActive() const {return appActive;}
27+
void AppActive(bool value) {appActive = value;}
28+
29+
private:
30+
wxPanel* panel = new wxPanel(this);
31+
bool appActive = true;
32+
};
33+
34+
class Application : public wxApp {
35+
bool ProcessEvent (wxEvent &event) override {
36+
if (event.GetEventType() == wxEVT_ACTIVATE_APP) {
37+
frame1->AppActive(dynamic_cast<wxActivateEvent&>(event).GetActive());
38+
wxMessageOutputDebug().Printf("wxEVT_ACTIVATE_APP [active=%s]", frame1->AppActive() ? "true" : "false");
39+
frame1->Refresh();
40+
}
41+
42+
return wxApp::ProcessEvent(event);
43+
}
44+
45+
bool OnInit() override {
46+
frame1 = new Frame1();
47+
frame1->Show();
48+
return true;
49+
}
50+
51+
Frame1* frame1 = nullptr;
52+
};
53+
}
54+
55+
wxIMPLEMENT_APP(Examples::Application);

0 commit comments

Comments
 (0)