views:

541

answers:

1

I'm writing (yet another, I know) keyboard remapper using C# and Visual Studio 2008.

I followed this guide to learn how to snap up low-level key presses. This works just fine for overriding e.g. normal alphabetical characters on my keyboard, but I seem to need a bit more to make Caps Lock act like Ctrl.

My understanding (which may be incorrect) is that Caps Lock and Ctrl are handled completely different from one another since Caps Lock is a toggling key whereas Ctrl is just a "normal" one.

So what I'm trying to understand here is how to make Caps Lock behave like a Ctrl key on the very lowest level and also how to make the normal Ctrl key act like a Caps Lock key.

Thanks

+1  A: 

Maintain a bool which represents expected state of caps lock. When caps lock key is hit, set the systems's caps lock value back to the bool's value. When ctrl is hit, toggle the expected state of the caps lock and set the system's cap lock value to the bool's value.

Use the following to set the initial expected state:

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.Winapi)] 
public static extern short GetKeyState(int keyCode); 
bool CapsLock = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;

Add special logic in HookCallback (from the link you provided) for when ctrl and caps lock are hit. Caps lock is when lParam is &H14. Ctrl is when lParam is &H11.

To get/set the system's caps lock value:

http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/fb8308e5-7620-43cc-8ad8-be67d94708fa/

Kenn