]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/keyboard/keyboard.cpp
Update version to 2.9.4 in version.bkl too and rebake everything.
[wxWidgets.git] / samples / keyboard / keyboard.cpp
index 5ea78192a1cfdcbf7b95bf99b976eaf46a396bf7..c9e1ff9fccbe9e3e99a85677a4bb011af48cda0c 100644 (file)
@@ -18,6 +18,9 @@
     #include "wx/wx.h"
 #endif
 
+#ifndef __WXMSW__
+    #include "../sample.xpm"
+#endif
 
 // Define a new frame type: this is going to be our main frame
 class MyFrame : public wxFrame
@@ -29,18 +32,41 @@ private:
     // event handlers
     void OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(true); }
     void OnAbout(wxCommandEvent& event);
+
+    void OnTestAccelA(wxCommandEvent& WXUNUSED(event))
+        { m_logText->AppendText("Test accelerator \"A\" used.\n"); }
+    void OnTestAccelCtrlA(wxCommandEvent& WXUNUSED(event))
+        { m_logText->AppendText("Test accelerator \"Ctrl-A\" used.\n"); }
+    void OnTestAccelEsc(wxCommandEvent& WXUNUSED(event))
+        { m_logText->AppendText("Test accelerator \"Esc\" used.\n"); }
+
     void OnClear(wxCommandEvent& WXUNUSED(event)) { m_logText->Clear(); }
-    void OnSkip(wxCommandEvent& event) { m_skip = event.IsChecked(); }
-    void OnKeyDown(wxKeyEvent& event) { LogEvent("KeyDown", event); }
+    void OnSkipDown(wxCommandEvent& event) { m_skipDown = event.IsChecked(); }
+    void OnSkipHook(wxCommandEvent& event) { m_skipHook = event.IsChecked(); }
+
+    void OnKeyDown(wxKeyEvent& event)
+    {
+        LogEvent("KeyDown", event);
+        if ( m_skipDown )
+            event.Skip();
+    }
     void OnKeyUp(wxKeyEvent& event) { LogEvent("KeyUp", event); }
     void OnChar(wxKeyEvent& event) { LogEvent("Char", event); }
+    void OnCharHook(wxKeyEvent& event)
+    {
+        LogEvent("Hook", event);
+        if ( m_skipHook )
+            event.Skip();
+    }
+
     void OnPaintInputWin(wxPaintEvent& event);
 
     void LogEvent(const wxString& name, wxKeyEvent& event);
 
     wxTextCtrl *m_logText;
     wxWindow *m_inputWin;
-    bool m_skip;
+    bool m_skipHook,
+         m_skipDown;
 };
 
 
@@ -75,14 +101,21 @@ IMPLEMENT_APP(MyApp)
 MyFrame::MyFrame(const wxString& title)
        : wxFrame(NULL, wxID_ANY, title),
          m_inputWin(NULL),
-         m_skip(true)
+         m_skipHook(true),
+         m_skipDown(true)
 {
+    SetIcon(wxICON(sample));
+
     // IDs for menu items
     enum
     {
         QuitID = wxID_EXIT,
         ClearID = wxID_CLEAR,
-        SkipID = 100,
+        SkipHook = 100,
+        SkipDown,
+        TestAccelA,
+        TestAccelCtrlA,
+        TestAccelEsc
     };
 
     // create a menu bar
@@ -91,8 +124,19 @@ MyFrame::MyFrame(const wxString& title)
     menuFile->Append(ClearID, "&Clear log\tCtrl-L");
     menuFile->AppendSeparator();
 
-    menuFile->AppendCheckItem(SkipID, "Call event.&Skip()\tCtrl-S");
-    menuFile->Check(SkipID, true);
+    menuFile->Append(TestAccelA, "Test accelerator &1\tA");
+    menuFile->Append(TestAccelCtrlA, "Test accelerator &2\tCtrl-A");
+    menuFile->Append(TestAccelEsc, "Test accelerator &3\tEsc");
+    menuFile->AppendSeparator();
+
+    menuFile->AppendCheckItem(SkipHook, "Skip CHAR_HOOK event",
+        "Not skipping this event disables both KEY_DOWN and CHAR events"
+    );
+    menuFile->Check(SkipHook, true);
+    menuFile->AppendCheckItem(SkipDown, "Skip KEY_DOWN event",
+        "Not skipping this event disables CHAR event generation"
+    );
+    menuFile->Check(SkipDown, true);
     menuFile->AppendSeparator();
 
     menuFile->Append(QuitID, "E&xit\tAlt-X", "Quit this program");
@@ -110,7 +154,7 @@ MyFrame::MyFrame(const wxString& title)
     SetMenuBar(menuBar);
 
     m_inputWin = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxSize(-1, 50),
-                              wxRAISED_BORDER),
+                              wxRAISED_BORDER);
     m_inputWin->SetBackgroundColour(*wxBLUE);
 
     wxTextCtrl *headerText = new wxTextCtrl(this, wxID_ANY, "",
@@ -121,12 +165,12 @@ MyFrame::MyFrame(const wxString& title)
                "  RawKeyCode RawKeyFlags");
 
 
