NO

Author Topic: Sending a key to a window that’s not in focus  (Read 3050 times)

Andre

  • Guest
Sending a key to a window that’s not in focus
« on: November 22, 2013, 08:51:28 pm »
I want to use the code from Ted Burke.

(Refer to <http://batchloaf.wordpress.com/2013/02/13/sending-a-key-to-a-window-thats-not-in-focus-c-program>)

Compiler creates following errors:
POLINK: error: Unresolved external symbol '__imp__FindWindowExA@16'.
POLINK: error: Unresolved external symbol '__imp__GetWindowTextA@12'.
POLINK: error: Unresolved external symbol '__imp__SetForegroundWindow@4'.
POLINK: error: Unresolved external symbol '__imp__SendInput@12'.
POLINK: fatal error: 4 unresolved external(s).
*** Fehlercode: 1 ***

Subsystem type is : console

Any help is appreciated.

Here is Ted's code:
Code: [Select]
#define WINVER 0x0500
#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
  // Check number of command line arguments
  if (argc < 3)
  {
    fprintf(stderr, "Too few command line arguments\n");
    fprintf(stderr, "Usage: sendkey.exe KEY_TO_SEND");
    fprintf(stderr, " WORD_FROM_TARGET_WINDOW_TITLE\n");
    return 1;
  }
     
  // Get the character to send from the first command
  // line argument
  char char_to_send = toupper(argv[1][0]);
     
  // Get first window on desktop
  HWND firstwindow = FindWindowEx(NULL, NULL, NULL, NULL);
  HWND window = firstwindow;
  TCHAR windowtext[MAX_PATH];
     
  // We need to get the console title in case we
  // accidentally match the search word with it
  // instead of the intended target window.
  TCHAR consoletitle[MAX_PATH];
  GetConsoleTitle(consoletitle, MAX_PATH);
     
  while(1)
  {
    fprintf(stderr, ".");
         
    // Check window title for a match
    GetWindowText(window, windowtext, MAX_PATH);
    if (strstr(windowtext, argv[2]) != NULL &&
        strcmp(windowtext, consoletitle) != 0) break;
         
    // Get next window
    window = FindWindowEx(NULL, window, NULL, NULL);
    if (window == NULL || window == firstwindow)
    {
      fprintf(stderr, "Window not found\n");
      return 1;
    }
  }
  fprintf(stderr, "Window found: %s\n", windowtext);
     
  // Bring specified window into focus
  SetForegroundWindow(window);
 
  // Create the desired keyboard event
  INPUT ip;
  ip.type = INPUT_KEYBOARD;
  ip.ki.wVk = char_to_send;
  ip.ki.wScan = 0;
  ip.ki.dwFlags = 0;
  ip.ki.time = 0;
  ip.ki.dwExtraInfo = 0;
     
  // Send the keyboard event to the specified window
  SendInput(1, &ip, sizeof(INPUT));
}
« Last Edit: November 22, 2013, 10:28:26 pm by Stefan Pendl »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1848
Re: Sending a key to a window that’s not in focus
« Reply #1 on: November 22, 2013, 09:11:03 pm »
add user32.lib to linker options.
or
Code: [Select]
#pragma comment(lib, "user32.lib") to source code.
« Last Edit: November 22, 2013, 09:12:57 pm by timovjl »
May the source be with you

Andre

  • Guest
Sending a key to a window that’s not in focus
« Reply #2 on: November 25, 2013, 09:27:29 am »
Thank you so much.

sajjad

  • Guest
Re: Sending a key to a window that’s not in focus
« Reply #3 on: November 17, 2014, 10:58:53 am »
(Refer to <http://batchloaf.wordpress.com/2013/02/13/sending-a-key-to-a-window-thats-not-in-focus-c-program>)

_________________
noor