[haiku-development] Keymap patch

  • From: "Casalinuovo Dario" <barrett666@xxxxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Tue, 2 Sep 2008 13:56:27 +0200

Hi,
Some months ago i wrote this small patch to implement a functionality
that show the keycode of the key when it's pressed.
Please review it and apply if it works.
The patch was made from Haiku rev26814

bye : )
Index: src/preferences/keymap/KeymapWindow.cpp
===================================================================
--- src/preferences/keymap/KeymapWindow.cpp     (revisione 26814)
+++ src/preferences/keymap/KeymapWindow.cpp     (copia locale)
@@ -6,6 +6,8 @@
  *             Sandor Vroemisse
  *             Jérôme Duval
  *             Alexandre Deckner, alex@xxxxxxxxxxxx
+ * With code from:
+ *             Dario Casalinuovo, barrett666@xxxxxxxxx
  */
 
 #include <Alert.h>
@@ -41,6 +43,8 @@
 static const uint32 kMsgMenuEditClear = 'mMEL';
 static const uint32 kMsgMenuEditSelectAll = 'mMEA';
 static const uint32 kMsgMenuFontChanged = 'mMFC';
+static const uint32 kMsgMenuShowKeyCode = 'mMSK';
+static const uint32 kMsgKeyCode = 'mKCD';
 static const uint32 kMsgSystemMapSelected = 'SmST';
 static const uint32 kMsgUserMapSelected = 'UmST';
 static const uint32 kMsgUseKeymap = 'UkyM';
@@ -84,7 +88,17 @@
                 "Revert", new BMessage(kMsgRevertKeymap));
        placeholderView->AddChild(fRevertButton);
        UpdateButtons();
+       // The 'CharCode' TextControl
+       fCharCodeTextControl = new BTextControl(BRect(342, 200, 415, 220), 
+               "CharKeyTextControl", 
+               "", "",
+       new BMessage(kMsgRevertKeymap));
+
+       fCharCodeTextControl->SetEnabled(false);
        
+       placeholderView->AddChild(fCharCodeTextControl);        
+       fCharCodeTextControl->Hide();
+
        BPath path;
        find_directory(B_USER_SETTINGS_DIRECTORY, &path);
        path.Append("Keymap");
@@ -158,7 +172,15 @@
        menu->AddItem(new BMenuItem( "Select All",
                new BMessage(kMsgMenuEditSelectAll), 'A'));
        menubar->AddItem(menu);
-       
+       menu->AddSeparatorItem();
+
+       fShowKeyCode = new BMenuItem("Show KeyCode",
+               new BMessage(kMsgMenuShowKeyCode));
+
+       fShowKeyCode->SetMarked(false);
+
+       menu->AddItem(fShowKeyCode);
+
        // Create the Font menu
        fFontMenu = new BMenu("Font");
        fFontMenu->SetRadioMode(true);
@@ -166,9 +188,9 @@
        font_family family, current_family;
        font_style current_style; 
        uint32 flags;
-       
+
        be_plain_font->GetFamilyAndStyle(&current_family, &current_style);
-       
+
        for (int32 i = 0; i < numFamilies; i++ )
                if (get_font_family(i, &family, &flags) == B_OK) {
                        BMenuItem *item = 
@@ -337,6 +359,32 @@
                        RevertKeymap();
                        UpdateButtons();
                        break;
+               case kMsgMenuShowKeyCode:
+           {
+               if (fShowKeyCode->IsMarked()) {
+                               fShowKeyCode->SetMarked(false);
+                               fCharCodeTextControl->Hide();
+                       } else {
+                               fShowKeyCode->SetMarked(true);
+                               fCharCodeTextControl->Show();
+                   }           
+           }
+               break;
+           case kMsgKeyCode:
+           {
+               if (fShowKeyCode->IsMarked() == true) {
+                       int32 keyCodeInt32;
+                       char keyCodeChar[4];
+                       status_t ret = message->FindInt32("keycode", 
&keyCodeInt32);
+                               
+                               if (ret >= B_OK) {
+                                       sprintf(keyCodeChar, "%d", 
keyCodeInt32);
+                                       
fCharCodeTextControl->SetText(keyCodeChar);
+                               }
+               }
+
+           }
+               break;
                default:
                        BWindow::MessageReceived(message);
                        break;
@@ -1005,10 +1053,19 @@
 
 
 void
+MapView::_SendKeycode(int32 keyCode)
+{
+       BMessage keyMsg(kMsgKeyCode);   
+       keyMsg.AddInt32("keycode", keyCode);    
+       Window()->PostMessage(&keyMsg);
+}
+
+
+void
 MapView::AttachedToWindow()
 {
        BControl::AttachedToWindow();
-               
+
        SetEventMask(B_KEYBOARD_EVENTS, 0);
        SetViewColor(B_TRANSPARENT_COLOR);      
        fTextView->SetViewColor(255, 255, 255);
@@ -1516,7 +1573,7 @@
                        if ((msg->FindData("states", B_UINT8_TYPE, 
reinterpret_cast<const void **>(&states), &size) != B_OK)
                                || (msg->FindInt32("modifiers", 
reinterpret_cast<int32 *>(&info.modifiers)) != B_OK))
                                break;
-                       
+                                               
                        if (fOldKeyInfo.modifiers != info.modifiers) {
                                fOldKeyInfo.modifiers = info.modifiers;
                                for (int8 i = 0; i < 16; i++) 
@@ -1548,8 +1605,9 @@
                                                                        break;
                                                                }
                                                        }
+                               
                                        }
-                               
+                                       
                                if (Window()->IsActive()
                                        && msg->what == B_KEY_DOWN) {
                                        fTextView->MakeFocus();
@@ -1585,7 +1643,17 @@
 void
 MapView::KeyDown(const char* bytes, int32 numBytes)
 {
-       MessageReceived(Window()->CurrentMessage());
+       BMessage* currmsg = Window()->CurrentMessage();
+       int32 KeyCode;
+       status_t ret;
+       
+       MessageReceived(currmsg);
+
+       ret = currmsg->FindInt32("key",&KeyCode);
+
+       if (ret == B_OK) {
+               _SendKeycode(KeyCode);
+       }
 }
 
 
@@ -1607,6 +1675,9 @@
                        if (fKeysRect[i].IsValid() && 
fKeysRect[i].Contains(point)) {
                                fCurrentMouseKey = i;
                                _DrawKey(fCurrentMouseKey);
+                               
+                               _SendKeycode(i);
+                               
                                char *str = NULL;
                                int32 numBytes;
                                fCurrentMap->GetChars(fCurrentMouseKey, 
fOldKeyInfo.modifiers, fActiveDeadKey, &str, &numBytes);
Index: src/preferences/keymap/KeymapWindow.h
===================================================================
--- src/preferences/keymap/KeymapWindow.h       (revisione 26814)
+++ src/preferences/keymap/KeymapWindow.h       (copia locale)
@@ -6,6 +6,8 @@
  *             Sandor Vroemisse
  *             Jérôme Duval
  *             Alexandre Deckner, alex@xxxxxxxxxxxx
+ *With code from:
+ *             Dario Casalinuovo, barrett666@xxxxxxxxx
  */
 
 #ifndef KEYMAP_WINDOW_H
@@ -15,7 +17,10 @@
 #include <FilePanel.h>
 #include <ListView.h>
 #include <MenuBar.h>
+#include <MenuItem.h>
+#include <MessageFilter.h>
 #include <String.h>
+#include <TextControl.h>
 #include <Window.h>
 
 #include "Keymap.h"
@@ -50,6 +55,7 @@
        void _DrawBorder(BView *view, const BRect &borderRect);
        void _DrawLocksLights();        
        void _InitOffscreen();
+       void _SendKeycode(int32 KeyCode);
        
        BBitmap         *fBitmap;
        BView           *fOffscreenView;
@@ -94,6 +100,10 @@
        BButton                         *fRevertButton;
        BMenu                           *fFontMenu;
        
+       BMenuItem           *fShowKeyCode;
+       
+       BTextControl        *fCharCodeTextControl;
+       
        MapView                         *fMapView;
                
        Keymap                          fCurrentMap;

Other related posts: