在 3.3 过滤某个事件 章节介绍的过滤键盘按键的小例子: 如果按键是 字母, 则正常输出, 否则什么也不输出.
这个程序我在 Linux 测试通过, 但在 Win7 64位下表现不正常: 如果按 数字键 1, 程序会提示按键不是字母, 但还是把 1 显示出来了, 之后再按任何数字都不会再输出了.
之后我测试了键盘上所有的符号(非字母), 发现对于所有的非字母键, 程序是可以判断出它不是字母, 但有一小部分符号会显示出来.
请问各位这是怎么造成的呢? 这在应用中会不会造成重大漏洞呢?
附C++代码:
Code: Select all
#include <wx/wx.h>
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
IMPLEMENT_APP(MyApp)
class MyFrame : public wxFrame
{
public:
MyFrame();
};
class MyTextCtrl : public wxTextCtrl
{
public:
MyTextCtrl(wxWindow* parent) : wxTextCtrl(parent, wxID_EDIT, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE) {}
void OnChar(wxKeyEvent& event)
{
if (wxIsalpha(event.GetKeyCode()))
{
event.Skip();
}
else
{
wxBell();
wxMessageBox(_("Must be a character!"), _("ERROR"), wxICON_ERROR | wxOK);
}
}
};
bool MyApp::OnInit()
{
MyFrame* frame = new MyFrame();
frame->Show(true);
return true;
}
MyFrame::MyFrame() : wxFrame(NULL, -1, _("过滤键盘按键事件"))
{
Centre();
MyTextCtrl* textCtrl = new MyTextCtrl(this);
textCtrl->Connect(wxID_EDIT, wxEVT_KEY_DOWN, wxKeyEventHandler(MyTextCtrl::OnChar));
}