Need Help with SendInput for key commands
-
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
-
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
* Use the
LayoutKind.Sequential
structure attribute. * You should fill thedwExtraInfo
member (set it to the value returned byGetMessageExtraInfo
). * The scan code should be set to the value returned byMapVirtualKey
applied to the virtual key code. * Depending on whether it's an extended key, you should also specify theKEYEVENTF_EXTENDEDKEY
flag. * You should useIntPtr
for pointers. UsingInt32
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!