Sending Keystrokes To Other Windows

I was trying to make a program that would send keystroke to other application windows. After a lot of Googling I found this guy's code: http://forum.codecall.net/121328-post1.html

I modified it a bit so I could send more than one key at a time. However, I feel like this is a really clunky method and think there must be a much more efficient way out there.
What is there I could change on this to make it a bit better?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <windows.h>
#include <winable.h> /* Dev-C++ specific */
#include <string>
using namespace std;

/* HWND = "Window Handle" */
HWND GameWindow = FindWindow(0, "Window");

/* This is a function to simplify usage of sending keys */
void GenerateKey(int vk, BOOL bExtended) {
    
    
    KEYBDINPUT  kb = {
        0
    }
    ;
    INPUT       Input = {
        0
    }
    ;
    
    /* Generate a "key down" */
    if (bExtended) {
        kb.dwFlags  = KEYEVENTF_EXTENDEDKEY;
    }
    
    kb.wVk  = vk;
    Input.type  = INPUT_KEYBOARD;
    Input.ki  = kb;
    SendInput(1, &Input, sizeof(Input));
    
    /* Generate a "key up" */
    ZeroMemory(&kb, sizeof(KEYBDINPUT));
    ZeroMemory(&Input, sizeof(INPUT));
    kb.dwFlags  =  KEYEVENTF_KEYUP;
    if (bExtended) {
        kb.dwFlags |= KEYEVENTF_EXTENDEDKEY;
    }
    
    kb.wVk = vk;
    Input.type = INPUT_KEYBOARD;
    Input.ki = kb;
    SendInput(1, &Input, sizeof(Input));
    
    return;
    
}


int main() {
    
    while(1==1){
        
        /*
        SetForegroundWindow will give the window focus for the
        keyboard/mouse!
        */
        
        SetForegroundWindow(GameWindow);
        
        string waffle ("THIS IS ONE LINE");
        string waffle0 ("THIS IS A SECOND LINE");
        string waffle1 ("THIRD");
        string waffle2 ("FOURTH");
        string waffle3 ("FIFTH");
        string waffle4 ("SIXTH");
        
        
        for(int i=0; i < waffle.size(); i++){
            
            int c=(int)waffle[i];
            GenerateKey(c, FALSE);
            
        }
        
        
        GenerateKey(0x0D, FALSE); /* enter key */
        
        
        
        for(int i=0; i < waffle0.size(); i++){
            
            int c=(int)waffle0[i];
            GenerateKey(c, FALSE);
            
        }
        
        
        GenerateKey(0x0D, FALSE); /* enter key */
        
        for(int i=0; i < waffle1.size(); i++){
            
            int c=(int)waffle1[i];
            GenerateKey(c, FALSE);
            
        }
        
        
        GenerateKey(0x0D, FALSE); /* enter key */
        
        for(int i=0; i < waffle2.size(); i++){
            
            int c=(int)waffle2[i];
            GenerateKey(c, FALSE);
            
        }
        
        
        GenerateKey(0x0D, FALSE); /* enter key */
        
        for(int i=0; i < waffle3.size(); i++){
            
            int c=(int)waffle3[i];
            GenerateKey(c, FALSE);
            
        }
        
        
        GenerateKey(0x0D, FALSE); /* enter key */
        
        for(int i=0; i < waffle4.size(); i++){
            
            int c=(int)waffle4[i];
            GenerateKey(c, FALSE);
            
        }
        
        Sleep(1000);
        GenerateKey(0x0D, FALSE); /* enter key */
        
        
        
        
    }
    
    return 0;
    
}
Last edited on
Topic archived. No new replies allowed.