![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 2
| Sending Strings using SendInput 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;
}
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. |
| TheWeird1 is offline | |
| | #2 |
| train spotter Join Date: Aug 2001 Location: near a computer
Posts: 3,359
| 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 |
| novacain is offline | |
| | #3 |
| Registered User Join Date: Jul 2009
Posts: 2
| Forgive me but how would I pass parameters to your function SendText? |
| TheWeird1 is offline | |
| | #4 |
| train spotter Join Date: Aug 2001 Location: near a computer
Posts: 3,359
| 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 |
| novacain is offline | |
![]() |
| Tags |
| sendinput, setwindowshookex |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Strings Program | limergal | C++ Programming | 4 | 12-02-2006 03:24 PM |
| Programming using strings | jlu0418 | C++ Programming | 5 | 11-26-2006 08:07 PM |
| Reading strings input by the user... | Cmuppet | C Programming | 13 | 07-21-2004 06:37 AM |
| winsock, sending c++ style strings | b00l34n | Networking/Device Communication | 17 | 05-06-2004 07:41 PM |
| sending strings from program to email | Unregistered | C++ Programming | 3 | 04-09-2002 06:23 PM |