none
Global Keyboard Hooking RRS feed

  • Question


  • Hi Folks,

    I'm currently working on an application that takes keyboard keys, remap them to some other Unicode characters and send them, using Sendkeys.Send(), to applications such as Notepad, Ms Word etc. I have had somewhat minor success with global Keyboard Hook.

    At the moment, I'm sort of stuck! When I try detecting if 'Caps Lock' is ON and consequently convert the corresponding characters to Lowercase, the keyboard hook program won't pull it off.

    A piece of my code is as shown below. As you'd notice, when the 'Caps Lock' is ON and a key such as Q is pressed, the program should map it to "ɣ" and then send it to other applications, otherwise send “Ɣ” instead.

     Can any one please help me.
    Appreciate it advance.


    private GlobalKeyboardHook ghook;
    
    private string toSend;
    private bool _shiftPressed = false;
    
    private void Form1_Load(object sender, EventArgs e)
    {
      keyBuffer = new StringBuilder();
      Console.OutputEncoding = System.Text.Encoding.Unicode;              
      ghook = new GlobalKeyboardHook();
      ghook.KeyDown +=new KeyEventHandler(ghook_KeyDown);
      foreach (Keys key in Enum.GetValues(typeof(Keys)))
          ghook.HookedKeys.Add(key);
      ghook.hook();
    }
    
    private void ghook_KeyDown(object sender, KeyEventArgs e)
     {
         toSend = " ";
         textBox2.Text = e.KeyCode.ToString();
    //Convert to Lowercase if 'Caps Lock' is On             
    
        if (Control.IsKeyLocked(Keys.CapsLock))
    
          {   capLock = true;
              e.Handled = true;
          }
    
    //Remap uppercase letters into Unicode characters
    
          else
          { 
           capLock = false;
    //eat this key event to prevent it from being pressed by the OS
              e.Handled = true;
              switch(e.KeyCode)
                {
                   case Keys.Q:
                        toSend = "Ö";
                        break;
                   case Keys.F:
                        toSend = "Ŋ";
                        break;
                   case Keys.S:
                        toSend = "Ɣ";
                        break;
                   default:
                        //ghook.unhook();
                        break;
                }
    
          }
    
         if (capLock)
           {
    //MessageBox.Show("The Caps Lock key is OFF.");
            switch(e.KeyCode)
              {
                 case Keys.Q:
                      toSend = "ö";
                      break;
                 case Keys.F:
                      toSend = "ŋ";
                      break;
                 case Keys.S:
                      toSend = "ɣ";
                      break;
                 default:
                      //ghook.unhook();
                      break;
               }
         }              
    
        if (toSend != null)
          {
            ghook.unhook();
            textBox1.Text += toSend;
            SendKeys.Send(toSend);
            e.Handled = true;
            ghook.hook();  
         }
    }
    
    



    • Edited by dotComex Wednesday, May 11, 2016 5:11 AM
    Wednesday, May 11, 2016 5:05 AM

Answers

  • Viorel_, many thanks. I did that, but for some reason, it doesn't prevent locking up my keyboard's 'Caps Lock'. Here is the line that's creating problems for me.

    if (Control.IsKeyLocked(Keys.CapsLock) == true)
    {
      capLock = true;
      e.Handled = true;
    
    }
                       

    I do not know why it's locking up the 'Caps Lock'!

    • Marked as answer by dotComex Wednesday, May 18, 2016 3:04 AM
    Wednesday, May 11, 2016 7:02 AM
  • Improper usage prevented the <Caps Lock> and other keys to work properly, since 'e.Handled = true' means that the System should not respond to event in normal manner.


    • Edited by Viorel_MVP Wednesday, May 11, 2016 8:04 AM
    • Marked as answer by dotComex Wednesday, May 18, 2016 3:03 AM
    Wednesday, May 11, 2016 8:04 AM

All replies

  • Maybe you should execute ‘toSend = null’ instead of ‘toSend = “ “’.

    Wednesday, May 11, 2016 6:47 AM
  • Viorel_, many thanks. I did that, but for some reason, it doesn't prevent locking up my keyboard's 'Caps Lock'. Here is the line that's creating problems for me.

    if (Control.IsKeyLocked(Keys.CapsLock) == true)
    {
      capLock = true;
      e.Handled = true;
    
    }
                       

    I do not know why it's locking up the 'Caps Lock'!

    • Marked as answer by dotComex Wednesday, May 18, 2016 3:04 AM
    Wednesday, May 11, 2016 7:02 AM
  • Try removing all of the ‘e.Handled = true’, excepting the last one. I.e. execute it only when you detect the required letters.


    • Edited by Viorel_MVP Wednesday, May 11, 2016 7:36 AM
    Wednesday, May 11, 2016 7:33 AM
  • Man, you are genius! Thanks. Why was the e.Handled=true' creating problems?
    Wednesday, May 11, 2016 7:53 AM
  • Improper usage prevented the <Caps Lock> and other keys to work properly, since 'e.Handled = true' means that the System should not respond to event in normal manner.


    • Edited by Viorel_MVP Wednesday, May 11, 2016 8:04 AM
    • Marked as answer by dotComex Wednesday, May 18, 2016 3:03 AM
    Wednesday, May 11, 2016 8:04 AM
  • Thank you for clarification. That helps.

    Now I got another issue.

    When 'Caps Lock' is ON and if a 'shift key' and a letter say F are pressed I wanted to send a corresponding character say "Ɛ̈". The following modification doesn't seem to work. What am I doing wrong here please?

    if (Control.IsKeyLocked(Keys.CapsLock) == true)
    {
      capLock = true;  
    
     }
    if (capLock)
    //if (Control.IsKeyLocked(Keys.CapsLock) == true)
    {
      //set the flag
       capLock = false;
       switch (e.KeyCode)
         {
            case Keys.Q:
            toSend = "Ɔ";
            break;
            case Keys.H:
            toSend = "Ɣ";
            case Keys.LShiftKey:                              
            case Keys.RShiftKey:
              if ("Q"==Keys.Q.ToString())
               toSend = "Ɛ̈";
              break;
          }
       }
       if (toSend != null)
       {
          ghook.unhook();
          textBox1.Text += toSend;
          SendKeys.Send(toSend);
          e.Handled = true;
          ghook.hook();
       }
    }


    • Edited by dotComex Wednesday, May 11, 2016 12:08 PM
    • Marked as answer by dotComex Wednesday, May 18, 2016 3:03 AM
    • Unmarked as answer by dotComex Wednesday, May 18, 2016 3:03 AM
    Wednesday, May 11, 2016 12:07 PM
  • Hi dotComex,

    Actually, each post corresponds to one issue.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, May 13, 2016 9:44 AM
  • I do apologize. Thanks for letting me know.
    Tuesday, May 17, 2016 11:32 AM