| Forum List • Thread List • Refresh • New Topic • Search • Previous • Next 1 | 1. I cant get key event? #2653 | Hi,
When i press a key, but, no happen to me.. Why?
wxKeyEvent *event = new wxKeyEvent(wxEVT_KEY_DOWN); if (event->GetKeyCode()==WXK_ESCAPE) { Close(); }else{ wxMessageBox(_T("A")); //Always run to this level }
Meanwhile, how do i get the key code when a press a key?
I try to use like this: wxMessageBox(event->GetKeyCode()); but prompt me error messages....
Any solutions can achieve?
Thanks...Meow~ | 2. Re: I cant get key event? #2654 Posted by: 2003-09-20 20:51:15 | Hi! Yes, you can get a key event. Otherwise it wouldn't make that much sense to have them :) You should use the specific event macros for that, like EVT_KEY_DOWN(func), EVT_KEY_UP(func) and EVT_CHAR(func). Write your own functions where you deal with the pressed keys and pass them a wxKeyEvent, like void OnKeyPressed(wxKeyEvent &event). Then you can use event.GetKeyCode() to get the code.
Have a look at the "text" example in the samples folder. There you should see the usage of all three wxKeyEvent macros.
upcase | 3. Re: I cant get key event? #2655 | Hi, I am still cant get it, still no respond.. :(
is that need not to call OnKeyDown from main dialog box or call it from anywhere?
if i call it, it promt me error message that, no matching function found OnKeyDown(wxKeyEvent &event);
class frmLogin : public wxDialog { public: frmLogin(wxWindow *parent); ~frmLogin(); private: void OnKeyDown(wxKeyEvent& event); DECLARE_EVENT_TABLE()
};
void frmLogin::OnKeyDown(wxKeyEvent& event) { wxMessageBox(_T("A")); }
BEGIN_EVENT_TABLE(frmLogin, wxDialog) EVT_KEY_DOWN(frmLogin::OnKeyDown) END_EVENT_TABLE()
Thank you...Meow~ | 4. Re: I cant get key event? #2657 Posted by: 2003-09-21 23:22:10 | Hi! Problem is that for catching the key event the window/control for which you defined the event macros must have focus. This means that if you defined the events for a dialog, but a button or text control in this dialog has the focus, no event will be caught. Try defining it for the App itself like this:
class MyApp: public wxApp { public: bool OnInit(); private: void OnKey(wxKeyEvent &e); DECLARE_EVENT_TABLE() };
IMPLEMENT_APP(MyApp)
BEGIN_EVENT_TABLE(MyApp, wxApp) EVT_KEY_DOWN(MyApp::OnKey) END_EVENT_TABLE()
void MyApp::OnKey(wxKeyEvent &e) { wxMessageBox("You pressed a key"); } ...
That way *all* keystrokes will be processed by OnKey().
upcase | 5. Re: I cant get key event? #2661 | I think i understand what you mean, is that mean, the event, must be focus on the main application?
Thanks.Meow~ | 6. Re: I cant get key event? #2662 Posted by: 2003-09-22 19:57:25 | Hi again :) Sorry, I was a bit sleepy yesterday. I'll try to clearify what I mean.
The way I see it is that you want to write an application that consits of a dialog and should be capable of recieving and processing keystrokes. So, since you wanted to work with wxKeyEvent I guess you don't mean Accelerators (like CTRL+S or such).
Firstly, you don't need to create wxKeyEvent objects yourself. You could do that to simulate keystrokes but for processing *real* ones, you only deal with events that get generated automatically. For that purpose wxWindows uses eventtables and macros. There's another way of working with events by pluging them dynamically, but I don't advice you to do that. Working with these macros you'll have to specify for which control/dialog/application a specific function should be reponsible. You do that while definening the event table. If you write an event function for let's say a button
BEGIN_EVENT_TABLE(myDialog, wxDialog)
EVT_BUTTON( ID_OF_BUTTON, myDialog::OnButtonPressed )
END_EVENT_TABLE()
void myDialog::OnButtonPressed(wxCommandEvent &e)
{
...
}
this means that the function OnButtonPressed() will only process events sended by the button with ID ID_OF_BUTTON on the dialog myDialg. Coming from Java this means you plug an EventHandler to the button, or, coming from Qt, you connect the signal clicked() of the button to one specific slot (function) of the dialog. Now, on Windows, what happens? Windows uses message loops to send out events. If the button has focus and is clicked, a message is generated and processed by the message loop. wx jumps in there, generates a wxCommandEvent and since it knows the ID of the button it looks if there's an entry in the event table with this ID for that specific dialog, finds one and calls the function OnButtonPressed(), passing it the wxCommandEvent.
Now that's where the problem with your wxKeyEvent starts. To have an event processed, the control/dialog/whatever for which you defined an eventtable entry and a processing function, the control/dialog/whatever must have focus, meaning it is active. When you want to catch an event like a keystroke on a dialog, this is most likely not the case. If you embedded other controls on your dialog, like buttons, checkboxes, etc., these will have focus, not the dialog itself! So, since there is a keypress but the dialog itself hasn't got focus, the correct function isn't called.
To work around this and to be sure to get ALL keystrokes, regardless of what control is focused, you could use wxApp, since this is the base where it all starts from. Using an event table like
class MyApp: public wxApp {
public:
bool OnInit();
private:
void OnKey(wxKeyEvent &e);
DECLARE_EVENT_TABLE()
};
IMPLEMENT_APP(MyApp)
BEGIN_EVENT_TABLE(MyApp, wxApp)
EVT_KEY_DOWN(MyApp::OnKey)
END_EVENT_TABLE()
void MyApp::OnKey(wxKeyEvent &e)
{
wxMessageBox("You pressed a key");
}
void MyApp::OnInit()
{
}
...
you specify that *all* keystroke events should call OnKey() of myApp. Problem is that now if you press enter while focusing a button OnKey() is called. This might not be what you want. If you click the button everything is fine. So you should think about which keystrokes should be skipped by OnKey and do so.
I hope I was able to clearify things a bit. Working with events in wx is sure one of the most hardest things to learn. I'll try and upload a simple example using this "add attachment".
upcase | 7. Re: I cant get key event? #2663 Posted by: 2003-09-22 20:01:27 | Hmmm, that didn't work. No zip allowed. Hope that the email you gave is correct, gonna send it that way :)
upcase | 8. Re: I cant get key event? #2664 | Hi, any possible that.. know which child window is activate now?
for example, maybe i called the child frame called MyChild1, after that called MyChild2
any possible to know, when i onkeydown, it will know which child window is activate, so that i can minimize it or close it?
Thank you... :)Meow~ | Forum List • Thread List • Refresh • New Topic • Search • Previous • Next 1 |
|
|