Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Need Help with SendInput for key commands

Need Help with SendInput for key commands

Scheduled Pinned Locked Moved C#
help
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Luke Dyer
    wrote on last edited by
    #1

    Hey everyone I can successfully use SendInput to send mouse clicks, but I am having trouble getting it to work with sending keys. Please can someone tell me where I have gone wrong :)

        public struct KEYDBINPUT
        {
            public Int16 wVk;
            public Int16 wScan;
            public Int32 dwFlags;
            public Int32 time;
            public Int32 dwExtraInfo;
            public Int32 \_\_filler1;
            public Int32 \_\_filler2;
        }
    
        public struct INPUT
        {
            public Int32 type;
            public KEYDBINPUT ki;
        }
    
        \[DllImport("user32")\]
        public static extern int SendInput(int cInputs, ref INPUT pInputs, int cbSize);
    
        public const int INPUT\_KEYBOARD = 1;
        public const int KEYEVENTF\_KEYUP = 0x0002;
    
        public void sendKey(short key)
        {
            INPUT inputDown = new INPUT();
            inputDown.type = INPUT\_KEYBOARD;
            inputDown.ki.dwFlags = 0;
    
            INPUT inputUp = new INPUT();
            inputUp.type = INPUT\_KEYBOARD;
            inputUp.ki.dwFlags = KEYEVENTF\_KEYUP;
    
            inputDown.ki.wVk = key;
            SendInput(1, ref inputDown, Marshal.SizeOf(inputDown));
    
            inputUp.ki.wVk = key;
            SendInput(1, ref inputUp, Marshal.SizeOf(inputUp));
        }
    

    Thanks a lot Luke

    D 1 Reply Last reply
    0
    • L Luke Dyer

      Hey everyone I can successfully use SendInput to send mouse clicks, but I am having trouble getting it to work with sending keys. Please can someone tell me where I have gone wrong :)

          public struct KEYDBINPUT
          {
              public Int16 wVk;
              public Int16 wScan;
              public Int32 dwFlags;
              public Int32 time;
              public Int32 dwExtraInfo;
              public Int32 \_\_filler1;
              public Int32 \_\_filler2;
          }
      
          public struct INPUT
          {
              public Int32 type;
              public KEYDBINPUT ki;
          }
      
          \[DllImport("user32")\]
          public static extern int SendInput(int cInputs, ref INPUT pInputs, int cbSize);
      
          public const int INPUT\_KEYBOARD = 1;
          public const int KEYEVENTF\_KEYUP = 0x0002;
      
          public void sendKey(short key)
          {
              INPUT inputDown = new INPUT();
              inputDown.type = INPUT\_KEYBOARD;
              inputDown.ki.dwFlags = 0;
      
              INPUT inputUp = new INPUT();
              inputUp.type = INPUT\_KEYBOARD;
              inputUp.ki.dwFlags = KEYEVENTF\_KEYUP;
      
              inputDown.ki.wVk = key;
              SendInput(1, ref inputDown, Marshal.SizeOf(inputDown));
      
              inputUp.ki.wVk = key;
              SendInput(1, ref inputUp, Marshal.SizeOf(inputUp));
          }
      

      Thanks a lot Luke

      D Offline
      D Offline
      Dominik Reichl
      wrote on last edited by
      #2

      * Use the LayoutKind.Sequential structure attribute. * You should fill the dwExtraInfo member (set it to the value returned by GetMessageExtraInfo). * The scan code should be set to the value returned by MapVirtualKey applied to the virtual key code. * Depending on whether it's an extended key, you should also specify the KEYEVENTF_EXTENDEDKEY flag. * You should use IntPtr for pointers. Using Int32 will break compatibility of your application with 64-bit operating systems. I recommend to use the definitions on P/Invoke.net: SendInput[^] Best regards Dominik


      Too many passwords to remember? Try KeePass Password Safe!

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups