Since R2009b, MATLAB has had marvelously customizable keyboard shortcuts through its Keyboard Shortcuts Preferences. This works very well for customizing shortcuts using command and control on a Mac.
Unfortunately, those keybindings seem to be unable to override MATLAB's built-in character map. For example, if I define option-f as cursor-next-word
(a la emacs), it accepts the binding. Hitting the key combination does properly move the cursor to the next word, but it additionally prints the ƒ
character! I believe this is from the character map (perhaps as opposed to the input map?). Neither EditorMacro nor KeyBindings are able to override this behavior.
I stumbled across this answer from a tangentially related question which gives me hope. In short, he defined a Java class that can handle keyboard events and replace them with other keystroke input. The solution, however, only works as prescribed on Windows. The following modifications were required to run on a Mac:
I needed to change the keycodes to remap to have 'pressed' in the string, like so:
map = {
'$' '^'
'#' char(181) % might be useful for text formatting
};
to:
map = {
'alt pressed F' '^'
'alt pressed B' char(181) % might be useful for text formatting
};
Unfortunately, after running the code, pressing option-f yields cursor-next-word
and the ƒ
character, just like before. However, if I disable the cursor-next-word
binding from the preferences, then I get both ƒ
and ^
! Indeed, even if I use a simple action like pressed F
, the KeyReplacementAction doesn't replace the action but rather augments it. It seems like this behavior is unique to MATLAB on OS X.
It seems as though I'm simply not overriding the correct keymap. I've tried digging through the Java runtime, but I'm not familiar enough with the event dispatch model to know where to look next. Perhaps something within Java's OS-level keymap?
Edit: I've since done some more digging around. It appears as though the Mac version of MATLAB does not properly respect the 'consumed' property of a keyEvent. I can attach the KeyReplacementAction to either the inputMap
or the keymap
, and in both cases I augment the keybinding instead of replacing it. I used reflection to 'unprotect' the consume()
method for AWTEvents, but the effect was the same as before.
Following the stack trace around, it appears as though the keyEvent is falling through to an instance of javax.swing.KeyboardManager
. It looks like I should be able to unbind keystrokes within the KeyboardManager, but I cannot figure out how to access the instance from the MATLAB handles I have. Perhaps someone more familiar with Swing's event model and the Java debugger could get farther.
Edit 2: flolo's answer spurred me to look into X11's keymaps. Several notes:
- Matlab does not seem to respect
~/.Xmodmap
or any currently-loaded modmaps. - Matlab makes use of the
$XKEYSYMDB
environment variable if it exists at startup. Otherwise, it loads it from$MATLAB/X11/app-defaults/XKeysymDB
. - The whole
$MATLAB/X11/app-defaults/
directory looks very interesting; perhaps some hackery there could make this work? - Where are the X11 keymaps on a Mac? How does MATLAB switch to international keyboard layouts?
Edit 3: Hrm, I think X11 is a red herring. lsof -c MATLAB
shows that it is accessing /System/Library/Keyboard Layouts/AppleKeyboardLayouts.bundle
. Working on that now…
Edit 4: MATLAB does indeed use the system keyboard layout. I created one without any bindings as R.M. suggested. This appeared to work — MATLAB does behave properly. Unfortunately, it also breaks my custom Cocoa keybindings in all other programs. Close, but no cigar. (Close enough, in fact, that R.M. won the +500 bounty due to a brief thought that it had worked… until I attempted to compose my congratulatory comment and discovered that I couldn't navigate the text field as usual.)