Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Caps Lock with Ctrl using C#

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

like image 408
Deniz Dogan Avatar asked Nov 05 '22 19:11

Deniz Dogan


1 Answers

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/

like image 187
Kenn Avatar answered Nov 15 '22 12:11

Kenn