Sending Strings using SendInput

This is a discussion on Sending Strings using SendInput within the Windows Programming forums, part of the Platform Specific Boards category; Hey everyone, I'm a beginner to C++ and I am having some issues with ease of outputting strings through the ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    Sending Strings using SendInput

    Hey everyone,

    I'm a beginner to C++ and I am having some issues with ease of outputting strings through the SendInput() API.

    Currently I am stuck at outputting one key at a time which in itself isn't a problem, but gets tedious if you have 9 different strings with up to 15 characters each. My goal is to be able to set a few hotkeys that call the SendMessage() function that can send a sentence.

    This is what I have currently.

    Code:
    #define WIN32_LEAN_AND_MEAN
    #define _WIN32_WINNT 0x1337
    
    
    #include <windows.h>
    #include <winable.h> /* GNU GCC specific */
    
    
    using namespace std;
    
    HHOOK keyboardHook;
    
    void GenerateKey(BYTE vk)
    {
    
        INPUT Input;
        ZeroMemory(&Input, sizeof(Input));
        Input.type = INPUT_KEYBOARD;
        Input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
        Input.ki.wVk = vk;
        SendInput(1, &Input, sizeof(INPUT));
    
        return;
    }
    
    LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);
    
        // If key is being pressed
        if (wParam == WM_KEYDOWN)
        {
    
            switch (p->vkCode)
            {
    
                //Define Hotkeys here
            case VK_SCROLL:
            {
    
                GenerateKey((UCHAR)VkKeyScan('C'));
                GenerateKey((UCHAR)VkKeyScan('o'));
                GenerateKey((UCHAR)VkKeyScan('d'));
                GenerateKey((UCHAR)VkKeyScan('i'));
                GenerateKey((UCHAR)VkKeyScan('n'));
                GenerateKey((UCHAR)VkKeyScan('g'));
                GenerateKey((UCHAR)VkKeyScan(' '));
    
                GenerateKey((UCHAR)VkKeyScan('i'));
                GenerateKey((UCHAR)VkKeyScan('s'));
                GenerateKey((UCHAR)VkKeyScan(' '));
    
                GenerateKey((UCHAR)VkKeyScan('f'));
                GenerateKey((UCHAR)VkKeyScan('u'));
                GenerateKey((UCHAR)VkKeyScan('n'));
                GenerateKey((UCHAR)VkKeyScan('.'));
                GenerateKey((UCHAR)VkKeyScan('\n')); /* enter key */
    
            }
            break;
    
            case VK_INSERT:
                break;
            case VK_END:
                break;
            case VK_PRINT:
                break;
                */
            }
    
        }
    
        return CallNextHookEx(NULL, nCode, wParam, lParam);
    }
    
    void keepAlive()
    {
        MSG message;
        while (GetMessage(&message,NULL,0,0))
        {
            TranslateMessage( &message );
            DispatchMessage( &message );
        }
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    
        keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardHookProc, hInstance, 0);
    
        keepAlive();
    
        UnhookWindowsHookEx(keyboardHook);
    
        return 0;
    
    }
    As you can see this can get tedious.

    Is there a way to do something like
    Code:
    GenerateKey((UCHAR)VkKeyScan('Coding Is Fun'));
    Last edited by TheWeird1; 07-04-2009 at 01:13 AM.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Code:
    //off the top of my head...(not tested)
    
    void  SendText(char *szText)
    {
          for(int i=0;i<strlen(szText);i++)
         {
              GenerateKey( (UCHAR) VkKeyScan( szText[i] ) );
         }
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    Forgive me but how would I pass parameters to your function SendText?

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Code:
    //send constant known string
    SendText("Some text to send.");
    
    //use char array to hold dynamic string
    
    //declare char array and init to empty
    char szBuf[MAX_PATH]={0};
    
    //format text from variables (one string and one int) 
    _snprintf(szBuf, MAX_PATH-1, "%s of %d", szVar, iVar);
    
    //send the text
    SendText(szBuf);
    
    //etc
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. winsock, sending c++ style strings
    By b00l34n in forum Networking/Device Communication
    Replies: 17
    Last Post: 05-06-2004, 07:41 PM
  5. sending strings from program to email
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2002, 06:23 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21