Get key

Pages: 12
didn't work the way I wanted it to... I mean it was great, but i want to run this program as a service, so if i switch windows (if one window closes to reveal another window -mabey 2 different levels, i dont know-) the input prcess follows (aka, i dont have to recode the keys).

What's the syntax for GetAsyncKeyState?
1
2
3
4
5
6
7
8
#include <windows.h>

//...

if(GetASyncKeyState('A') & 0x8000)
{
  // user is currently holding the 'A' key down
}


For non-character keys (space, ctrl, symbols, arrows, etc), you can use the VK_XXX keycodes outlined on this page:

http://msdn.microsoft.com/en-us/library/dd375731(v=VS.85).aspx

Note that GetASyncKeyState is a nonblocking call which means it will not wait for the user to press a button. It will return immediately.
So, how do i get it not to make infinite a's on my screen?
Last edited on
As I mentioned GetASyncKeyState is nonblocking -- it immediately gives you the realtime state of that key. So if you are checking it in a loop and echoing the keypress to cout, it will spit out a bunch of As as long as the key is held down.

So the question is... what kind of behavior are you looking for?
well... thats odd, it's spitting sh*t out even if a key isn't held...
Here's my code so far (this is for testing purposes):

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>
#include <conio.h>
#include <ctype.h>
using namespace std;

char write(char ch)
{
    ofstream log;
    log.open("log.txt", ios::app);
    log<< ch;
    log.close();
    return 0;
}

int main()
{
    char ch;
    unsigned int x;
    x = 0;
    do
    {
        if (GetAsyncKeyState('A') & 0x8000);
        {
            ch = 'a';
            write(ch);
        }
        if (GetAsyncKeyState('B') & 0x8000);
        {
            ch = 'b';
            write(ch);
        }
        if (GetAsyncKeyState('C') & 0x8000) ;
        {
            ch = 'c';
            write(ch);
        }
        if (GetAsyncKeyState('D') & 0x8000);
        {
            ch = 'd';
            write(ch);
        }
        if (GetAsyncKeyState('E') & 0x8000);
        {
            ch = 'e';
            write(ch);
        }
        if (GetAsyncKeyState('F') & 0x8000);
        {
            ch = 'f';
            write(ch);
        }
        if (GetAsyncKeyState('G') & 0x8000);
        {
            ch = 'g';
            write(ch);
        }
        if (GetAsyncKeyState('H') & 0x8000);
        {
            ch = 'h';
            write(ch);
        }
        if (GetAsyncKeyState('I') & 0x8000);
        {
            ch = 'i';
            write(ch);
        }
        if (GetAsyncKeyState('J') & 0x8000);
        {
            ch = 'j';
            write(ch);
        }
        if (GetAsyncKeyState('K') & 0x8000);
        {
            ch = 'k';
            write(ch);
        }
        if (GetAsyncKeyState('L') & 0x8000);
        {
            ch = 'l';
            write(ch);
        }
        if (GetAsyncKeyState('M') & 0x8000);
        {
            ch = 'm';
            write(ch);
        }
        if (GetAsyncKeyState('N') & 0x8000);
        {
            ch = 'n';
            write(ch);
        }
        if (GetAsyncKeyState('O') & 0x8000);
        {
            ch = 'o';
            write(ch);
        }
        if (GetAsyncKeyState('P') & 0x8000);
        {
            ch = 'p';
            write(ch);
        }
        if (GetAsyncKeyState('Q') & 0x8000);
        {
            ch = 'q';
            write(ch);
        }
        if (GetAsyncKeyState('R') & 0x8000);
        {
            ch = 'r';
            write(ch);
        }
        if (GetAsyncKeyState('S') & 0x8000);
        {
            ch = 's';
            write(ch);
        }
        if (GetAsyncKeyState('T') & 0x8000);
        {
            ch = 't';
            write(ch);
        }
        if (GetAsyncKeyState('U') & 0x8000);
        {
            ch = 'u';
            write(ch);
        }
        if (GetAsyncKeyState('V') & 0x8000);
        {
            ch = 'v';
            write(ch);
        }
        if (GetAsyncKeyState('W') & 0x8000);
        {
            ch = 'w';
            write(ch);
        }
        if (GetAsyncKeyState('X') & 0x8000);
        {
            ch = 'x';
            write(ch);
        }
        if (GetAsyncKeyState('Y') & 0x8000);
        {
            ch = 'y';
            write(ch);
        }
        if (GetAsyncKeyState('Z') & 0x8000) ;
        {
            ch = 'z';
            write(ch);
        }
    }
    while (x == 0);
    return 0;
}


i did this so I could open the file afterwards and see what I get (because like I said, the way I'm designing this is to run in the background) and the results so far aren't promising. I keep getting the alphabet printed a million times over in a text file... this indicates to me that it isnt recognizing a key has been pressed, just that the key... well.. is a key, i guess...

After i figure this out, it wont write to a text file, but instead be used as input like "move" or "jump" or even be used to store you set options in the option menu.
Last edited on
well... thats odd, it's spitting sh*t out even if a key isn't held...


Okay you're obviously making a keylogger. Async Keystate will work fine, there are better methods but this is where my help ends. (I'm not being high and mighty, just saying if you want to go this route you should have the capability to do it without hand holding)
look, im a BEGINNER. Even if i succeeded in making a keylogger, i wouldn't know how to use it. The only thing i know is that I need this thing to get a key ONCE, and then respond with an action (which might be jump, or move, or mabey a shortcut to a command) that will corralate to my "game". Since you think im making a keylogger, ill tell you my exact plans:

this background program will write a key pressed to a text file, the game will read the text file, store the key as a variable, link the variable "key" with a command (ex: press w to jump) and do an action. But first, I need the darn thing to write to the text file. This is the easiest command structure I can think of to create a game so that commands stay the same, but are carried on throughout the program even if it closes and opens a graphics window where the code may change.

also, if i was making a keylogger, i wouldn't want to sneak around would i? No, i would want the log to be sent at a time from their computer to mine via internet or whatever. Since i don't know jack about networking with C++, you can rest assure if im keylogging, it wont be real.. well... threatening.
Last edited on
The code you posted should be flooding the file with all characters regardless of what keys are held down. Don't put a semicolon after your if statements.

Also, if you find yourself copy/pasting code en-masse like that, you're doing it wrong. You don't need to duplicate code for each key. Put it in a loop:

1
2
3
4
5
6
7
for(char ch = 'A'; ch <= 'Z'; ++ch)
{
  if(GetASyncKeyState(ch) & 0x8000)  // <- no semicolon
  {
    write( tolower(ch) );
  }
}
oh, ok. I'll try that. Thanks.

Also, what's "tolower" do?
Last edited on
Converts every character in the string it is passed to lower case. Also, might want to plop that code in a function to keep your main nice and clean. You can make it inline so you don't lose any performance when it's being called frequently.
Last edited on
You will need to create a simple key logger to check each key only once. You can do this with array of bools. Each bool will store if each key has already been pressed. If it has then don't output to the file.
ok. Thanks guys! Yall were a real help! I appreciate your time and effort, so now i can move on to my game design instead of this control issue.
Topic archived. No new replies allowed.
Pages: 12