绑定wxEVT_CHAR_HOOK在 wxWidgets 3.1.2 中不起作用

Binding wxEVT_CHAR_HOOK not working in wxWidgets 3.1.2

本文关键字:不起作用 wxEVT CHAR HOOK 绑定 wxWidgets      更新时间:2023-10-16

我试图在每次按下键时调用一个函数。由于某种原因,我无法将wxEVT_CHAR_HOOK事件绑定到函数。 创建按钮并在按下按钮时将按钮绑定到函数没有问题。 我正在使用Visual Studio 2017和wxWidgets 3.1.2

我已经尝试与wxEVT_KEY_DOWN交换wxEVT_CHAR_HOOK,但这不起作用。 但是,将同一函数与另一个事件(如wxEVT_BUTTON)绑定确实有效。

enum
{
ID_Hello = wxID_HIGHEST + 1,
ID_Button1 = wxID_HIGHEST + 2,
ID_Panel1 = wxID_HIGHEST + 3
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame();
frame->Show(true);
return true;
}
MyFrame::MyFrame() : wxFrame(NULL, wxID_ANY, "Hello World")
{
panel1 = new wxPanel(this, ID_Panel1, wxDefaultPosition, wxDefaultSize);
wxSize buttonSizeHelloWorld = wxSize(200, 200);
wxPoint buttonPosHelloWorld = wxPoint(50, 50);
HelloWorldButton = new wxButton(panel1, ID_Button1,
"Hello", buttonPosHelloWorld, buttonSizeHelloWorld);
//These work fine
Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
Bind(wxEVT_BUTTON, &MyFrame::OnExit, this, ID_Button1);
//This is what fails
Bind(wxEVT_CHAR_HOOK, &MyFrame::ProcessKeypress, this);
}
void MyFrame::ProcessKeypress(wxCommandEvent& event) {
wxLogMessage("A key was pressed");
}
void MyFrame::OnHello(wxCommandEvent& event)
{
wxLogMessage("Hello world from wxWidgets!");
}
void MyFrame::OnExit(wxCommandEvent& event)
{
Close(true);
}

错误消息(我搜索了它,但没有找到有用的东西):

C2664 "void wxEventFunctorMethod::CheckHandlerArgument(EventArg *)" : 无法将"wxKeyEvent *"的参数 1 转换为 "EventArg *" (event.h -> 行: 374)

wxEVT_CHAR_HOOK的处理程序必须接受wxKeyEvent&参数而不是wxCommandEvent&,错误消息告诉您这正是这一点(您还应该看到编译器输出中wxCommandEventEventArg)。