-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add KeyEvents and ProcessEvent examples
- Loading branch information
1 parent
fd6e67a
commit adbd395
Showing
5 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
cmake_minimum_required(VERSION 3.0) | ||
project(KeyEvents) | ||
|
||
find_package(wxWidgets REQUIRED) | ||
include(${wxWidgets_USE_FILE}) | ||
link_libraries(${wxWidgets_LIBRARIES}) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
add_executable(${PROJECT_NAME} WIN32 MACOSX_BUNDLE KeyEvents.cpp) | ||
set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "Events") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <wx/wx.h> | ||
#include <wx/display.h> | ||
|
||
namespace Examples { | ||
class Frame : public wxFrame { | ||
public: | ||
Frame() : wxFrame(nullptr, wxID_ANY, "KeyEvents", wxDefaultPosition, {300, 300}) { | ||
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()}); | ||
logWindow->GetFrame()->SetSize(wxDisplay(wxDisplay::GetFromWindow(this)).GetClientArea().GetWidth() / 4, wxDisplay(wxDisplay::GetFromWindow(this)).GetClientArea().GetHeight()); | ||
|
||
panel->Bind(wxEVT_KEY_DOWN, [&](wxKeyEvent& event) { | ||
logWindow->LogTextAtLevel(0, wxString::Format("KeyDown={KeyCode=0x%04x, Modifiers=[%s]}", event.GetKeyCode(), ModiiersToString(event.GetModifiers()))); | ||
event.Skip(); | ||
}); | ||
|
||
panel->Bind(wxEVT_CHAR, [&](wxKeyEvent& event) { | ||
logWindow->LogTextAtLevel(0, wxString::Format("Char={UnicodeKey=%s}", event.GetUnicodeKey()== WXK_NONE ? "[None]" : wxString::Format("'%c'", event.GetUnicodeKey()))); | ||
}); | ||
|
||
panel->Bind(wxEVT_KEY_UP, [&](wxKeyEvent& event) { | ||
logWindow->LogTextAtLevel(0, wxString::Format("KeyUp={KeyCode=0x%04x, Modifiers=[%s]}%s", event.GetKeyCode(), ModiiersToString(event.GetModifiers()), event.GetModifiers() == WXK_NONE ? "\n" : "")); | ||
}); | ||
} | ||
|
||
private: | ||
static std::string ModiiersToString(int modifiers) { | ||
std::string result; | ||
if ((modifiers & wxMOD_SHIFT) == wxMOD_SHIFT) result += "Shift, "; | ||
if ((modifiers & wxMOD_RAW_CONTROL) == wxMOD_RAW_CONTROL) result += "Control, "; | ||
if ((modifiers & wxMOD_ALT) == wxMOD_ALT) result += "Alt, "; | ||
#if defined(__WXOSX__) | ||
if ((modifiers & wxMOD_CONTROL) == wxMOD_CONTROL) result += "Command, "; | ||
#endif | ||
if ((modifiers & wxMOD_META) == wxMOD_META) result += "Meta, "; | ||
if (result.size() > 1) result.resize(result.size() - 2); | ||
return result; | ||
} | ||
|
||
wxPanel* panel = new wxPanel(this); | ||
wxLogWindow* logWindow = new wxLogWindow(this, "Debug"); | ||
}; | ||
|
||
class Application : public wxApp { | ||
bool OnInit() override { | ||
(new Frame())->Show(); | ||
return true; | ||
} | ||
}; | ||
} | ||
|
||
wxIMPLEMENT_APP(Examples::Application); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
cmake_minimum_required(VERSION 3.0) | ||
project(ProcessEvent) | ||
|
||
find_package(wxWidgets REQUIRED) | ||
include(${wxWidgets_USE_FILE}) | ||
link_libraries(${wxWidgets_LIBRARIES}) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
add_executable(${PROJECT_NAME} WIN32 MACOSX_BUNDLE ProcessEvent.cpp) | ||
set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "Events") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <wx/wx.h> | ||
#include <wx/msgout.h> | ||
|
||
namespace Examples { | ||
class Frame1 : public wxFrame { | ||
public: | ||
Frame1() : wxFrame(nullptr, wxID_ANY, "Frame1") { | ||
SetClientSize(300, 300); | ||
Bind(wxEVT_PAINT, [&](wxPaintEvent& event) { | ||
wxPaintDC dc(this); | ||
if (AppActive()) { | ||
dc.SetBrush({wxSystemSettings::GetColour(wxSystemColour::wxSYS_COLOUR_MENUHILIGHT)}); | ||
dc.SetPen(*wxTRANSPARENT_PEN); | ||
dc.DrawRectangle({10, 10, 280, 50}); | ||
dc.DrawText("Application is active", 10, 10); | ||
} else { | ||
dc.SetBrush({wxSystemSettings::GetColour(wxSystemColour::wxSYS_COLOUR_BTNFACE)}); | ||
dc.SetPen(*wxTRANSPARENT_PEN); | ||
dc.DrawRectangle({10, 10, 280, 50}); | ||
dc.DrawText("Application is inactive", 10, 10); | ||
} | ||
}); | ||
|
||
} | ||
|
||
bool AppActive() const {return appActive;} | ||
void AppActive(bool value) {appActive = value;} | ||
|
||
private: | ||
wxPanel* panel = new wxPanel(this); | ||
bool appActive = true; | ||
}; | ||
|
||
class Application : public wxApp { | ||
bool ProcessEvent (wxEvent &event) override { | ||
if (event.GetEventType() == wxEVT_ACTIVATE_APP) { | ||
frame1->AppActive(dynamic_cast<wxActivateEvent&>(event).GetActive()); | ||
wxMessageOutputDebug().Printf("wxEVT_ACTIVATE_APP [active=%s]", frame1->AppActive() ? "true" : "false"); | ||
frame1->Refresh(); | ||
} | ||
|
||
return wxApp::ProcessEvent(event); | ||
} | ||
|
||
bool OnInit() override { | ||
frame1 = new Frame1(); | ||
frame1->Show(); | ||
return true; | ||
} | ||
|
||
Frame1* frame1 = nullptr; | ||
}; | ||
} | ||
|
||
wxIMPLEMENT_APP(Examples::Application); |