Search code examples
c#.netkeyboardsendinput

KeyEventArgs to VirtualKeyCode


I'm trying to make use of the WindowsInputSimulator library to help me simulate keyPresses.

The software will consist of a client and a server. When a key is entered on the Client, it's KeyEventArgs are sent to the Server. The server then does the following with it:

public void SendKeyDown(Keys keyCode, Keys modifiers)
{
  uint nonVK = MapVirtualKey((uint)keyCode, 2);
  char mappedChar = Convert.ToChar(nonVK);
  if (modifiers.Equals(Keys.None))
  {
    VirtualKeyCode vkc;
    if (Enum.TryParse(VkKeyScan(mappedChar).ToString(), out vkc))
    {
      InputSimulator.SimulateKeyDown(vkc);
    }
  }
  else
  {
    //Find out which modifier we're working with.
    uint modVK = MapVirtualKey((uint)modifiers, 2);
    char modifierChar = Convert.ToChar(modVK);
    VirtualKeyCode vkc, modVkc;
    if (Enum.TryParse(VkKeyScan(mappedChar).ToString(), out vkc)
        && Enum.TryParse(VkKeyScan(modifierChar).ToString(), out modVkc))
    {
      InputSimulator.SimulateModifiedKeyStroke(modVkc, vkc);
    }
  }
}

Which works for single keys. However, I'm trying to work with modifier keys as well, and I'm running in to some trouble. For example, pressing SHIFT + K produces "k2" Which leads me to believe either my transation into VirtualKeyCodes is wonky, or something else is.

Also, when sending these commands, should I catch only the KeyDown / KeyUp events? Or should I also watch for the KeyPress event? I should be able to wrok with arrow keys and non-Character keys as well, which makes me think I should just ignore the KeyPress.

EDIT: Also, how would I know when I'm working with multiple modifiers? How should I be stringing them together?

Thoughts? Thanks!


Solution

  • I was able to get it working with the following. Keep in mind this works for a SINGLE modifier, and a single CHARACTER. Special characters don't yet work with this code, but I figure it's a step in the right direction, and answered my immediate question.

        public void SendKey(int keyValue, Keys modifiers)
        {
            VirtualKeyCode key;
            if (modifiers.Equals(Keys.None))
            {
                if (Enum.TryParse(VkKeyScan(((char)keyValue)).ToString(), out key))
                {
                    InputSimulator.SimulateKeyDown(key);
                    InputSimulator.SimulateKeyUp(key);
                }   
            }
            else if (modifiers.Equals(Keys.Shift) && keyValue >= (int)Keys.A && keyValue <= (int)Keys.Z)
            {
                if (Enum.TryParse(VkKeyScan(((char) keyValue)).ToString(), out key))
                {
                    InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.SHIFT, key);
                }
            }
            else if (modifiers.Equals(Keys.Control) && keyValue >= (int)Keys.A && keyValue <= (int)Keys.Z)
            {
                if (Enum.TryParse(VkKeyScan(((char)keyValue)).ToString(), out key))
                {
                    InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, key);
                }
            }
            else if (modifiers.Equals(Keys.Alt) && keyValue >= (int)Keys.A && keyValue <= (int)Keys.Z)
            {
                if (Enum.TryParse(VkKeyScan(((char)keyValue)).ToString(), out key))
                {
                    //Alt is named MENU for legacy purposes.
                    InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.MENU, key);
                }
            }
    
        }