-3
    private static IntPtr KeyboardHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        int vkCode = Marshal.ReadInt32(lParam);
        if (nCode >= 0 && wParam == (IntPtr)KeyboardMessages.WM_KEYDOWN)
        {
            Console.WriteLine((Keys)vkCode);
        }
        if (nCode >= 0 && wParam == (IntPtr)KeyboardMessages.WM_KEYUP && vkCode == (int)myKey.CapsLock)
        {
            // Turn CapsLock off
        }
        return CallNextHookEx(_hookIDKeyboard, nCode, wParam, lParam);

I got a low level keyboard hook and try to turn capslock off, after it has been pressed. I tried to use keybd_event and SendInput, which both sends me a "Capital" in Console, but doesn't turn it off.

Whats the best way to turn Capslock off, after it has been pressed?

koin
  • 203
  • 2
  • 12
  • 2
    It is silly to do this with a global hook. If you want to disable caps lock system-wide, do it by editing the registry. This amounts to changing the "Scancode Map" in `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout`. You can disable the caps lock key entirely, or even change it into something useful, like Ctrl. Examples [here](http://johnhaller.com/useful-stuff/disable-caps-lock). – Cody Gray - on strike Jun 15 '16 at 08:45
  • @CodyGray but this isn't all I wanna do, also I don't want to reboot after changing it everyime. – koin Jun 15 '16 at 13:19
  • A keyboard hook is not a valid way to "turn Capslock into a "modifier" like shift, ctrl." It will not work the way you want it to. I have already given you a solution for doing that. Prying the key cap off of your keyboard is another alternative, and it doesn't even require a restart. – Cody Gray - on strike Jun 15 '16 at 14:17
  • @CodyGray I still wan't to be able to press the key and don't have just another shift or ctrl key or disable it compleatly. My goal is to be able to press Capslock & Q (or W,E...) to execute something like a Macro or a Single keystroke. – koin Jun 15 '16 at 14:42
  • https://autohotkey.com/ – Cody Gray - on strike Jun 15 '16 at 14:44
  • @CodyGray I use AutoHotkey already with something like this "SetCapsLockState, AlwaysOff CapsLock & q :: Send {a}", but Id like to get same functionality into my Programm, because I'd like to extend it, share it and I'm interessted in how this works. – koin Jun 15 '16 at 14:52
  • Well, AutoHotKey is extremely extensible. And certainly not written in C#. – Cody Gray - on strike Jun 15 '16 at 14:57

1 Answers1

1

You can check here if you used keybd_event correctly to turn off the caps lock. However, in your case you need to be aware of the following. If you try this from within your WM_KEYUP, you will be interfering with the current key-up handling which likely causes the problem that you experience. You should send these keys asynchronously. A better approach is to block the initial caps lock keyup/keydown message so the caps lock is not switched on in the first place. From the documentation of the KeyboardProc:

If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the rest of the hook chain or the target window procedure.

Community
  • 1
  • 1
Ton Plooij
  • 2,583
  • 12
  • 15
  • ty I'll try this out. I wan't to turn Capslock into a "modifier" like shift, ctrl. When I block the Keydown message of capslock, am I still able to get Something like "CapsLock & Q" to return me an "A" on screen? – koin Jun 15 '16 at 13:28