-    m_logText = new wxTextCtrl(this, wxID_ANY, "", 
+    m_logText = new wxTextCtrl(this, wxID_ANY, "",
                                wxDefaultPosition, wxDefaultSize,
                                wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL);
 
     // set monospace font to have output in nice columns
-    wxFont font(10, wxFONTFAMILY_TELETYPE, 
+    wxFont font(10, wxFONTFAMILY_TELETYPE,
                 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
     headerText->SetFont(font);
     m_logText->SetFont(font);
@@ -153,20 +197,38 @@ MyFrame::MyFrame(const wxString& title)
     Connect(ClearID, wxEVT_COMMAND_MENU_SELECTED,
             wxCommandEventHandler(MyFrame::OnClear));
 
-    Connect(SkipID, wxEVT_COMMAND_MENU_SELECTED,
-            wxCommandEventHandler(MyFrame::OnSkip));
+    Connect(SkipHook, wxEVT_COMMAND_MENU_SELECTED,
+            wxCommandEventHandler(MyFrame::OnSkipHook));
+    Connect(SkipDown, wxEVT_COMMAND_MENU_SELECTED,
+            wxCommandEventHandler(MyFrame::OnSkipDown));
+
+    Connect(TestAccelA, wxEVT_COMMAND_MENU_SELECTED,
+            wxCommandEventHandler(MyFrame::OnTestAccelA));
 
-    // connect event handlers for the blue input window 
+    Connect(TestAccelCtrlA, wxEVT_COMMAND_MENU_SELECTED,
+            wxCommandEventHandler(MyFrame::OnTestAccelCtrlA));
+
+    Connect(TestAccelEsc, wxEVT_COMMAND_MENU_SELECTED,
+            wxCommandEventHandler(MyFrame::OnTestAccelEsc));
+
+    // connect event handlers for the blue input window
     m_inputWin->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(MyFrame::OnKeyDown),
                         NULL, this);
     m_inputWin->Connect(wxEVT_KEY_UP, wxKeyEventHandler(MyFrame::OnKeyUp),
                         NULL, this);
     m_inputWin->Connect(wxEVT_CHAR, wxKeyEventHandler(MyFrame::OnChar),
                         NULL, this);
-    m_inputWin->Connect(wxEVT_PAINT, 
+    m_inputWin->Connect(wxEVT_PAINT,
                         wxPaintEventHandler(MyFrame::OnPaintInputWin),
                         NULL, this);
 
+    // notice that we don't connect OnCharHook() to the input window, unlike
+    // the usual key events this one is propagated upwards
+    Connect(wxEVT_CHAR_HOOK, wxKeyEventHandler(MyFrame::OnCharHook));
+
+    // status bar is useful for showing the menu items help strings
+    CreateStatusBar();
+
     // and show itself (the frames, unlike simple controls, are not shown when
     // created initially)
     Show(true);
@@ -192,7 +254,7 @@ void MyFrame::OnPaintInputWin(wxPaintEvent& WXUNUSED(event))
     font.SetPointSize(font.GetPointSize() + 2);
     dc.SetFont(font);
 
-    dc.DrawLabel("Press keys here", 
+    dc.DrawLabel("Press keys here",
                  m_inputWin->GetClientRect(), wxALIGN_CENTER);
 }
 
@@ -203,7 +265,7 @@ const char* GetVirtualKeyCodeName(int keycode)
     switch ( keycode )
     {
 #define WXK_(x) \
-        case WXK_##x: return #x; 
+        case WXK_##x: return #x;
 
         WXK_(BACK)
         WXK_(TAB)
@@ -304,9 +366,16 @@ const char* GetVirtualKeyCodeName(int keycode)
         WXK_(NUMPAD_SUBTRACT)
         WXK_(NUMPAD_DECIMAL)
         WXK_(NUMPAD_DIVIDE)
+
+        WXK_(WINDOWS_LEFT)
+        WXK_(WINDOWS_RIGHT)
+#ifdef __WXOSX__
+        WXK_(RAW_CONTROL)
+#endif
 #undef WXK_
-    default: 
-        return NULL; 
+
+    default:
+        return NULL;
     }
 }
 
@@ -317,22 +386,25 @@ wxString GetKeyName(const wxKeyEvent &event)
     const char* virt = GetVirtualKeyCodeName(keycode);
     if ( virt )
         return virt;
-    if ( keycode > 0 && keycode < 27 )
+    if ( keycode > 0 && keycode < 32 )
         return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode - 1));
-    if ( keycode >= 27 && keycode < 128 )
+    if ( keycode >= 32 && keycode < 128 )
         return wxString::Format("'%c'", (unsigned char)keycode);
+
 #if wxUSE_UNICODE
-    return wxString::Format("'%c'", event.GetUnicodeKey());
-#else
-    return "unknown";
+    int uc = event.GetUnicodeKey();
+    if ( uc != WXK_NONE )
+        return wxString::Format("'%c'", uc);
 #endif
+
+    return "unknown";
 }
 
 
 void MyFrame::LogEvent(const wxString& name, wxKeyEvent& event)
 {
     wxString msg;
-    // event  key_name  KeyCode  modifiers  Unicode  raw_code raw_flags 
+    // event  key_name  KeyCode  modifiers  Unicode  raw_code raw_flags
     msg.Printf("%7s %15s %5d   %c%c%c%c"
 #if wxUSE_UNICODE
                    "%5d (U+%04x)"
@@ -340,7 +412,7 @@ void MyFrame::LogEvent(const wxString& name, wxKeyEvent& event)
                    "    none   "
 #endif
 #ifdef wxHAS_RAW_KEY_CODES
-                   "  %7lu    0x%lx"
+                   "  %7lu    0x%08lx"
 #else
                    "  not-set    not-set"
 #endif
@@ -363,9 +435,6 @@ void MyFrame::LogEvent(const wxString& name, wxKeyEvent& event)
                );
 
     m_logText->AppendText(msg);
-
-    if ( m_skip )
-        event.Skip();
 }