views:

836

answers:

4

When reading either of these questions or the EmacsWiki article about mapping Caps Lock to Control in emacs in Windows, the best answers seem to involve the registry. My question is what a user can do when they can't modify the registry of the machine because they don't have admin rights. Is there a way to do the mapping from within emacs? This article comes oh-so-close, even saying, "As people have mentioned, you can of course map Caps-Lock to other keys instead, for example the Control key." But I can't figure out how to represent the control key (using various references that look pretty comprehensive but might not be Windows-centric), trying things like

(setq w32-enable-caps-lock nil)
(global-set-key [capslock] '[control])

and

(setq w32-enable-caps-lock nil)
(global-set-key [capslock] 'ctl-x-map)

which don't seem to work.

+4  A: 

I believe it doesn't work because Windows (or X) doesn't pass an actual event for [capslock] or [control] - it's a modifier key, like [shift]. Pressing a modifier key doesn't cause the application to get a keystroke, but if you press a modifier and a second (non-modifier) key, then you get the keystroke.

For example [a] and [A] are two different keys, one is the regular "a" and the other is essentially "shift-a". I'm sure you could set Windows up to have the "shift-a" actually send a "z" or something.

Because [capslock], [control], [shift], [meta] are all modifier keys, they don't generate key events in and of themselves for the applications.

In short, Emacs doesn't get a [control] key event that it can remap, it gets a C-a event. This is generally done by the obvious combination of [control] and [a] keys. But, the event could be generated by a different keystroke, say [F10] or even [y] (confusing, yes).

This is how I understand it. Clarifications are welcome of course.

Trey Jackson
Yes, this is the sense I was getting, but thank you for articulating it so much better than I could have.
Brendan Foote
+4  A: 

I know this does not really answer your question, but Trey Jackson basically explained the problem. To detect control key alone requires low-level keyboard hook on Windows, which I don't think Emacs implements.

One way (involving external program, but does not require admin right) is to use AutoHotkey. All you need is the following 2 line script:

CapsLock::Ctrl
LCtrl::Capslock

You will need to run this script whenever emacs is open, but thankfully Autohotkey is pretty low profile application (usually takes about a few hundreds Kb in memory only). You can for example execute this script in your .emacs so that your control key and capslock key are swapped whenever emacs is opened.

polyglot
Well done backing up from my assumed solution and finding another way. I'd looked very briefly at AutoHotKey and assumed it must require admin rights, but it looks to work like a charm. I'll edit the question and add some relevant links.
Brendan Foote
Correct me if I'm wrong, but the spelling in the second line of your script sample should be "LCtrl::CapsLock" rather than "LCtrl::Cpaslock"
Bryce Thomas
Yes of course you are right. Fixed.
polyglot
+1  A: 

The simplest way to remap CapsLock on Windows is to download this CapsLockChanger utility, choose a key to map to (Control) and put it in your Start->Startup menu.

It stays in your tray, does the job on key remapping and does not require messing around with registry.

A: 

You do not necessarily need Admin rights in order to modify the registry.
In fact, the Windows Registry is a segmented resource, much like a filesystem, and you can have granular permissions on various branches and nodes within the registry. Are you sure your assumption that you cannot change the registry, is correct? I think it is true by default on Vista, but not on previous Windows.

Cheeso
The usual method of key remapping is very low-level and per-machine (changes go somewhere into HKLM\SYSTEM) so admin rights are a must.
Anton Tykhyy