KeyRemap4MacBook

Macでキーマップのカスタマイズと言えばKeyRemap4MacBook(とPCKeyboardHack)。以前(もう1年近く前!)同じようにカーネルに潜り込んでキーマップをカスタマイズするドライバを作ろうとしたことがあったのですが、私にはちょっとムリだったようで…。個人的にはsysctlで設定を変更できるインターフェイスが秀逸だなぁと思っています。

そんなKeyRemap4MacBookですが、ちょっとパッチを当てて使っています。アプリケーションキー → F11、Command R → Option L のマップを追加しているのと、キーリピート周りを少し変えています。

diff -urp KeyRemap4MacBook-3.0.0/files/prefpane/sysctl.xml KeyRemap4MacBook-3.0.0-custom/files/prefpane/sysctl.xml
--- KeyRemap4MacBook-3.0.0/files/prefpane/sysctl.xml	2008-02-02 15:38:12.000000000 +0900
+++ KeyRemap4MacBook-3.0.0-custom/files/prefpane/sysctl.xml	2008-02-06 23:52:02.000000000 +0900
@@ -34,6 +34,16 @@
     </item>
 
     <item>
+      <name>Remap Command_R Key</name>
+      <list>
+        <item>
+          <name>Command_R to Option_L</name>
+          <sysctl>keyremap4macbook.remap.commandR2optionL</sysctl>
+        </item>
+      </list>
+    </item>
+
+    <item>
       <name>Remap Control_L Key</name>
       <list>
         <item>
@@ -210,6 +220,16 @@
     </item>
 
     <item>
+      <name>Remap Application Key</name>
+      <list>
+        <item>
+          <name>Application to F11</name>
+          <sysctl>keyremap4macbook.remap.application2f11</sysctl>
+        </item>
+      </list>
+    </item>
+
+    <item>
       <name>Misc Remapping</name>
       <list>
         <item>
diff -urp KeyRemap4MacBook-3.0.0/src/kext/KeyRemap4MacBook.cpp KeyRemap4MacBook-3.0.0-custom/src/kext/KeyRemap4MacBook.cpp
--- KeyRemap4MacBook-3.0.0/src/kext/KeyRemap4MacBook.cpp	2008-02-01 22:38:46.000000000 +0900
+++ KeyRemap4MacBook-3.0.0-custom/src/kext/KeyRemap4MacBook.cpp	2008-02-06 23:52:02.000000000 +0900
@@ -272,7 +272,7 @@ org_pqrs_driver_KeyRemap4MacBook::keyboa
         const int MINVAL = 200;
         if (initial < MINVAL) initial = MINVAL;
         p->timer->setTimeoutMS(initial);
-      } else {
+      } else if ((p->repeat).key == key) {
         p->timer->cancelTimeout();
       }
     }
diff -urp KeyRemap4MacBook-3.0.0/src/kext/config.cpp KeyRemap4MacBook-3.0.0-custom/src/kext/config.cpp
--- KeyRemap4MacBook-3.0.0/src/kext/config.cpp	2008-02-02 01:31:51.000000000 +0900
+++ KeyRemap4MacBook-3.0.0-custom/src/kext/config.cpp	2008-02-06 23:52:02.000000000 +0900
@@ -47,6 +47,10 @@ namespace org_pqrs_KeyRemap4MacBook {
              &(config.remap_commandL2backquote), 0, "");
 
   // ----------------------------------------
+  SYSCTL_INT(_keyremap4macbook_remap, OID_AUTO, commandR2optionL, CTLTYPE_INT|CTLFLAG_RW,
+             &(config.remap_commandR2optionL), 0, "");
+
+  // ----------------------------------------
   SYSCTL_INT(_keyremap4macbook_remap, OID_AUTO, controlL2commandL, CTLTYPE_INT|CTLFLAG_RW,
              &(config.remap_controlL2commandL), 0, "");
 
@@ -266,6 +270,10 @@ namespace org_pqrs_KeyRemap4MacBook {
              &(config.remap_jis_unify_kana_eisuu_to_commandL), 0, "");
 
   // ----------------------------------------
+  SYSCTL_INT(_keyremap4macbook_remap, OID_AUTO, application2f11, CTLTYPE_INT|CTLFLAG_RW,
+             &(config.remap_application2f11), 0, "");
+
+  // ----------------------------------------
   SYSCTL_INT(_keyremap4macbook, OID_AUTO, debug, CTLTYPE_INT|CTLFLAG_RW,
              &(config.debug), 0, "");
 
@@ -293,6 +301,8 @@ namespace org_pqrs_KeyRemap4MacBook {
     sysctl_register_oid(&sysctl__keyremap4macbook_remap_commandL2optionL);
     sysctl_register_oid(&sysctl__keyremap4macbook_remap_commandL2backquote);
 
+    sysctl_register_oid(&sysctl__keyremap4macbook_remap_commandR2optionL);
+
     sysctl_register_oid(&sysctl__keyremap4macbook_remap_controlL2commandL);
 
     sysctl_register_oid(&sysctl__keyremap4macbook_remap_enter2commandL);
@@ -387,6 +397,9 @@ namespace org_pqrs_KeyRemap4MacBook {
     sysctl_register_oid(&sysctl__keyremap4macbook_remap_jis_unify_kana_eisuu_to_commandL);
 
     // ----------------------------------------
+    sysctl_register_oid(&sysctl__keyremap4macbook_remap_application2f11);
+
+    // ----------------------------------------
     sysctl_register_oid(&sysctl__keyremap4macbook_debug);
     sysctl_register_oid(&sysctl__keyremap4macbook_version);
   }
@@ -411,6 +424,8 @@ namespace org_pqrs_KeyRemap4MacBook {
     sysctl_unregister_oid(&sysctl__keyremap4macbook_remap_commandL2optionL);
     sysctl_unregister_oid(&sysctl__keyremap4macbook_remap_commandL2backquote);
 
+    sysctl_unregister_oid(&sysctl__keyremap4macbook_remap_commandR2optionL);
+
     sysctl_unregister_oid(&sysctl__keyremap4macbook_remap_controlL2commandL);
 
     sysctl_unregister_oid(&sysctl__keyremap4macbook_remap_enter2commandL);
@@ -505,6 +520,9 @@ namespace org_pqrs_KeyRemap4MacBook {
     sysctl_unregister_oid(&sysctl__keyremap4macbook_remap_jis_unify_kana_eisuu_to_commandL);
 
     // ----------------------------------------
+    sysctl_unregister_oid(&sysctl__keyremap4macbook_remap_application2f11);
+
+    // ----------------------------------------
     sysctl_unregister_oid(&sysctl__keyremap4macbook_debug);
     sysctl_unregister_oid(&sysctl__keyremap4macbook_version);
   }
diff -urp KeyRemap4MacBook-3.0.0/src/kext/config.hpp KeyRemap4MacBook-3.0.0-custom/src/kext/config.hpp
--- KeyRemap4MacBook-3.0.0/src/kext/config.hpp	2008-02-02 01:22:52.000000000 +0900
+++ KeyRemap4MacBook-3.0.0-custom/src/kext/config.hpp	2008-02-06 23:52:02.000000000 +0900
@@ -21,6 +21,8 @@ namespace org_pqrs_KeyRemap4MacBook {
     int remap_commandL2optionL;
     int remap_commandL2backquote;
 
+    int remap_commandR2optionL;
+
     int remap_controlL2commandL;
 
     int remap_enter2commandL;
@@ -113,6 +115,9 @@ namespace org_pqrs_KeyRemap4MacBook {
     int remap_jis_unify_kana_eisuu_to_commandL;
 
     // ----------------------------------------
+    int remap_application2f11;
+
+    // ----------------------------------------
     int debug;
   };
   extern Config config;
diff -urp KeyRemap4MacBook-3.0.0/src/kext/keycode.hpp KeyRemap4MacBook-3.0.0-custom/src/kext/keycode.hpp
--- KeyRemap4MacBook-3.0.0/src/kext/keycode.hpp	2007-12-07 12:26:51.000000000 +0900
+++ KeyRemap4MacBook-3.0.0-custom/src/kext/keycode.hpp	2008-02-06 23:52:02.000000000 +0900
@@ -143,6 +143,8 @@ namespace org_pqrs_KeyRemap4MacBook {
       JIS_EISUU = 102,
       JIS_KANA = 104,
 
+      APPLICATION = 110,
+
       NONE = 255,
     };
   }
diff -urp KeyRemap4MacBook-3.0.0/src/kext/remap.cpp KeyRemap4MacBook-3.0.0-custom/src/kext/remap.cpp
--- KeyRemap4MacBook-3.0.0/src/kext/remap.cpp	2008-02-02 01:35:03.000000000 +0900
+++ KeyRemap4MacBook-3.0.0-custom/src/kext/remap.cpp	2008-02-06 23:59:32.000000000 +0900
@@ -50,6 +50,15 @@ namespace org_pqrs_KeyRemap4MacBook {
 
   // ----------------------------------------
   void
+  remap_commandR2optionL(const RemapParams &params)
+  {
+    if (! config.remap_commandR2optionL) return;
+
+    RemapUtil::modifierToModifier(params, ModifierFlag::COMMAND_R, ModifierFlag::OPTION_L);
+  }
+
+  // ----------------------------------------
+  void
   remap_controlL2commandL(const RemapParams &params)
   {
     if (! config.remap_controlL2commandL) return;
@@ -797,6 +806,18 @@ namespace org_pqrs_KeyRemap4MacBook {
       isKana = ! isKana;
     }
   }
+
+  // ----------------------------------------
+  void
+  remap_application2f11(const RemapParams &params)
+  {
+    if (! config.remap_application2f11) return;
+
+    // Application => F11 (if no Modifier)
+    //if (allFlagStatus.makeFlags(params) != 0) return;
+    RemapUtil::keyToKey(params, KeyCode::APPLICATION, KeyCode::F11);
+  }
+
 }
 
 // ----------------------------------------------------------------------
@@ -823,6 +844,8 @@ org_pqrs_KeyRemap4MacBook::remap_core(co
   remap_commandL2optionL(params);
   remap_commandL2backquote(params);
 
+  remap_commandR2optionL(params);
+
   remap_enter2commandL(params);
   remap_enter2controlL(params);
   remap_enter2fn(params);
@@ -891,6 +914,9 @@ org_pqrs_KeyRemap4MacBook::remap_core(co
   remap_jis_unify_eisuu_to_kana(params);
   remap_jis_unify_kana_eisuu_to_commandL(params);
 
+  // ----------------------------------------
+  remap_application2f11(params);
+
   // ------------------------------------------------------------
   // *** Note: we need to call remap_emacsmode as possible late. ***
   // *** If qwerty2colemak is enable, Control+H... works with Colemak Keyboard Layout. ***