Note that there are some explanatory texts on larger screens.

plurals
  1. POEnabling option-key shortcuts in MATLAB for Mac
    text
    copied!<p>Since R2009b, MATLAB has had marvelously customizable keyboard shortcuts through its <a href="http://blogs.mathworks.com/desktop/2009/09/28/configurable-keyboard-shortcuts-have-arrived/" rel="nofollow noreferrer">Keyboard Shortcuts Preferences</a>. This works very well for customizing shortcuts using command and control on a Mac.</p> <p>Unfortunately, those keybindings seem to be unable to override MATLAB's built-in character map. For example, if I define option-f as <code>cursor-next-word</code> (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 <code>ƒ</code> character! I believe this is from the character map (perhaps as opposed to the input map?). Neither <a href="http://www.mathworks.com/matlabcentral/fileexchange/24615-editormacro-assign-a-macro-to-a-keyboard-key-stroke-in-the-matlab-editor-and-command-window" rel="nofollow noreferrer">EditorMacro</a> nor <a href="http://www.mathworks.com/matlabcentral/fileexchange/25089" rel="nofollow noreferrer">KeyBindings</a> are able to override this behavior.</p> <p>I stumbled across <a href="https://stackoverflow.com/questions/1759413/how-do-i-make-a-character-in-matlab/1786487#1786487">this answer from a tangentially related question</a> 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:</p> <p>I needed to change the keycodes to remap to have 'pressed' in the string, like so:</p> <pre><code>map = { '$' '^' '#' char(181) % might be useful for text formatting }; </code></pre> <p>to:</p> <pre><code>map = { 'alt pressed F' '^' 'alt pressed B' char(181) % might be useful for text formatting }; </code></pre> <p>Unfortunately, after running the code, pressing option-f yields <code>cursor-next-word</code> and the <code>ƒ</code> character, just like before. However, if I disable the <code>cursor-next-word</code> binding from the preferences, then I get <strong>both</strong> <code>ƒ</code> and <code>^</code>! Indeed, even if I use a simple action like <code>pressed F</code>, the KeyReplacementAction doesn't replace the action but rather <em>augments</em> it. It seems like this behavior is unique to MATLAB on OS X.</p> <p>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?</p> <hr> <p><strong>Edit</strong>: 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 <code>inputMap</code> or the <code>keymap</code>, and in both cases I augment the keybinding instead of replacing it. I used reflection to 'unprotect' the <code>consume()</code> method for AWTEvents, but the effect was the same as before.</p> <p>Following the stack trace around, it appears as though the keyEvent is falling through to an instance of <code>javax.swing.KeyboardManager</code>. 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.</p> <hr> <p><strong>Edit 2</strong>: <a href="https://stackoverflow.com/users/36472/flolo">flolo</a>'s answer spurred me to look into X11's keymaps. Several notes: </p> <ul> <li>Matlab does not seem to respect <code>~/.Xmodmap</code> or any currently-loaded modmaps. </li> <li>Matlab makes use of the <code>$XKEYSYMDB</code> environment variable if it exists at startup. Otherwise, it loads it from <code>$MATLAB/X11/app-defaults/XKeysymDB</code>.</li> <li>The whole <code>$MATLAB/X11/app-defaults/</code> directory looks very interesting; perhaps some hackery there could make this work?</li> <li>Where <em>are</em> the X11 keymaps on a Mac? How does MATLAB switch to international keyboard layouts?</li> </ul> <hr> <p><strong>Edit 3</strong>: Hrm, I think X11 is a red herring. <code>lsof -c MATLAB</code> shows that it is accessing <code>/System/Library/Keyboard Layouts/AppleKeyboardLayouts.bundle</code>. Working on that now…</p> <hr> <p><strong>Edit 4</strong>: MATLAB does indeed use the system keyboard layout. I created one without any bindings as <a href="https://stackoverflow.com/users/616736/r-m">R.M.</a> 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.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload