d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Send Key Presses > And Possible Mouse Clicking
Add Reply New Topic New Poll
Member
Posts: 14,861
Joined: Jun 28 2007
Gold: 35,165.53
Oct 5 2014 12:00pm
Trying to make a program to press Keys for me (and maybe even type strings for me) and activate the window I want. I've got a way figured out using INPUT and SendInput but it seems really long and I'd have to declare every key I want (not to mention typing a string this way seems very long and complicated)
Figuring out how to activate a window and check to see if it's the window I want right now. Can someone tell me how to press keys, type strings, and possible send the key press/strings to a non-active window
Code
HWND Notepad = FindWindow(0, TEXT("Untitled - Notepad"));
SetForegroundWindow(Notepad);


Code
#include <windows.h>
#include <winuser.h>
using namespace std;

void main()
{
INPUT a;
a.type = INPUT_KEYBOARD;
a.ki.wScan = 0;
a.ki.time = 0;
a.ki.dwExtraInfo = 0;

a.ki.wVk = 0x41;
a.ki.dwFlags = 0;
HWND WNDNotepad = FindWindow(0, TEXT("Notepad"));
while (1){
SendInput(1, &a, sizeof(INPUT));
Sleep(500);
}

aaa
}


This post was edited by InunoTaishou on Oct 5 2014 12:02pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 5 2014 08:20pm
That's the windows api for you, deal with it.

You can pretty much forget doing anything meaningful with this as if you are trying to manipulate an application that has textboxes, you will only be able to send text to what ever is currently highlighted.

If you are trying to create automation you should sooner look into code caves and code injection into the running process and analysis their functions so that you may call them within their user space.
Member
Posts: 14,861
Joined: Jun 28 2007
Gold: 35,165.53
Oct 5 2014 08:36pm
Quote (AbDuCt @ Oct 5 2014 08:20pm)
That's the windows api for you, deal with it.

You can pretty much forget doing anything meaningful with this as if you are trying to manipulate an application that has textboxes, you will only be able to send text to what ever is currently highlighted.

If you are trying to create automation you should sooner look into code caves and code injection into the running process and analysis their functions so that you may call them within their user space.


That's one of the main reasons to do it but I do have other reasons.

Anyways I did find a shorter solution but I would want to know if there's any other ways to do it, faster, best, stronger, more efficient.

Code
#define VK_ENTER 0x0D
#define VK_E 0x45
#define VK_K 0x4B
#define VK_L 0x4C
#define VK_Y 0x59
HWND WinActive(HWND window){
if (GetForegroundWindow() == window)
return window;
else
return 0;
}

HWND GetActive(){
return GetForegroundWindow();
}

int main()
{
HWND Notepad = FindWindow(TEXT("Notepad"), NULL);
HWND edit = FindWindowEx(Notepad, NULL, TEXT("Edit"), NULL);
int count = 1000;
while (count){
PostMessage(edit, WM_KEYDOWN, VK_K, 1);
PostMessage(edit, WM_KEYDOWN, VK_Y, 1);
PostMessage(edit, WM_KEYDOWN, VK_L, 1);
PostMessage(edit, WM_KEYDOWN, VK_E, 1);
PostMessage(edit, WM_KEYDOWN, VK_ENTER, 1);
--count;
}
}


And another solution someone told me was "SendMessage()". I haven't tested this one yet but it looks like it only works when the I want to type to is set as the active window.
Member
Posts: 15
Joined: Aug 31 2014
Gold: 0.00
Oct 6 2014 02:41am
As a previous poster said, that's the (C-based, fucking ugly) windows API. You can, however, wrap those calls up in some C++ classes. Should be easy.
Member
Posts: 3,099
Joined: Nov 26 2006
Gold: 0.10
Oct 6 2014 08:26pm
postmessage is probably youre best bet.

But don't forget to send the KEYUP event if you're going to go that route.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll