Remap Caps lock key to Esc in Mma 7
Asked Answered
S

1

13

TLDR: How do I get CapsLock to translate to "ShortNameDelimiter" in Mma 7?

I like pretty text in my mma notebooks, and often define functions as f[\[Alpha]_] =... so as to match the exact equation that I'm working with. As such, it involves a lot of Esc-letter-Esc sequences, and reaching for Esc every other stroke breaks my flow of typing.

Now, the CapsLock key is seldom used (I can't remember the last time I needed it), but conveniently placed (your pinky is right there!). Remapping it to Esc on vim worked wonders for me and I was wondering if there was a way to do the same in mma, without having to modify the system's keyboard layout.

I tried editing KeyEventTranslations.tr by adding the following in EventTranslations[{...

Item[KeyEvent["CapsLock"], "ShortNameDelimiter"]

but that had no effect. Is there another way to do it? Is CapsLock not the correct identifier? If it helps, I'm using Mma7 student version on a Mac.

Scullion answered 27/4, 2011 at 18:22 Comment(18)
hints.macworld.com/article.php?story=20060825072451882 Read How do I remap the Caps Lock key to the Escape key?Wheelman
@belisarius: That article talks about remapping for the entire system, which, as I've mentioned in the question, is not something I want to do. HOW ELSE WILL I YELL ON THE INTERNETZ?? ;)Scullion
@yoda Please be my guest: yell as much as you want :) I wanted to show you that there is something there that a standard remapping can't do.Wheelman
@belisarius: The mac version for which that article is written is way outdated. OS X changed how they handle key bindings etc since then and so I don't think that will be applicable. But I know you use Windows, so you're excused :) Anyway, just in case some one else is reading: If you ever need to remap keys & custom define them system wide on OS X, Ukelele is a great software.Scullion
I suspect that the Caps/Scroll/NumLock keys may not be passed to, or read by, Mathematica.Reorientation
@Mr. Wizard: That's interesting... I hadn't thought of that. My concern was that modifying internal files might be disabled because I'm using a student version. Also, in the past, similar fixes that have worked for others have not worked for me.Scullion
Your last sentence is disturbing. Anyway, since the Lock keys toggle lights on the keyboard, which is done through the computer rather than wired into the keyboard, there must be some hook that handles this, and it probably interferes with what you want. I recall that games that use CapsLock for other functions often break the indicator light behavior in some way, suggesting the need intercept this hook.Reorientation
@Mr. Wizard: I mentioned that point because I suspect they weren't working because it's not a full version. Perhaps someone @ WRI can weigh in on that. Regardless, I'm interested in a solution, even if it's for v8, because I don't think I'll be stuck with student v7 for long.Scullion
yoda, I understood your point, and I found it disturbing.Reorientation
@Reorientation I think you're right. EventHandler doesn't see pressing the CapsLock key as a keydown event.Sulk
@Sjoerd: EventHandler doesn't see Ctrl, Opt/Alt or Cmd keys either, yet Mma is aware of them.Scullion
@yoda. I see. But Dynamic[CurrentValue["ModifierKeys"]] shows those keys but not the CapsLock key.Sulk
@Sjoerd: You're right... perhaps mma truly does not know about CapsLock.Scullion
by the way... U YELL ON TEH INTERNETZ BY HO:DING DOWN THE SHIFT KEY THE HOL TIME THATZ WHY THE SPE::ING IZ SO BAD!Reorientation
@yoda You could try to use external program like AutoHotkey as Istvan Zachar suggest. I have not tried it.Amitie
@Alexy: I think that's probably the way to go. I have a couple of options for external programs that I can use. I'll probably post the solution once I've tried them over the weekend.Scullion
@yoda I don't think the student version of Mathematica has anything special to prevent modification of internal files from working.Misappropriate
@Brett: Thanks for the info. I'm convinced that this time it was just because it cannot be done. I probably should revisit my earlier attempts at modifying and see if I was missing something.Scullion
A
7

Modifier keys are handled quite specially, and I doubt Mathematica will be able to override the system. You probably have to do this in a layer between Mathematica and the OS. BUT, it is possible to make the key behave different depending on the application you are in. Thus with a bit of work, it MAY be possible to have the capslock key behave differently only in Mathematica.

edit: I did not see you say which operating system you had, so I've added Mac instructions.


Windows

For example, if you have Windows, you can use the program called http://www.autohotkey.com/ . It specifically has a feature where you can bind a key to a script, specifically the following script:

How can a hotkey or hotstring be made exclusive to certain program(s)?

In other words, I want a certain key to act as it normally does except when a specific window is active. In the following example, NumpadEnter is made to perform normally except when a window titled "CAD Editor" is active. Note the use of the $ prefix in "$NumpadEnter", which is required to let the hotkey "send itself":

$NumpadEnter::
IfWinNotActive, CAD Editor
{
    Send, {NumpadEnter}
    return
}
; Otherwise, the desired application is active, so do a custom action:
Send, abc
return

This next example is more pure than the above, but it will only work if the "CAD Editor" application is designed to ignore the NumpadEnter key itself. The tilde prefix (~) makes NumpadEnter into a non-suppressed hotkey, meaning that the NumpadEnter keystroke itself is always sent to the active window, the only difference being that it triggers a hotkey action. The ~ feature requires Windows NT/2k/XP.

~NumpadEnter::
IfWinNotActive, CAD Editor
    return
; Otherwise, the desired application is active, so do a custom action:
Send, abc
return

To quote from "MRCS" in this forum post, you may find the following useful:

The first one I named CapsLockR.ahk and contains the following script:

CapsLock UP::Run C:\Documents and Sett...[path to script]...\CapsLock.ahk 

The second one is named CapsLock.ahk and has this script:

GetKeyState, state, CapsLock, T 
if state = D 
    SetCapsLockState, off 
else 
    SetCapsLockState, on 
exit

Thus worse comes to worst, if you are having trouble modifying the "Behave like Foo if Active Window = Mathematica else behave like Bar" script, you can tack on this to manually toggle the CapsLock state I think. Googling will also reveal more results.


Linux

I know that on Linux, you can use the program called xbindkeys to bind the CapsLock to a script, from which you can in turn call xdo if you detect Mathematica is one of the topmost windows (e.g. via Getting pid and details for topmost window , or xdotool getwindowfocus) or worse-comes-to-worst, you can just have a script which toggles your configuration between CapsLock -> xdotool key Escape, xdotool type "whatever", xdotool key Escape ("Mathematica mode") and "normal mode"... though that may prevent you from YELLING AT MATHEMATICIANS OVER INSTANT MESSAGING WHILE DOING MATHEMATICS. Unless you You may need to find some way to programatically toggle CapsLock, perhaps by creating a dummy CapsLock key (though that's an extreme hack, it is likely one can find some kind of library; perhaps Anybody know how to toggle caps lock on/off in Python? may be useful). (This issue could be avoided by using a key besides CapsLock, or not caring that you want to keep your CapsLock functionality; you could also just turn another key you never use into CapsLock.)


Mac

Mac may have similar tools. For example, you can get xdotool like on Linux above via the MacPorts project. I hear the CapLock key cannot normally be rebound as easily on Mac, so if you can deal with another key it may be much easier. But theoretically it should be possible...

If you wish to use CapsLock, you can use PCKeyboardHack http://pqrs.org/macosx/keyremap4macbook/extra.html to remap the CapLock key to something which will tell OS X to let you remap the CapsLock. Then you remap it, then bind the key using Quicksilver to a script that makes calls xdotool to check if you're in Mathematica also also to issue the :esc:...:esc: if you are (see the Linux section of this answer). Otherwise you simulate a keypress on the CapsLock. But you remapped CapsLock! So you might need to make another dummy key you never use into the CapsLock key, and trigger a keypress on that using Cocoa libraries or a simple AppleScript. If you wish to pursue the CapsLock route, you might find Using Caps Lock as Esc in Mac OS X useful.

Alien answered 6/5, 2011 at 8:33 Comment(4)
Actually, the asker did write: "I'm using Mma7 student version on a Mac." but I think this idea is still helpful.Reorientation
If everyone who makes mistakes like that "can't read" then we are 99.99% illiterate.Reorientation
Thanks, I do know about the PCKeyboardHack and it's sitting on my machine, waiting to be tried out. Haven't had the time yet :/. Moreover, I misspoke when I wrote above that I remapped CapsLock to Esc in vim. That was on my Linux machine and you can do that with xmodmap. Remapping CapsLock is not straightforward at all on a mac. I'll update it with my progress shortly.Scullion
I have also seen statements that "Caps Lock rebounds much slower" on Macs (or something like that). However, I've remapped my Caps Lock to be control and have it set so that selecting something (text, a file etc) and double-pressing ctrl sends it to Launchbar (never mind what that is). It works no matter how fast I do it. So I do not think that claim is correct.Ferreous

© 2022 - 2024 — McMap. All rights reserved.