Link to home
Start Free TrialLog in
Avatar of RalphG
RalphG

asked on

Using SendInput to emulate keyboard for MS Windows in "C" not C++

Hello everyone.
                      Some time ago I was helped by EE in my goal to create a program that could send keys to a Microsoft Windows application, ranging from Win 98 to XP.

The program will need to do all sorts of things like copy applications from a CD and then by launching the copied application will send keyboard instructions to it so as to perform the programs install routine that would otherwise take up my time.

I have written much of the program so far but keep getting stuck on the send keys part.

Below is my test code. Would someone please explain to me what I am doing so wrong with the SendInput routine.

Oh. I am developing on a Windows XP Pro SP1 machine using "Wedit Win32 Version 3.3" that uses the "lcc-win32" copiler. When written, however I would like the program to run on any Windows machine from 98 to XP.

Thanks in advance Ralph.



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <shellapi.h>

int main(int argc,char *argv[]) {
      if (argc == 1) {
            Beep;
            printf("The program has started\n");
            window_shopping();
            printf("The program has ended\n");
      }
      return 0;
}


void window_shopping(){
      /*Searches for a window and does not release until window is found*/
      printf("Looking for the window\n");
      BOOL bFound = FALSE;
      while (!bFound){
                EnumWindows(EnumWindowsProc, (LPARAM)&bFound);
                if (!bFound){
                    Sleep(250);
            }
            else{
                  bFound = TRUE;
            };
      };
      printf("Found the window and fired keys\n");
}


BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam){
                // Detect the window.
                BOOL bIsOurWindow = FALSE;
      char *sTitle = "";
      GetWindowText (hWnd, sTitle, 255);
      
      if (strcmp("Untitled - Notepad",sTitle) == 0){
            printf("## %s ##\n",sTitle);
            bIsOurWindow = TRUE;
      }
      else{
            // Put this back in to show all window titles encountered
            //printf("%s\n",sTitle);
            BOOL bIsOurWindow = FALSE;
      };

    if (bIsOurWindow){
          INPUT myInputs[2];
      myInputs[0] = KEYBDINPUT{VK_TAB};
      myInputs[1] = {VK_TAB};
          SetFocus(hWnd);
          SendInput(1, myInputs, sizeof(myInputs[0]));
          SendInput(1, 33, sizeof(myInputs[0]));
    };

      return 1;
}
            
ASKER CERTIFIED SOLUTION
Avatar of KurtVon
KurtVon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RalphG
RalphG

ASKER

Thankyou so much for helping KurtVon.

The compile problem is my error all the time, it never compiles, and the "33" was an attempt to use anything other than a correct VK_TAB just to see if I could get any more info from the compile errors, i.e. break the program to help find other breaks etc.

After putting your code in my program I now get the three following compile errors:

"Error C:\lcc\projects\autofix\autofix.c: 30 type error in argument 1 to 'memset'; found 'struct tagINPUT' expected 'pointer to void'"

"Error C:\lcc\projects\autofix\autofix.c: 32 unknown field 'wVK' of struct tagKEYBDINPUT"

"Error C:\lcc\projects\autofix\autofix.c: 37 unknown field 'wVK' of struct tagKEYBDINPUT"

This is typical of the problems I have been having! I am sure the answer is there it just keeps passing me by.

By the way if I put "memset(&myInput, 0, sizeof(myInput));" instead of "memset(myInput, 0, sizeof(myInput));" the first compile error goes away leaving only the two "wKV" errors. Am I correct to create the pointer "&myInput" as shown above?
Avatar of RalphG

ASKER

Just remembered, the following link appears to explain that I do require an array for the SendInput procedure, atleast that what it says:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/KeyboardInput/KeyboardInputReference/KeyboardInputFunctions/SendInput.asp 
Avatar of RalphG

ASKER

A ha! The following compiles fine:

if (bIsOurWindow)
{
      INPUT myInput;

          memset(&myInput, 0, sizeof(myInput));
          myInput.type = INPUT_KEYBOARD;
          myInput.ki.wVk = VK_MENU;

          SetFocus(hWnd);
          SendInput(1, &myInput, sizeof(myInput));

          myInput.ki.wVk = VK_DOWN;
          SendInput(1, &myInput, sizeof(myInput));
}

Changes include:
"myInput" becomes "&myInput"          (Note preceding "&")
"wVK" becomes "wVk"                       (Note case)
Whoops.  Sorry about the typos.  Glad it worked out.